注意:我真的是编程新手。
首先,我将发布我编写的代码:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode([640, 480])
paddle = pygame.image.load("pulgake.png")
pygame.display.set_caption("PONG!")
WHITE = [0, 0, 0]
back = pygame.image.load("taust.png")
screen.blit(back, (0, 0))
screen.blit(paddle, (600, 240))
pygame.display.flip()
xpaddle = 600
ypaddle = 240
delay = 10
interval = 10
pygame.key.set_repeat(delay, interval)
class Ball:
def __init__(self):
self.pos = [0,0]
self.velocity = [1,0]
def move():
self.pos[0] += self.velocity[0]
self.pos[1] += self.velocity[1]
if self.pos[0] > 10:
self.pos[0] *= -1
elif self.pos[1] > 470: # My background has a frame
self.pos[1] *= -1
elif self.pos[1] < 10:
self.pos[1] *= -1
elif self.pos[0] > 605:
False
def draw(screen):
pygame.draw.circle(screen,WHITE,self.pos,15)
pall = Ball()
while True:
pall.move()
screen.blit(back, (0,0))
screen.blit(paddle, (xpaddle, ypaddle))
pygame.display.flip()
for i in pygame.event.get():
if i.type == pygame.QUIT:
sys.exit()
elif i.type == pygame.KEYDOWN:
if i.key == pygame.K_UP:
ypaddle = ypaddle - 10
if ypaddle < 10:
ypaddle = 10
elif i.key == pygame.K_DOWN:
ypaddle = ypaddle + 10
if ypaddle > 410:
ypaddle = 410
我收到这个错误-
line 44, in <module>
pall.move()
TypeError: move() takes no arguments (1 given)
所以,我想得到一个能从墙上反弹的球(还有桨,但我还不知道怎么做)
另外,如果有人帮助我在这场比赛中获得秒表,我将非常感激,显示时间“幸存”。
编辑:添加def move(self)
现在,发生了一个更大的问题:
draw() 和 move() - 未定义全局名称“self”。我实在想不通这个。