用Python写一个贪吃蛇游戏
时间: 2024-03-06 17:40:42 浏览: 112
好的,我可以为您提供一些关于用Python写贪吃蛇游戏的资源,您可以先从这些资源入手。
1. 一篇Python实现的贪吃蛇游戏的教程:https://realpython.com/beginners-guide-python-turtle/
2. 一个GitHub项目,里面有可运行的Python蛇游戏:https://github.com/MattKleinsmith/snake-python
希望这些资源对您有所帮助!
相关问题
使用python写一个贪吃蛇游戏
好的,下面是一个简单的贪吃蛇游戏代码示例:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
# 设置窗口大小
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("贪吃蛇游戏")
# 设置游戏时钟
clock = pygame.time.Clock()
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.snake_list = [(250, 250), (240, 250), (230, 250)]
self.direction = "right"
def move(self):
if self.direction == "right":
head = (self.snake_list[0][0]+10, self.snake_list[0][1])
elif self.direction == "left":
head = (self.snake_list[0][0]-10, self.snake_list[0][1])
elif self.direction == "up":
head = (self.snake_list[0][0], self.snake_list[0][1]-10)
elif self.direction == "down":
head = (self.snake_list[0][0], self.snake_list[0][1]+10)
self.snake_list.insert(0, head)
self.snake_list.pop(-1)
def change_direction(self, direction):
if direction == "right" and self.direction != "left":
self.direction = "right"
elif direction == "left" and self.direction != "right":
self.direction = "left"
elif direction == "up" and self.direction != "down":
self.direction = "up"
elif direction == "down" and self.direction != "up":
self.direction = "down"
# 定义食物类
class Food:
def __init__(self):
self.position = (random.randint(0, 49)*10, random.randint(0, 49)*10)
def generate(self):
self.position = (random.randint(0, 49)*10, random.randint(0, 49)*10)
# 创建贪吃蛇和食物对象
snake = Snake()
food = Food()
# 是否结束游戏的标志
game_over = False
# 游戏循环
while not game_over:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
snake.change_direction("right")
elif event.key == pygame.K_LEFT:
snake.change_direction("left")
elif event.key == pygame.K_UP:
snake.change_direction("up")
elif event.key == pygame.K_DOWN:
snake.change_direction("down")
# 移动贪吃蛇
snake.move()
# 判断是否吃到食物
if snake.snake_list[0] == food.position:
food.generate()
snake.snake_list.append(snake.snake_list[-1])
# 判断是否游戏结束
if snake.snake_list[0][0] < 0 or snake.snake_list[0][0] >= 500 or snake.snake_list[0][1] < 0 or snake.snake_list[0][1] >= 500:
game_over = True
for i in range(1, len(snake.snake_list)):
if snake.snake_list[0] == snake.snake_list[i]:
game_over = True
# 绘制游戏画面
screen.fill(white)
for pos in snake.snake_list:
pygame.draw.rect(screen, green, [pos[0], pos[1], 10, 10])
pygame.draw.rect(screen, red, [food.position[0], food.position[1], 10, 10])
pygame.display.update()
# 控制游戏帧率
clock.tick(10)
# 退出pygame
pygame.quit()
```
运行这段代码,即可看到一个简单的贪吃蛇游戏窗口。使用方向键控制贪吃蛇的移动,尽可能吃到食物,避免碰到墙壁或自己的身体。
用python写一个贪吃蛇游戏
好的,我来给你写一个贪吃蛇游戏的基本框架。首先,你需要准备好相关的库,比如 pygame。
```python
import pygame
import random
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 初始化游戏
pygame.init()
# 设置窗口大小和标题
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('贪吃蛇')
# 设置帧率
clock = pygame.time.Clock()
# 初始化蛇的位置
snake_pos = [(200, 200), (210, 200), (220, 200)]
# 初始化食物的位置
food_pos = (random.randint(0, 39) * 10, random.randint(0, 29) * 10)
food_spawn = True
# 初始化蛇的方向
direction = 'RIGHT'
change_to = direction
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 控制蛇的方向
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
if event.key == pygame.K_LEFT:
change_to = 'LEFT'
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
# 确保蛇不能反向移动
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# 根据方向移动蛇的头部
if direction == 'UP':
snake_pos[0] = (snake_pos[0][0], snake_pos[0][1] - 10)
if direction == 'DOWN':
snake_pos[0] = (snake_pos[0][0], snake_pos[0][1] + 10)
阅读全文