pycharm贪吃蛇代码
时间: 2024-05-25 17:09:20 浏览: 162
python 贪吃蛇代码
5星 · 资源好评率100%
PyCharm 是一款Python集成开发环境,可以用于编写和调试Python程序。关于贪吃蛇代码,它是一个比较经典的小游戏,可以通过Python语言编写实现。以下是一个简单的贪吃蛇代码示例:
```python
import pygame, sys, random, time
from pygame.locals import *
# 初始化pygame库
pygame.init()
# 设置窗口大小
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
CELL_SIZE = 20
# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 创建一个窗口
window_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Greedy Snake')
# 定义一个函数,用来画蛇和食物
def draw_item(item_x, item_y, item_color):
pygame.draw.rect(window_surface, item_color, Rect(item_x, item_y, CELL_SIZE, CELL_SIZE))
# 定义一个函数,用来打印文字
def print_text(font, x, y, text, color):
imgText = font.render(text, True, color)
window_surface.blit(imgText, (x, y))
# 设置字体
font1 = pygame.font.SysFont('SimHei', 24)
font2 = pygame.font.Font(None, 72)
# 定义游戏状态常量
GAME_OVER = -1
GAME_RUN = 0
GAME_WIN = 1
# 定义蛇的初始位置和长度
snake_position = [(0, 0), (20, 0), (40, 0), (60, 0)]
snake_length = 4
snake_direction = 'right'
# 定义食物的初始位置
food_position = (100, 100)
# 定义游戏状态
game_status = GAME_RUN
# 设置游戏帧率
fps_clock = pygame.time.Clock()
FPS = 5
# 游戏循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT and snake_direction != 'left':
snake_direction = 'right'
elif event.key == K_LEFT and snake_direction != 'right':
snake_direction = 'left'
elif event.key == K_UP and snake_direction != 'down':
snake_direction = 'up'
elif event.key == K_DOWN and snake_direction != 'up':
snake_direction = 'down'
elif event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
# 根据蛇的方向移动蛇头位置,并将蛇头加入到蛇的位置列表中
if game_status == GAME_RUN:
if snake_direction == 'right':
new_head = (snake_position[-1] + CELL_SIZE, snake_position[-1])
elif snake_direction == 'left':
new_head = (snake_position[-1] - CELL_SIZE, snake_position[-1])
elif snake_direction == 'up':
new_head = (snake_position[-1], snake_position[-1] - CELL_SIZE)
elif snake_direction == 'down':
new_head = (snake_position[-1], snake_position[-1] + CELL_SIZE)
snake_position.append(new_head)
# 如果蛇头和食物位置相同,则将食物重新放置,并将蛇长度加1
if snake_position[-1] == food_position:
food_position = (random.randint(0, WINDOW_WIDTH // CELL_SIZE - 1) * CELL_SIZE,
random.randint(0, WINDOW_HEIGHT // CELL_SIZE - 1) * CELL_SIZE)
snake_length += 1
# 如果蛇头位置超出窗口范围或与蛇身重叠,则游戏结束
if not (0 <= snake_position[-1] < WINDOW_WIDTH and 0 <= snake_position[-1] < WINDOW_HEIGHT) or \
len(snake_position) != len(set(snake_position)):
game_status = GAME_OVER
# 如果蛇长度等于窗口格子数,则游戏胜利
elif len(snake_position) - snake_length > WINDOW_WIDTH * WINDOW_HEIGHT // CELL_SIZE ** 2:
game_status = GAME_WIN
# 如果蛇长度小于等于4,则无法转弯,只能直行
if len(snake_position) <= 4:
snake_direction = 'right'
# 删除蛇尾,以保持蛇长度不变
if len(snake_position) > snake_length:
del snake_position
# 绘制游戏界面
window_surface.fill(WHITE)
for x in range(0, WINDOW_WIDTH, CELL_SIZE):
pygame.draw.line(window_surface, BLACK, (x, 0), (x, WINDOW_HEIGHT))
for y in range(0, WINDOW_HEIGHT, CELL_SIZE):
pygame.draw.line(window_surface, BLACK, (0, y), (WINDOW_WIDTH, y))
# 绘制蛇和食物
for pos in snake_position:
draw_item(pos, pos, BLACK)
draw_item(food_position, food_position, RED)
# 绘制分数和状态信息
print_text(font1, 10, 10, f"Length: {snake_length}", BLACK)
if game_status == GAME_OVER:
print_text(font2, WINDOW_WIDTH // 2 - 160, WINDOW_HEIGHT // 2 - 50, "Game Over", BLACK)
elif game_status == GAME_WIN:
print_text(font2, WINDOW_WIDTH // 2 - 160, WINDOW_HEIGHT // 2 - 50, "You Win", BLACK)
# 更新屏幕显示
pygame.display.update()
# 控制游戏帧率
fps_clock.tick(FPS)
# 关闭pygame库
pygame.quit()
```
阅读全文