用python生成一个贪吃蛇大作战的代码
时间: 2023-05-24 17:03:00 浏览: 126
A:
```python
import pygame
import random
# 定义colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 设置屏幕
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 480
BLOCK_SIZE = 20
SCREEN = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
# 设置初始化的贪吃蛇
INITIAL_SNAKE_LENGTH = 3
INITIAL_SNAKE_HEAD_X = SCREEN_WIDTH / 2
INITIAL_SNAKE_HEAD_Y = SCREEN_HEIGHT / 2
INITIAL_SNAKE = [[INITIAL_SNAKE_HEAD_X, INITIAL_SNAKE_HEAD_Y],\
[INITIAL_SNAKE_HEAD_X - BLOCK_SIZE, INITIAL_SNAKE_HEAD_Y],\
[INITIAL_SNAKE_HEAD_X - BLOCK_SIZE * 2, INITIAL_SNAKE_HEAD_Y]]
# 设置食物的范围
FOOD_MIN_X = BLOCK_SIZE
FOOD_MIN_Y = BLOCK_SIZE
FOOD_MAX_X = SCREEN_WIDTH - BLOCK_SIZE * 2
FOOD_MAX_Y = SCREEN_HEIGHT - BLOCK_SIZE * 2
# 设置移动的方向
LEFT = 'LEFT'
RIGHT = 'RIGHT'
UP = 'UP'
DOWN = 'DOWN'
# 初始化分数
SCORE = 0
# 绘制方块
def draw_block(color, position):
block = pygame.Rect((position[0], position[1]), (BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(SCREEN, color, block)
# 控制贪吃蛇的移动
def move_snake(direction, snake):
if direction == RIGHT:
new_position = [snake[0][0] + BLOCK_SIZE, snake[0][1]]
elif direction == LEFT:
new_position = [snake[0][0] - BLOCK_SIZE, snake[0][1]]
elif direction == UP:
new_position = [snake[0][0], snake[0][1] - BLOCK_SIZE]
elif direction == DOWN:
new_position = [snake[0][0], snake[0][1] + BLOCK_SIZE]
snake.insert(0, new_position)
snake.pop()
# 判断是否吃到了食物
def check_food(snake, food_position):
if snake[0][0] == food_position[0] and snake[0][1] == food_position[1]:
snake.append([0, 0])
return True
return False
# 生成食物的位置
def generate_food():
food_x = random.randint(FOOD_MIN_X, FOOD_MAX_X)
food_y = random.randint(FOOD_MIN_Y, FOOD_MAX_Y)
return [food_x // BLOCK_SIZE * BLOCK_SIZE, food_y // BLOCK_SIZE * BLOCK_SIZE]
# 检查是否碰到了墙或者自己
def check_collision(snake):
if snake[0][0] < 0 or snake[0][0] >= SCREEN_WIDTH or snake[0][1] < 0 or snake[0][1] >= SCREEN_HEIGHT:
return True
for block in snake[1:]:
if snake[0][0] == block[0] and snake[0][1] == block[1]:
return True
return False
# 初始化pygame
pygame.init()
# 设置标题
pygame.display.set_caption("贪吃蛇大作战")
# 初始化时钟
clock = pygame.time.Clock()
# 绘制贪吃蛇
def draw_snake(snake):
for block in snake:
draw_block(GREEN, block)
# 更新分数
def update_score(score):
font = pygame.font.SysFont(None, 30)
text = font.render("得分:{}".format(score), True, WHITE)
SCREEN.blit(text, [SCREEN_WIDTH - 100, 10])
# 主函数
def main():
# 初始化贪吃蛇
snake = INITIAL_SNAKE
# 初始化食物位置
food_position = generate_food()
# 初始移动方向
direction = RIGHT
# 游戏循环
running = True
while running:
# 事件循环
for event in pygame.event.get():
# 点击窗口"X"退出游戏
if event.type == pygame.QUIT:
running = False
# 判断按下的方向键
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and direction != RIGHT:
direction = LEFT
elif event.key == pygame.K_RIGHT and direction != LEFT:
direction = RIGHT
elif event.key == pygame.K_UP and direction != DOWN:
direction = UP
elif event.key == pygame.K_DOWN and direction != UP:
direction = DOWN
# 绘制背景
SCREEN.fill(BLACK)
# 生成食物
draw_block(RED, food_position)
# 绘制贪吃蛇
draw_snake(snake)
# 根据方向移动贪吃蛇
move_snake(direction, snake)
# 检查是否吃到食物
if check_food(snake, food_position):
# 更新食物位置
food_position = generate_food()
# 更新分数
global SCORE
SCORE += 10
# 检查是否碰到墙或者自己
if check_collision(snake):
running = False
# 更新分数
update_score(SCORE)
# 更新屏幕
pygame.display.update()
# 控制帧率
clock.tick(10)
# 退出游戏
pygame.quit()
if __name__ == '__main__':
main()
```
阅读全文