import pygameimport randomimport sys// 贪吃蛇游戏 - 功能完整,可正常运行和玩,注释详细以便后续功能扩展
// 初始化游戏pygame.init()
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; // 蛇的速度,可以手动修改
// 绘制蛇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));}}
// 绘制食物function draw_food(food) {pygame.draw.rect(window, [0, 255, 0], new pygame.Rect(food[0], food[1], 10, 10));}
// 获取随机食物位置function get_random_food() {let x = random.randint(0, 31) 10;let y = random.randint(0, 23) 10;return [x, y];}
// 碰撞检测function check_collision(snake) {let head = snake[0];if (head[0] = ck_width || 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;}
// 显示游戏结束界面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;}}}}}
// 游戏主循环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);}}

备注:字体文件需从网络上下载并替换使用。
Designs.ai
AI设计工具
48 查看详情
let btn_font = new pygame.font.Font('arial.ttf', 24); // 指定字体
未经允许不得转载:肥猫博客 » 贪吃蛇怀旧版Windows版exe程序-python源码
以上就是贪吃蛇怀旧版Windows版exe程序-python源码的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/821618.html
微信扫一扫
支付宝扫一扫