python代码编写贪吃蛇小游戏
时间: 2023-08-31 13:05:21 浏览: 107
100行Python代码的贪吃蛇小游戏源码.zip
5星 · 资源好评率100%
好的,以下是一个基于Python的贪吃蛇小游戏的代码示例:
```python
import pygame
import random
# 设置游戏界面尺寸
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 480
# 贪吃蛇身体大小
BLOCK_SIZE = 20
# 初始化pygame模块
pygame.init()
# 创建游戏界面对象
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置游戏标题
pygame.display.set_caption('贪吃蛇')
# 定义颜色对象
color_red = pygame.Color(255, 0, 0)
color_green = pygame.Color(0, 255, 0)
color_blue = pygame.Color(0, 0, 255)
color_white = pygame.Color(255, 255, 255)
color_black = pygame.Color(0, 0, 0)
# 定义游戏时钟对象
clock = pygame.time.Clock()
# 定义贪吃蛇初始位置和长度
snake_position = [100, 100]
snake_body = [[100, 100], [80, 100], [60, 100]]
# 定义食物初始位置
food_position = [random.randrange(1, (SCREEN_WIDTH//BLOCK_SIZE))*BLOCK_SIZE,
random.randrange(1, (SCREEN_HEIGHT//BLOCK_SIZE))*BLOCK_SIZE]
# 定义初始方向
direction = 'RIGHT'
change_direction = direction
# 画出贪吃蛇和食物
def draw_score(score):
score_font = pygame.font.SysFont(None, 25)
score_surface = score_font.render('Score: {0}'.format(score), True, color_white)
screen.blit(score_surface, (10, 10))
def draw_snake(snake_body):
for position in snake_body:
pygame.draw.rect(screen, color_green, pygame.Rect(position[0], position[1], BLOCK_SIZE, BLOCK_SIZE))
def draw_food(food_position):
pygame.draw.rect(screen, color_red, pygame.Rect(food_position[0], food_position[1], BLOCK_SIZE, BLOCK_SIZE))
# 游戏主循环
score = 0
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
change_direction = 'RIGHT'
elif event.key == pygame.K_LEFT:
change_direction = 'LEFT'
elif event.key == pygame.K_UP:
change_direction = 'UP'
elif event.key == pygame.K_DOWN:
change_direction = 'DOWN'
elif event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# 判断是否更改方向
if change_direction == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
elif change_direction == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
elif change_direction == 'UP' and direction != 'DOWN':
direction = 'UP'
elif change_direction == 'DOWN' and direction != 'UP':
direction = 'DOWN'
# 移动贪吃蛇的身体
if direction == 'RIGHT':
snake_position[0] += BLOCK_SIZE
elif direction == 'LEFT':
snake_position[0] -= BLOCK_SIZE
elif direction == 'UP':
snake_position[1] -= BLOCK_SIZE
elif direction == 'DOWN':
snake_position[1] += BLOCK_SIZE
# 判断贪吃蛇是否吃到食物
if snake_position == food_position:
food_position = [random.randrange(1, (SCREEN_WIDTH//BLOCK_SIZE))*BLOCK_SIZE,
random.randrange(1, (SCREEN_HEIGHT//BLOCK_SIZE))*BLOCK_SIZE]
score += 10
else:
snake_body.pop()
# 更新贪吃蛇身体的位置
snake_body.insert(0, list(snake_position))
# 绘制游戏界面
screen.fill(color_black)
draw_score(score)
draw_snake(snake_body)
draw_food(food_position)
pygame.display.update()
# 判断游戏是否结束
if snake_position[0] < 0 or snake_position[0] > SCREEN_WIDTH-BLOCK_SIZE or \
snake_position[1] < 0 or snake_position[1] > SCREEN_HEIGHT-BLOCK_SIZE:
pygame.quit()
exit(0)
for block in snake_body[1:]:
if snake_position == block:
pygame.quit()
exit(0)
# 设置游戏帧数并更新
clock.tick(15)
```
希望能帮到您! 如需更详细的说明,请与我交流。
阅读全文