Python 乒乓球游戏中弹跳球的OOP实现
所以最近我进入了OOP,这对我来说是一个非常新的话题,我已经制作了一个不使用对象的乒乓球游戏,我正在考虑使用对象编写一个新的脚本。问题是,当我运行代码时,它会显示一个空屏幕,我不确定我做错了什么(我还是OOP新手)。有人能帮忙吗Python 乒乓球游戏中弹跳球的OOP实现,python,oop,pygame,Python,Oop,Pygame,所以最近我进入了OOP,这对我来说是一个非常新的话题,我已经制作了一个不使用对象的乒乓球游戏,我正在考虑使用对象编写一个新的脚本。问题是,当我运行代码时,它会显示一个空屏幕,我不确定我做错了什么(我还是OOP新手)。有人能帮忙吗 import pygame class Ball(): def __init__(self, x, y, xmove, ymove, color, size): self.x = 0 self.y = 0 se
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()
我认为你在循环中使用了错误的条件。它的意思应该是
完成时==False:
或完成时!=正确:
而且你的构造函数是错误的。您给球的参数永远不会设置,因为您使用默认值初始化了所有参数。请改用此构造函数
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
天哪,你说得对!我刚读了一本教程,对设置默认值和使用属性分配变量感到困惑。非常感谢!!:D