2016-11-15 94 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

它应该在这两个变化后工作。让我知道如果你仍然有问题!

相关问题