2016-11-15 16 views
0

私がしようとしていることが間違っているか不可能であるか分かりません。ここに私のコードは次のとおりです。引数1はpygameでなければなりません。ウィンドウではなく表面でなければなりません。

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) 

例外TypeError:引数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) 

私はあなたのコードに2つの変更を行いました。

  • まず、あなたが取得されたエラーのためとして、あなたはpygameのを期待していたことを代わりにpygameののScreenオブジェクトの定義されたカスタムWindowオブジェクトを渡しました。この関数のドキュメントを確認してくださいhere
  • 第2に、元のコンストラクタではデフォルトでthick=Noneが定義されていますが、pygame関数ではintが必要なので、thick=0に変更しました。

これらの2つの変更の後で動作するはずです。まだ問題がある場合は教えてください!

関連する問題