所以这是我的第一个游戏(蛇),它给了我一个错误:ln 21, in update
self.rect.centerx += self.direction(0) *2, 'tuple' object is not callable.
我已经查找了答案,但我似乎没有找到我的问题,我不知道'tuple'的意思,完整的游戏代码在这里:
import pygame
from pygame.locals import *
class SnakeHead(object):
def __init__(self):
self.image = pygame.image.load('data/snake_head.png')
self.rect = self.image.get_rect()
self.rect.centerx = 320
self.rect.centery = 240
self.direction = (0,-1)
self.crashed = False
def render(self, screen):
screen.blit(self.image, self.rect)
def update(self):
self.rect.centerx += self.direction(0) * 2
self.rect.centery += self.direction(1) * 2
if self.rect.left < 0:
self.rect.left = 0
self.crashed = True
if self.rect.right > 639:
self.rect.right = 639
self.crashed = True
if self.rect.top < 0:
self.rect.top = 0
self.crashed = True
if self.rect.bottom > 479:
self.rect.bottom = 479
self.crashed = True
def run_game():
pygame.init()
screen = pygame.display.set_mode((640,480))
snakehead = SnakeHead()
clock = pygame.time.Clock()
while 1:
clock.tick(32)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
return
screen.fill((175,175,175))
snakehead.update()
snakehead.render(screen)
pygame.display.flip() `
我一直在问自己,探测器是什么时候超过2小时,但找不到任何东西,如果有人能告诉我,我会非常高兴:)
-Xattics
答案 0 :(得分:7)
这一行是问题
self.rect.centerx += self.direction(0) * 2
direction
是一个tuple
,如果你想索引你应该做
self.rect.centerx += self.direction[0] * 2