2016-11-15 124 views
0

不知道我試圖做的是錯的還是不可能的。這裏是我的代碼:參數1必須是pygame.Surface,而不是窗口

import pygame 

class Window(object): 
    def __init__(self, (width, height), color, cap=' '): 
     self.width = width 
     self.height = height 
     self.color = color 
     self.cap = cap 
     self.screen = pygame.display.set_mode((self.width, self.height)) 
    def display(self): 
     self.screen 
     #screen = 
     pygame.display.set_caption(self.cap) 
     self.screen.fill(self.color) 

class Ball(object): 
    def __init__(self, window, (x, y), color, size, thick=None): 
     self.window = window 
     self.x = x 
     self.y = y 
     self.color = color 
     self.size = size 
     self.thick = thick 
    def draw(self): 
     pygame.draw.circle(self.window, self.color, (self.x, self.y), 
          self.size, self.thick) 

def main(): 
    black = (0, 0, 0) 
    white = (255, 255, 255) 
    screen = Window((600, 600), black, 'Pong') 
    screen.display() 
    ball = Ball(screen, (300, 300), white, 5) 
    ball.draw() 

    running = True 

    while running: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       running = False 

     pygame.display.flip() 
    pygame.quit() 
main() 

這是錯誤我得到:

Traceback (most recent call last): 
    File "C:\Users\acmil\Desktop\Team 7\newPongLib.py", line 47, in <module> 
    main() 
    File "C:\Users\acmil\Desktop\Team 7\newPongLib.py", line 36, in main 
    ball.draw() 
    File "C:\Users\acmil\Desktop\Team 7\newPongLib.py", line 28, in draw 
self.size, self.thick) 

類型錯誤:參數1必須pygame.Surface,不是窗口

,如果我做我不明白一個Window對象爲什麼它不會在屏幕上畫出一個球。任何幫助表示讚賞。

回答

0

更改您的級以下幾點:

class Ball(object): 
    def __init__(self, window, (x, y), color, size, thick=0): 
     self.window = window 
     self.x = x 
     self.y = y 
     self.color = color 
     self.size = size 
     self.thick = thick 
    def draw(self): 
     pygame.draw.circle(self.window.screen, self.color, (self.x, self.y), 
          self.size, self.thick) 

我做了兩個修改你的代碼。

  • 首先,對於你得到的錯誤,你通過在你所定義的,而不是pygame的的Screen對象pygame的期待一個定製Window對象。查看關於此功能here的文檔。
  • 其次,默認情況下,您的原始構造函數定義爲thick=None,但該pygame函數需要一個int,所以我將其更改爲thick=0

它應該在這兩個變化後工作。讓我知道如果你仍然有問題!

相關問題