python贪吃蛇小游戏代码
时间: 2023-03-30 18:03:14 浏览: 188
python贪吃蛇游戏代码
5星 · 资源好评率100%
以下是一个简单的 Python 贪吃蛇小游戏代码:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 游戏窗口大小
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
# 定义颜色
BLACK = (, , )
WHITE = (255, 255, 255)
RED = (255, , )
GREEN = (, 255, )
BLUE = (, , 255)
# 创建游戏窗口
game_window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('贪吃蛇小游戏')
# 定义蛇的初始位置和大小
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
# 定义食物的初始位置和大小
food_position = [random.randrange(1, (WINDOW_WIDTH // 10)) * 10,
random.randrange(1, (WINDOW_HEIGHT // 10)) * 10]
food_size = [10, 10]
# 定义初始方向
direction = 'RIGHT'
change_to = direction
# 定义游戏时钟
clock = pygame.time.Clock()
# 定义字体
font_style = pygame.font.SysFont(None, 50)
# 显示分数
def show_score(score):
score_font = font_style.render("Score: " + str(score), True, BLUE)
game_window.blit(score_font, [, ])
# 绘制蛇
def draw_snake(snake_body):
for position in snake_body:
pygame.draw.rect(game_window, GREEN, pygame.Rect(
position[], position[1], 10, 10))
# 绘制食物
def draw_food(food_position, food_size):
pygame.draw.rect(game_window, RED, pygame.Rect(
food_position[], food_position[1], food_size[], food_size[1]))
# 游戏结束
def game_over():
message = font_style.render("Game Over!", True, RED)
game_window.blit(message, [WINDOW_WIDTH / 6, WINDOW_HEIGHT / 3])
pygame.display.update()
pygame.time.delay(200)
pygame.quit()
quit()
# 游戏循环
def game_loop():
# 定义全局变量
global direction
global change_to
global snake_position
global snake_body
global food_position
global score
# 初始化分数
score =
# 游戏是否结束
game_exit = False
while not game_exit:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
elif event.key == pygame.K_LEFT:
change_to = 'LEFT'
elif event.key == pygame.K_UP:
change_to = 'UP'
elif event.key == pygame.K_DOWN:
change_to = 'DOWN'
# 判断方向是否相反
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
elif change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
elif change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
elif change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
# 根据方向移动蛇的位置
if direction == 'RIGHT':
snake_position[] += 10
elif direction == 'LEFT':
snake_position[] -= 10
elif direction == 'UP':
snake_position[1] -= 10
elif direction == 'DOWN':
snake_position[1] += 10
# 判断蛇是否吃到食物
if snake_position == food_position:
food_position = [random.randrange(1, (WINDOW_WIDTH // 10)) * 10,
random.randrange(1, (WINDOW_HEIGHT // 10)) * 10]
score += 10
snake_body.append([snake_position[], snake_position[1]])
# 绘制游戏窗口
game_window.fill(WHITE)
draw_snake(snake_body)
draw_food(food_position, food_size)
show_score(score)
pygame.display.update()
# 判断蛇是否撞墙或撞到自己
if snake_position[] < or snake_position[] > WINDOW_WIDTH - 10:
game_over()
elif snake_position[1] < or snake_position[1] > WINDOW_HEIGHT - 10:
game_over()
for block in snake_body[1:]:
if snake_position == block:
game_over()
# 更新蛇的身体
snake_body.insert(, [snake_position[], snake_position[1]])
snake_body.pop()
# 控制游戏速度
clock.tick(15)
# 退出 Pygame
pygame.quit()
quit()
# 开始游戏
game_loop()
```
希望对你有所帮助!
阅读全文