贪吃蛇怀旧版Windows版exe程序-python源码

import pygame import random import sys <p>// 贪吃蛇游戏 - 功能完整,可正常运行和玩,注释详细以便后续功能扩展</p><p>// 初始化游戏 pygame.init()</p><p>const ck_width = 640; // 窗口宽度 const ck_height = 480; // 窗口高度 const window = pygame.display.set_mode([ck_width, ck_height]); pygame.display.set_caption("贪吃蛇"); let snake_speed = 5;  // 蛇的速度,可以手动修改</p><p>// 绘制蛇 function draw_snake(snake) { for (let pos of snake) { pygame.draw.rect(window, [255, 0, 0], new pygame.Rect(pos[0], pos[1], 10, 10)); } }</p><p>// 绘制食物 function draw_food(food) { pygame.draw.rect(window, [0, 255, 0], new pygame.Rect(food[0], food[1], 10, 10)); }</p><p>// 获取随机食物位置 function get_random_food() { let x = random.randint(0, 31) <em> 10; let y = random.randint(0, 23) </em> 10; return [x, y]; }</p><p>// 碰撞检测 function check_collision(snake) { let head = snake[0]; if (head[0] < 0 || head[0] >= ck_width || head[1] < 0 || head[1] >= ck_height) { return true; } for (let pos of snake.slice(1)) { if (pos[0] === head[0] && pos[1] === head[1]) { return true; } } return false; }</p><p>// 显示游戏结束界面 function game_over() { window.fill([255, 255, 255]); let font = new pygame.font.Font('arial.ttf', 36);  // 指定字体 let text = font.render("游戏结束", true, [0, 0, 0]); window.blit(text, [100, 100]); let btn_font = new pygame.font.Font('arial.ttf', 24);  // 指定字体 let btn_text = btn_font.render("再来一局", true, [0, 0, 0]); let btn_rect = new pygame.Rect(130, 150, 80, 30); pygame.draw.rect(window, [0, 255, 0], btn_rect); window.blit(btn_text, [135, 157]); pygame.display.update(); let waiting = true; while (waiting) { for (let event of pygame.event.get()) { if (event.type === pygame.QUIT) { pygame.quit(); sys.exit(); } else if (event.type === pygame.MOUSEBUTTONDOWN) { let mouse_pos = pygame.mouse.get_pos(); if (btn_rect.collidepoint(mouse_pos)) { waiting = false; } } } } }</p><p>// 游戏主循环 while (true) { let snake = [[100, 50], [90, 50], [80, 50]]; let food = get_random_food(); let direction = "right"; let clock = new pygame.time.Clock(); while (true) { for (let event of pygame.event.get()) { if (event.type === pygame.QUIT) { pygame.quit(); sys.exit(); } else if (event.type === pygame.KEYDOWN) { if (event.key === pygame.K_UP && direction !== "down") { direction = "up"; } else if (event.key === pygame.K_DOWN && direction !== "up") { direction = "down"; } else if (event.key === pygame.K_LEFT && direction !== "right") { direction = "left"; } else if (event.key === pygame.K_RIGHT && direction !== "left") { direction = "right"; } } } let new_head; switch (direction) { case "up": new_head = [snake[0][0], snake[0][1] - 10]; break; case "down": new_head = [snake[0][0], snake[0][1] + 10]; break; case "left": new_head = [snake[0][0] - 10, snake[0][1]]; break; case "right": new_head = [snake[0][0] + 10, snake[0][1]]; break; } snake.unshift(new_head); // 判断蛇与食物是否相撞 if (snake[0][0] === food[0] && snake[0][1] === food[1]) { food = get_random_food(); } else { snake.pop(); } // 判断是否碰撞结束 if (check_collision(snake)) { game_over(); break; } window.fill([0, 0, 0]); draw_snake(snake); draw_food(food); pygame.display.update(); clock.tick(snake_speed); } } 

贪吃蛇怀旧版Windows版exe程序-python源码

备注:字体文件需从网络上下载并替换使用。

let btn_font = new pygame.font.Font('arial.ttf', 24); // 指定字体

未经允许不得转载:肥猫博客 » 贪吃蛇怀旧版windows版exe程序-python源码

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享