一个完整的贪吃蛇python
时间: 2024-03-07 11:19:20 浏览: 13
以下是一个简单的贪吃蛇游戏的Python实现。它使用了Pygame库来创建游戏窗口和处理用户输入、游戏逻辑等。
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 定义颜色
BLACK = pygame.Color(0, 0, 0)
WHITE = pygame.Color(255, 255, 255)
RED = pygame.Color(255, 0, 0)
GREEN = pygame.Color(0, 255, 0)
BLUE = pygame.Color(0, 0, 255)
# 设置游戏窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
WINDOW_SURFACE = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Greedy Snake')
# 设置游戏时钟
CLOCK = pygame.time.Clock()
# 定义贪吃蛇的初始位置和大小
SNAKE_BLOCK_SIZE = 20
INITIAL_SNAKE_LENGTH = 3
INITIAL_SNAKE_POSITION = [
(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2 + SNAKE_BLOCK_SIZE * i)
for i in range(INITIAL_SNAKE_LENGTH)
]
# 定义食物的初始位置和大小
FOOD_BLOCK_SIZE = 20
FOOD_POSITION = (random.randint(0, WINDOW_WIDTH - FOOD_BLOCK_SIZE),
random.randint(0, WINDOW_HEIGHT - FOOD_BLOCK_SIZE))
# 定义贪吃蛇的移动速度
SNAKE_SPEED = 5
# 定义贪吃蛇的初始方向
DIRECTION_UP = 'up'
DIRECTION_DOWN = 'down'
DIRECTION_LEFT = 'left'
DIRECTION_RIGHT = 'right'
INITIAL_DIRECTION = DIRECTION_UP
# 定义贪吃蛇的初始分数
INITIAL_SCORE = 0
def draw_block(color, position):
"""绘制一个方块"""
block_rect = pygame.Rect(position[0], position[1], SNAKE_BLOCK_SIZE, SNAKE_BLOCK_SIZE)
pygame.draw.rect(WINDOW_SURFACE, color, block_rect)
def draw_snake(snake_blocks):
"""绘制贪吃蛇"""
for block in snake_blocks:
draw_block(GREEN, block)
def move_snake(snake_blocks, direction):
"""移动贪吃蛇"""
head_position = snake_blocks[0]
if direction == DIRECTION_UP:
new_head_position = (head_position[0], head_position[1] - SNAKE_BLOCK_SIZE)
elif direction == DIRECTION_DOWN:
new_head_position = (head_position[0], head_position[1] + SNAKE_BLOCK_SIZE)
elif direction == DIRECTION_LEFT:
new_head_position = (head_position[0] - SNAKE_BLOCK_SIZE, head_position[1])
elif direction == DIRECTION_RIGHT:
new_head_position = (head_position[0] + SNAKE_BLOCK_SIZE, head_position[1])
snake_blocks.insert(0, new_head_position)
snake_blocks.pop()
def generate_food_position():
"""生成新的食物位置"""
return (random.randint(0, WINDOW_WIDTH - FOOD_BLOCK_SIZE),
random.randint(0, WINDOW_HEIGHT - FOOD_BLOCK_SIZE))
def draw_food(food_position):
"""绘制食物"""
draw_block(RED, food_position)
def check_collision(snake_blocks, food_position):
"""检查碰撞"""
if snake_blocks[0][0] == food_position[0] and snake_blocks[0][1] == food_position[1]:
snake_blocks.append(snake_blocks[-1])
return True
return False
def check_game_over(snake_blocks):
"""检查游戏是否结束"""
if snake_blocks[0][0] < 0 or snake_blocks[0][0] >= WINDOW_WIDTH or \
snake_blocks[0][1] < 0 or snake_blocks[0][1] >= WINDOW_HEIGHT:
return True
for block in snake_blocks[1:]:
if snake_blocks[0][0] == block[0] and snake_blocks[0][1] == block[1]:
return True
return False
def show_game_over():
"""显示游戏结束界面"""
font = pygame.font.SysFont('arial', 36)
text_surface = font.render('Game Over!', True, WHITE)
text_rect = text_surface.get_rect()
text_rect.center = (WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)
WINDOW_SURFACE.blit(text_surface, text_rect)
pygame.display.update()
def game():
"""游戏主循环"""
snake_blocks = INITIAL_SNAKE_POSITION.copy()
direction = INITIAL_DIRECTION
score = INITIAL_SCORE
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_UP and direction != DIRECTION_DOWN:
direction = DIRECTION_UP
elif event.key == pygame.K_DOWN and direction != DIRECTION_UP:
direction = DIRECTION_DOWN
elif event.key == pygame.K_LEFT and direction != DIRECTION_RIGHT:
direction = DIRECTION_LEFT
elif event.key == pygame.K_RIGHT and direction != DIRECTION_LEFT:
direction = DIRECTION_RIGHT
WINDOW_SURFACE.fill(BLACK)
draw_snake(snake_blocks)
draw_food(FOOD_POSITION)
if check_collision(snake_blocks, FOOD_POSITION):
FOOD_POSITION = generate_food_position()
score += 10
move_snake(snake_blocks, direction)
if check_game_over(snake_blocks):
show_game_over()
pygame.time.wait(2000)
return
font = pygame.font.SysFont('arial', 24)
score_surface = font.render('Score: %d' % score, True, WHITE)
score_rect = score_surface.get_rect()
score_rect.topleft = (10, 10)
WINDOW_SURFACE.blit(score_surface, score_rect)
pygame.display.update()
CLOCK.tick(SNAKE_SPEED)
if __name__ == '__main__':
game()
```
这个实现并不完美,但它可以让你了解如何使用Pygame来创建一个简单的游戏,并且它提供了一个良好的起点,可以用来构建更复杂的游戏。