所以最近我进入了OOP,这对我来说是一个非常新的话题,我已经做了一个不使用对象的pong游戏,我正在考虑使用对象来编写一个新的脚本。问题是,当我运行代码时,它会显示一个空屏幕,我不确定我做错了什么(我还是OOP新手)。有人能帮忙吗?你知道吗
import pygame
class Ball():
def __init__(self, x, y, xmove, ymove, color, size):
self.x = 0
self.y = 0
self.xmove = 0
self.ymove = 0
self.color = (255, 255, 255)
self.size = 10
def draw(self, screen):
pygame.draw.circle(screen, self.color, [self.x, self.y], self.size)
def ballmove(self):
self.x += self.xmove
self.y += self.ymove
done = False
pygame.init()
WIDTH = 640
HEIGHT = 480
clock = pygame.time.Clock()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
ball = Ball(0, 0, 1.5, 1.5, [255, 255, 255], 10)
while done != False:
screen.fill(0)
ball.ballmove()
ball.draw(screen)
pygame.display.update()
我认为你在循环中使用了错误的条件。意思应该是
while done == False:
或while done != True:
你的构造器也错了。你给球的参数永远不会设置,因为你用默认值初始化了所有的参数。改用此构造函数
相关问题 更多 >
编程相关推荐