[英]Trying to use OOP for bouncing ball in pong game
所以最近我进入了面向对象编程(OOP),这对我来说是一个非常新的话题,而且我已经制作了一个不使用对象的乒乓游戏,并且我正在考虑使用已利用的对象制作一个新脚本。 问题是,当我运行代码时,它显示一个空白屏幕,而且我不确定自己做错了什么(我还是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:
而且您的构造函数是错误的。 您提供给球的参数将永远不会设置,因为您已使用默认值初始化了所有参数。 改用此构造函数
def __init__(self, x, y, xmove, ymove, color, size):
self.x = x
self.y = y
self.xmove = xmove
self.ymove = ymove
self.color = color
self.size = size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.