So recently I got into OOP, which is a very new topic for me, and I had already made a pong game that did not employ objects and I'm thinking of doing a new script with objects utilised. The problem is, when I run the code, it displays an empty screen and I'm not sure what I did wrong (I'm still new to OOP). Can anyone help out?
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()
I think you used a wrong condtion in your loop. It should mean while done == False:
or while done != True:
Also your constructor is wrong. The parameter you give to the ball will never set, because you initialized all paramters with default values. Use this constructor instead
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.