试着用OOP在乒乓球中弹跳球

2024-09-30 05:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

所以最近我进入了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()

Tags: 对象selfsizeinitdefscreenpygameoop
1条回答
网友
1楼 · 发布于 2024-09-30 05:20:18

我认为你在循环中使用了错误的条件。意思应该是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

相关问题 更多 >

    热门问题