Python实现简单的贪吃蛇游戏
整体思路
贪吃蛇游戏可以分为三个模块:蛇的移动、食物的出现、蛇和食物的碰撞检测。
蛇的移动
蛇的移动使用Python的turtle模块实现。我们需要创建一个蛇类,用来存储蛇的坐标、方向、身体长度等信息。当蛇向上、下、左、右移动的时候,我们只需要将蛇头的坐标变为前一个身体坐标的值即可。蛇尾的坐标也需要随着蛇头的移动而更新,保证蛇长度不变。
食物的出现
食物的出现是一个随机坐标,只需要在游戏区域内随机生成一个坐标即可。需要注意的是,食物不能出现在蛇的身体上,否则游戏结束。在检测到食物和蛇的碰撞后,需要将食物重新生成一个随机坐标。
蛇和食物的碰撞检测
每当蛇头的坐标和食物坐标重合时,代表蛇吃到了食物,此时需要更新蛇的身体长度,重新生成食物坐标。与此同时,还需要判断蛇是否碰到了边界或碰到了自己的身体,从而结束游戏。
示例代码
以下代码是一个简单的贪吃蛇游戏实现,它包括了上述三个模块的功能。可以通过Python的turtle库来运行该脚本。
import turtle
import random
class Snake:
def __init__(self):
self.snake = [(0, 0), (0, -20), (0, -40)]
self.direction = "Up"
self.body_size = 3
def move(self):
if self.direction == "Up":
new_head = (self.snake[0][0], self.snake[0][1] + 20)
elif self.direction == "Down":
new_head = (self.snake[0][0], self.snake[0][1] - 20)
elif self.direction == "Left":
new_head = (self.snake[0][0] - 20, self.snake[0][1])
elif self.direction == "Right":
new_head = (self.snake[0][0] + 20, self.snake[0][1])
self.snake.insert(0, new_head)
if len(self.snake) > self.body_size:
self.snake.pop()
def eat_food(self):
self.body_size += 1
def change_direction(self, direction):
if direction == "Up" and self.direction != "Down":
self.direction = direction
elif direction == "Down" and self.direction != "Up":
self.direction = direction
elif direction == "Left" and self.direction != "Right":
self.direction = direction
elif direction == "Right" and self.direction != "Left":
self.direction = direction
class Game:
def __init__(self):
self.window = turtle.Screen()
self.window.setup(600, 600)
self.window.title("Snake Game")
self.snake = Snake()
self.food = self.create_food()
self.create_border()
self.is_game_over = False
self.score = 0
self.show_score()
self.window.onkey(lambda: self.snake.change_direction("Up"), "Up")
self.window.onkey(lambda: self.snake.change_direction("Down"), "Down")
self.window.onkey(lambda: self.snake.change_direction("Left"), "Left")
self.window.onkey(lambda: self.snake.change_direction("Right"), "Right")
self.window.listen()
def create_food(self):
while True:
food = (random.randint(-280, 280), random.randint(-280, 280))
if food not in self.snake.snake:
return food
def create_border(self):
border = turtle.Turtle()
border.hideturtle()
border.speed(0)
border.penup()
border.goto(-290, -290)
border.pendown()
border.goto(-290, 290)
border.goto(290, 290)
border.goto(290, -290)
border.goto(-290, -290)
def show_score(self):
score = turtle.Turtle()
score.hideturtle()
score.speed(0)
score.penup()
score.goto(0, 260)
score.write(f"Score: {self.score}", align="center", font=("Courier", 24, "normal"))
def update_score(self):
self.score += 10
self.show_score()
def game_over(self):
self.is_game_over = True
message = turtle.Turtle()
message.hideturtle()
message.speed(0)
message.penup()
message.goto(0, 0)
message.write("Game Over", align="center", font=("Courier", 24, "normal"))
def run(self):
while not self.is_game_over:
self.snake.move()
if self.snake.snake[0] == self.food:
self.snake.eat_food()
self.food = self.create_food()
self.update_score()
if self.snake.snake[0][0] < -280 or self.snake.snake[0][0] > 280 or \
self.snake.snake[0][1] < -280 or self.snake.snake[0][1] > 280:
self.game_over()
for i in range(1, len(self.snake.snake)):
if self.snake.snake[0] == self.snake.snake[i]:
self.game_over()
turtle.clear()
self.create_border()
turtle.penup()
turtle.goto(self.food)
turtle.color("red")
turtle.dot(20)
for body in self.snake.snake:
turtle.penup()
turtle.goto(body)
turtle.color(0, 0, 0)
turtle.pendown()
turtle.dot(20)
self.window.update()
self.window.mainloop()
game = Game()
game.run()
这是一个基本的从上至下的贪吃蛇,窗口大小为600x600。在首次运行游戏时,它将首先创建蛇和食物,并设置监听键盘方向输入。当蛇碰到边界或碰到自己的身体时,游戏结束。每当蛇吃到食物时,身体长度增加,并重新生成一个食物。执行时,需要下载turtle库。例如,使用pip install turtle命令安装它。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python实现简单的贪吃蛇游戏 - Python技术站