2014-10-18 90 views
0

[解決]蟒蛇pygame的int對象錯誤

蟒蛇扔我一個一個類型的錯誤,當我嘗試運行我的pygame的劇本,我找不到任何解決方案.. 我已經看了看周圍的其他職位,但couldn解決方案找不到任何幫助。我在哪裏錯了? 錯誤;

Traceback (most recent call last): 
    File "pygameclass.py", line 43, in <module> 
    ball.append(Ball(25, 400, 300 (50,50,50), "L", 25, 1, 100)) 
TypeError: 'int' object is not callable 

我的代碼;

import pygame, sys, random 
from pygame.locals import * 

w = 800 
h = 400 

z = 0 

screen = pygame.display.set_mode((w,h)) 

pygame.display.update() 

class Ball: 
    def __init__(self, radius, y,x , color, size, maxforce, force, life): 
     self.y = y 
     self.x = x 
     self.size =size 
     self.maxforce = maxforce 
     self.force = force 
     self.radius = radius 
     self.color = color 
     self.life = life 
     pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius) 

    def fall (self): 
     if self.y < h-self.radius: 
      self.y +=self.force 
      if self.force < self.maxforce: 
       self.force+=1 
      elif self.y > h-self.radius or self.y == h-self.raidus: 
       self.y = h-self.radius -1 
       self.force = self.force*-1 
       self.maxforce = self.maxforce/2 
      pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius) 
      self.life-=1 
      if self.life<0: 
       ball.remove(self) 



clock=pygame.time.Clock() 
ball = [] 
ball.append(Ball(25, 400, 300 (50,50,50), "L", 25, 1, 100)) 

while True: 
    clock.tick(60) 
    x,y = pygame.mouse.get_pos() 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 

    screen.fill((0,0,0)) 
    for i in ball: 
     i.fall 

回答

1

剛剛錯過的元組前的逗號:

ball.append(Ball(25, 400, 300 <- missing a comma -> (50,50,50), "L", 25, 1, 100)) 

ball.append(Ball(25, 400, 300,(50,50,50), "L", 25, 1, 100))

你還缺少括號調用秋季方法在你的循環:

for i in ball: 
    i.fall <- should be i.fall() 

而且拼寫錯誤這裏:

elif self.y > h-self.radius or self.y == h-self.raidus <- should be self.radius 
+0

你有沒有考慮過這樣做的生活?你已經救了自己很多的挫折,儘管儘管這些修復,球似乎不幹,我只剩下一個黑屏.. – 2014-10-18 20:57:49

+0

不知道爲什麼,但你有另一個問題'ball.remove(self) '在那個秋天的方法,沒有球 – 2014-10-18 21:02:57

+0

這可能是問題,爲什麼我留下了一個空白的屏幕,對不起,你已經篩選過的爛攤子,這是我第一天學習使用pygame – 2014-10-18 21:10:41

1

看起來你忘了一個逗號。

ball.append(Ball(25, 400, 300, (50,50,50), "L", 25, 1, 100)) 

它認爲你試圖調用函數300(),這是不可能的。

+0

非常感謝,該死的我是一個白癡 – 2014-10-18 20:50:56