pycharm做一个贪吃蛇游戏
时间: 2023-07-18 19:03:28 浏览: 108
Pygame实现贪吃蛇游戏
5星 · 资源好评率100%
好的,这里是一个简单的贪吃蛇游戏的示例代码,使用 Pygame 模块实现:
```python
import pygame
import random
# 游戏窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 贪吃蛇方块大小
BLOCK_SIZE = 20
# 初始化 Pygame
pygame.init()
# 创建游戏窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("贪吃蛇游戏")
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 定义字体
font = pygame.font.SysFont(None, 25)
# 定义游戏状态
GAME_OVER = 0
GAME_PLAYING = 1
# 定义游戏速度
FPS = 10
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.blocks = [(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)]
self.direction = "right"
self.color = GREEN
def draw(self):
for block in self.blocks:
pygame.draw.rect(window, self.color, (block[0], block[1], BLOCK_SIZE, BLOCK_SIZE))
def move(self):
head = self.blocks[0]
if self.direction == "right":
new_head = (head[0] + BLOCK_SIZE, head[1])
elif self.direction == "left":
new_head = (head[0] - BLOCK_SIZE, head[1])
elif self.direction == "up":
new_head = (head[0], head[1] - BLOCK_SIZE)
elif self.direction == "down":
new_head = (head[0], head[1] + BLOCK_SIZE)
self.blocks.insert(0, new_head)
self.blocks.pop()
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"
def check_collision(self):
head = self.blocks[0]
if head[0] < 0 or head[0] >= WINDOW_WIDTH or head[1] < 0 or head[1] >= WINDOW_HEIGHT:
return True
for block in self.blocks[1:]:
if head == block:
return True
return False
def eat(self, food):
head = self.blocks[0]
if head == food:
self.blocks.insert(0, food)
# 定义食物类
class Food:
def __init__(self):
self.color = RED
self.generate_new_position()
def draw(self):
pygame.draw.rect(window, self.color, (self.position[0], self.position[1], BLOCK_SIZE, BLOCK_SIZE))
def generate_new_position(self):
x = random.randint(0, (WINDOW_WIDTH - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
y = random.randint(0, (WINDOW_HEIGHT - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
self.position = (x, y)
# 初始化游戏
snake = Snake()
food = Food()
game_state = GAME_PLAYING
score = 0
# 游戏主循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
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")
# 游戏逻辑
if game_state == GAME_PLAYING:
snake.move()
if snake.check_collision():
game_state = GAME_OVER
snake.eat(food.position)
if snake.blocks[0] == food.position:
food.generate_new_position()
score += 1
# 绘制画面
window.fill(BLACK)
if game_state == GAME_PLAYING:
snake.draw()
food.draw()
text = font.render("Score: {}".format(score), True, WHITE)
window.blit(text, (10, 10))
elif game_state == GAME_OVER:
text = font.render("Game Over! Score: {}".format(score), True, WHITE)
window.blit(text, (WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 - 25))
# 更新画面
pygame.display.update()
# 控制游戏速度
pygame.time.Clock().tick(FPS)
```
这个示例代码实现了一个基本的贪吃蛇游戏,通过 Pygame 模块来绘制游戏窗口和游戏元素。你可以根据自己的需求对代码进行修改和调整。
阅读全文