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

爱谁谁
发布: 2025-05-18 09:16:15
原创
607人浏览过

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源码

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

简篇AI排版
简篇AI排版

AI排版工具,上传图文素材,秒出专业效果!

简篇AI排版554
查看详情 简篇AI排版
let btn_font = new pygame.font.Font('arial.ttf', 24); // 指定字体
登录后复制

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

以上就是贪吃蛇怀旧版Windows版exe程序-python源码的详细内容,更多请关注php中文网其它相关文章!

相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号