【发布时间】:2020-05-31 20:37:57
【问题描述】:
作为一个新手,我想在 Pygame 中使用 OOP 编写一个蛇游戏。我在 Youtube 上复制了一个教程(就像你一样),我完全不明白为什么蛇只是朝一个方向移动并且不会打开按键。我试图通过在函数中插入文本来添加线索,以便我可以查看按键是否响应它们,但我已经尝试了 2 个小时试图解决它并且我被卡住了。请有知识的人快速检查一下代码并给我一个提示。我会很感激的。
import pygame
import sys
import random
import time
class Snake():
def __init__(self):
self.position = [100,550]
self.body = [[100,550],[90,550],[80,550]]
self.direction = "DOWN"
self.changeDirectionTo = self.direction
def changeDirectTo(self,dir):
if dir == "RIGHT" and not self.direction == "LEFT":
self.direction == "RIGHT"
print ("In changeDirectTo function 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
print ("in move function Right")
if self.direction == "LEFT":
self.position[0] -= 10
print ("in move function left")
if self.direction == "UP":
self.position[1] += 10
if self.direction == "DOWN":
self.position[1] -= 10
print ("in move function down")
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] > 1490 or self.position[0] <0:
return 1
elif self.position[1] > 1490 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 FoodSpawer():
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
self.isFoodOnScreen = True
return self.position
def setFoodOnScreen(self, b):
self.isFoodOnScreen = b
window = pygame.display.set_mode((1200,1200))
pygame.display.set_caption("Snake by Mike")
fps = pygame.time.Clock()
score = 0
snake = Snake()
foodSpawner = FoodSpawer()
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.changeDirectTo("RIGHT")
print("Right Press")
if event.key == pygame.K_LEFT:
snake.changeDirectTo("LEFT")
print("Left Press")
if event.key == pygame.K_UP:
snake.changeDirectTo("UP")
print("Up Press")
if event.key == pygame.K_DOWN:
snake.changeDirectTo("DOWN")
print("Down Press")
foodPos = foodSpawner.spawnFood()
if (snake.move(foodPos) ==1):
score += 1
foodSpawner.setFoodOnScreen(False)
window.fill(pygame.Color(255,255,255))
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()
pygame.display.set_caption("Snake by Mike | score: " + str(score))
pygame.display.flip()
fps.tick(14)
【问题讨论】:
-
self.direction == "RIGHT"是比较操作而不是赋值。必须是self.direction = "RIGHT" -
当然。如此基本的东西让我很生气。非常感谢
标签: python-3.x oop pygame