2014-10-18 45 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