2017-09-26 74 views
0

所以最近我進入了OOP,這對我來說是一個非常新的話題,而且我已經制作了一個沒有使用對象的乒乓球遊戲,我正在考慮做一個使用對象的新腳本。問題是,當我運行代碼時,它顯示一個空白屏幕,我不確定我做錯了什麼(我還是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() 

回答

1

我想你在循環中使用了一個錯誤的condtion。它應該意味着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 
+0

Omg你說得對!我剛剛閱讀了一本教程書,我對設置默認值和賦予變量屬性感到困惑。非常感謝!! :d –