TypeError:'方法'对象在pygame

时间:2018-04-28 01:12:33

标签: python-3.x pygame

我的课程代码有问题。我一直收到这个错误:TypeError:'方法'对象不可订阅,我不知道在哪里更改内容或更改内容。它显示了我在最后的颜色foor foodPass的行上的错误。我不知道问题是在那里还是我宣布foodPass

    import pygame
import sys
import random
import time


class Snake():
    def __init__(self):
        self.position = [100, 50]
        self.body = [[100, 50], [90, 50], [80, 50]]
        self.direction = "RIGHT"
        self.changeDirectionTo = self.direction

    def changeDirTo(self, dir):
        if dir == "RIGHT" and not self.direction == "LEFT":
            self.direction = "RIGHT"
        if dir == "LEFT" and not self.direction == "RIGHT":
            self.direction = "LEFT"
        if dir == "UP" and not self.direction == "DOWN":
            self.direction = "UP"
        if dir == "DOWN" and not self.direction == "UP":
            self.direction = "DOWN"

    def move(self, foodPos):
        if self.direction == 'RIGHT':
            self.position[0] += 10
        if self.direction == 'LEFT':
            self.position[0] -= 10
        if self.direction == 'UP':
            self.position[1] -= 10
        if self.direction == 'DOWN':
            self.position[1] += 10
        self.body.insert(0, list(self.position))
        if self.position == foodPos:
            return 1
        else:
            self.body.pop()
            return 0

    def checkCollision(self):
        if self.position[0] > 490 or self.position[0] < 0:
            return 1
        elif self.position[1] > 490 or self.position[1] < 0:
            return 1
        for bodyPart in self.body[1:]:
            if self.position == bodypart:
                return 1
        return 0

    def getHeadPos(self):
        return self.position

    def getBody(self):
        return self.body


class FoodSpawner():
    def __init__(self):
        self.position = [random.randrange(1, 50)*10, random.randrange(1, 50)*10]
        self.isFoodVis = True

    def spawnFood(self):
        if self.isFoodVis == False:
            self.position = [random.randrange(1,50)*10, random.randrange(1,50)*10]
            self.position = True
        return self.position

    def setFood(self,b):
        self.isFoodVis = b

window = pygame.display.set_mode((500,500))
pygame.display.set_caption("Sanke Game")
fps = pygame.time.Clock()

score = 0

snake = Snake()
foodSpawner = FoodSpawner()

def gameOver():
    pygame.quit()
    sys.exit()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameOver()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                snake.changeDirTo('RIGHT')
            if event.key == pygame.K_LEFT:
                snake.changeDirTo('LEFT')
            if event.key == pygame.K_Up:
                snake.changeDirTo('UP')
            if event.key == pygame.K_DOWN:
                snake.changeDirTo('DOWN')

    foodPos = foodSpawner.spawnFood
    if(snake.move(foodPos)==1):
        score+=1
        foodSpawner.setFoodOnScreen(False)


    pygame.display.set_caption("Sanke Game | Score : "+ str(score) )
    pygame.display.flip
    fps.tick(24)

    window.fill(pygame.Color(225,225,225))
    for pos in snake.getBody():
        pygame.draw.rect(window,pygame.Color(0,225,0),pygame.Rect(pos[0],pos[1],10,10))
    pygame.draw.rect(window,pygame.Color(225,0,0),pygame.Rect(foodPos[0],foodPos[1],10,10))
    if(snake.checkCollision()==1):
        gameOver()

2 个答案:

答案 0 :(得分:2)

这只是一个小错字,你声明foodPos。它应该是:

   foodPos = foodSpawner.spawnFood()

答案 1 :(得分:2)

当调用函数spawnFood时,您会缺少括号。

foodPos = foodSpawner.spawnFood

应该是:

foodPos = foodSpawner.spawnFood()