Wie löse ich dieses Problem?
Verfasst: Samstag 21. März 2020, 19:48
Problem ist in der Zeile: pygame.draw.rect(win, (255, 0, 0), [foodPos(0), foodPos(1), 10, 10])
Fehlermeldung: 'NoneType' object is not callable
<import pygame
import random
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 == "RIGHT":
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.postion[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.isFoodOnScreen = True
def spawnFood(self):
if self.isFoodOnScreen == False:
self.position = [random.randrange(1, 50) * 10, random.randrange(1, 50) * 10]
self.isFoodOnScreen = True
return self.position
def setFoodOnScreen(self, b):
self.isFoodOnScreen = b
def gameover():
pygame.quit()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Snake Game")
fps = pygame.time.Clock()
score = 0
snake = Snake()
foodSpawner = FoodSpawner()
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.changeDirectionTo('RIGHT')
if event.key == pygame.K_LEFT:
snake.changeDirectionTo('LEFT')
if event.key == pygame.K_UP:
snake.changeDirectionTo('Up')
if event.key == pygame.K_DOWN:
snake.changeDirectionTo('DOWN')
foodPos = foodSpawner.spawnFood()
if snake.move(foodPos) == 1:
score += 1
foodSpawner.setFoodOnScreen(False)
win.fill((255,255,255))
for pos in snake.getBody():
pygame.draw.rect(win, (0, 255, 0), (pos[0], pos[1], 10, 10))
pygame.draw.rect(win, (255, 0, 0), [foodPos(0), foodPos(1), 10, 10])
if snake.checkCollision() == 1:
gameover()
pygame.display.set_caption("Snake Game | Score: " + str(score))
pygame.display.flip()
fps.tick(24)>
Fehlermeldung: 'NoneType' object is not callable
<import pygame
import random
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 == "RIGHT":
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.postion[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.isFoodOnScreen = True
def spawnFood(self):
if self.isFoodOnScreen == False:
self.position = [random.randrange(1, 50) * 10, random.randrange(1, 50) * 10]
self.isFoodOnScreen = True
return self.position
def setFoodOnScreen(self, b):
self.isFoodOnScreen = b
def gameover():
pygame.quit()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Snake Game")
fps = pygame.time.Clock()
score = 0
snake = Snake()
foodSpawner = FoodSpawner()
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.changeDirectionTo('RIGHT')
if event.key == pygame.K_LEFT:
snake.changeDirectionTo('LEFT')
if event.key == pygame.K_UP:
snake.changeDirectionTo('Up')
if event.key == pygame.K_DOWN:
snake.changeDirectionTo('DOWN')
foodPos = foodSpawner.spawnFood()
if snake.move(foodPos) == 1:
score += 1
foodSpawner.setFoodOnScreen(False)
win.fill((255,255,255))
for pos in snake.getBody():
pygame.draw.rect(win, (0, 255, 0), (pos[0], pos[1], 10, 10))
pygame.draw.rect(win, (255, 0, 0), [foodPos(0), foodPos(1), 10, 10])
if snake.checkCollision() == 1:
gameover()
pygame.display.set_caption("Snake Game | Score: " + str(score))
pygame.display.flip()
fps.tick(24)>