python贪吃蛇编程代码

当编写贪吃蛇游戏的 Python 代码时,你可以使用 Pygame 等库来实现图形界面和游戏逻辑。

python
import pygame import time import random pygame.init() # 定义颜色 white = (255, 255, 255) yellow = (255, 255, 102) black = (0, 0, 0) red = (213, 50, 80) green = (0, 255, 0) blue = (50, 153, 213) # 设置屏幕大小 dis_width = 600 dis_height = 400 dis = pygame.display.set_mode((dis_width, dis_height)) pygame.display.set_caption('贪吃蛇游戏') # 设置游戏参数 snake_block = 10 snake_speed = 15 # 定义字体 font_style = pygame.font.SysFont(None, 50) # 定义蛇 snake = [[dis_width // 2, dis_height // 2]] snake_direction = 'RIGHT' change_to = snake_direction # 定义食物位置 foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 # 定义分数 score = 0 # 定义函数 def our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(dis, white, [x[0], x[1], snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True, color) dis.blit(mesg, [dis_width / 6, dis_height / 3]) # 游戏主循环 game_over = False while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and not snake_direction == 'DOWN': change_to = 'UP' elif event.key == pygame.K_DOWN and not snake_direction == 'UP': change_to = 'DOWN' elif event.key == pygame.K_LEFT and not snake_direction == 'RIGHT': change_to = 'LEFT' elif event.key == pygame.K_RIGHT and not snake_direction == 'LEFT': change_to = 'RIGHT' # 更新蛇的方向 if change_to == 'UP' and not snake_direction == 'DOWN': snake_direction = 'UP' elif change_to == 'DOWN' and not snake_direction == 'UP': snake_direction = 'DOWN' elif change_to == 'LEFT' and not snake_direction == 'RIGHT': snake_direction = 'LEFT' elif change_to == 'RIGHT' and not snake_direction == 'LEFT': snake_direction = 'RIGHT' # 移动蛇的头部 if snake_direction == 'UP': snake[0][1] -= snake_block elif snake_direction == 'DOWN': snake[0][1] += snake_block elif snake_direction == 'LEFT': snake[0][0] -= snake_block elif snake_direction == 'RIGHT': snake[0][0] += snake_block # 判断是否吃到食物 if snake[0][0] == foodx and snake[0][1] == foody: foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0 score += 1 else: snake.pop() # 判断是否碰到边界或自身 if ( snake[0][0] < 0 or snake[0][0] >= dis_width or snake[0][1] < 0 or snake[0][1] >= dis_height ): game_over = True for block in snake[1:]: if block == snake[0]: game_over = True # 更新屏幕 dis.fill(black) pygame.draw.rect(dis, blue, [foodx, foody, snake_block, snake_block]) our_snake(snake_block, snake) # 显示分数 score_font = pygame.font.SysFont(None, 35) value = score_font.render("Score: " + str(score), True, white) dis.blit(value, [0, 0]) pygame.display.update() # 控制游戏速度 pygame.time.Clock().tick(snake_speed) # 游戏结束时显示消息 message("你输了!", red) pygame.display.update() time.sleep(2) pygame.quit() quit()

请确保你已安装 Pygame 库,你可以使用

bash
pip install pygame

此代码实现了一个简单的贪吃蛇游戏,你可以根据需要进行修改和扩展。

python
# 在之前的代码后面添加

希望这个代码对你有帮助,如果有任何问题,请随时问我!