设计一个pycharm贪吃蛇小游戏,并编写代码
时间: 2023-08-19 11:32:21 浏览: 388
好的,以下是一个简单的PyCharm贪吃蛇小游戏的代码实现:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 定义窗口大小和背景颜色
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 480
BG_COLOR = (255, 255, 255)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('贪吃蛇')
# 定义贪吃蛇和食物的初始位置和大小
SNAKE_SIZE = 20
snake = [(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2), (SCREEN_WIDTH // 2 - SNAKE_SIZE, SCREEN_HEIGHT // 2), (SCREEN_WIDTH // 2 - 2 * SNAKE_SIZE, SCREEN_HEIGHT // 2)]
food_size = SNAKE_SIZE
food_x = random.randint(0, SCREEN_WIDTH // SNAKE_SIZE - 1) * SNAKE_SIZE
food_y = random.randint(0, SCREEN_HEIGHT // SNAKE_SIZE - 1) * SNAKE_SIZE
# 定义贪吃蛇的移动方向
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
direction = RIGHT
# 定义游戏循环
running = True
clock = pygame.time.Clock()
while running:
# 监听键盘事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != DOWN:
direction = UP
elif event.key == pygame.K_DOWN and direction != UP:
direction = DOWN
elif event.key == pygame.K_LEFT and direction != RIGHT:
direction = LEFT
elif event.key == pygame.K_RIGHT and direction != LEFT:
direction = RIGHT
# 根据移动方向更新贪吃蛇的位置
if direction == UP:
new_head = (snake[0][0], snake[0][1] - SNAKE_SIZE)
elif direction == DOWN:
new_head = (snake[0][0], snake[0][1] + SNAKE_SIZE)
elif direction == LEFT:
new_head = (snake[0][0] - SNAKE_SIZE, snake[0][1])
else:
new_head = (snake[0][0] + SNAKE_SIZE, snake[0][1])
snake.insert(0, new_head)
# 检查是否发生碰撞
if new_head[0] < 0 or new_head[0] >= SCREEN_WIDTH or new_head[1] < 0 or new_head[1] >= SCREEN_HEIGHT or new_head in snake[1:]:
running = False
elif new_head[0] == food_x and new_head[1] == food_y:
# 如果贪吃蛇头部吃到了食物,增加蛇的长度,并在新的位置生成食物
snake.append(snake[-1])
food_x = random.randint(0, SCREEN_WIDTH // SNAKE_SIZE - 1) * SNAKE_SIZE
food_y = random.randint(0, SCREEN_HEIGHT // SNAKE_SIZE - 1) * SNAKE_SIZE
# 更新屏幕
screen.fill(BG_COLOR)
pygame.draw.rect(screen, (255, 0, 0), (food_x, food_y, food_size, food_size))
for pos in snake:
pygame.draw.rect(screen, (0, 255, 0), (pos[0], pos[1], SNAKE_SIZE, SNAKE_SIZE))
pygame.display.update()
# 控制游戏帧率
clock.tick(10)
# 游戏结束时显示得分
score = len(snake) - 3
font = pygame.font.SysFont('arial', 48)
text = font.render('Game over! Your score: {}'.format(score), True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
screen.blit(text, text_rect)
pygame.display.update()
# 等待用户输入重新开始或退出游戏
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_RETURN:
# 重新开始游戏
snake = [(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2), (SCREEN_WIDTH // 2 - SNAKE_SIZE, SCREEN_HEIGHT // 2), (SCREEN_WIDTH // 2 - 2 * SNAKE_SIZE, SCREEN_HEIGHT // 2)]
direction = RIGHT
food_x = random.randint(0, SCREEN_WIDTH // SNAKE_SIZE - 1) * SNAKE_SIZE
food_y = random.randint(0, SCREEN_HEIGHT // SNAKE_SIZE - 1) * SNAKE_SIZE
running = True
elif event.key == pygame.K_ESCAPE:
# 退出游戏
pygame.quit()
exit()
```
以上代码实现了一个简单的PyCharm贪吃蛇小游戏,具体游戏效果可以通过运行代码来体验。
阅读全文