Python 恐龙跑跑小游戏实现流程


Posted in Python onFebruary 15, 2022

导语:

谷歌浏览器中有个很有名的彩蛋:当你网络出现问题时,就会出现一个“小恐龙游戏”。?

Python 恐龙跑跑小游戏实现流程

相信很多人都玩过 chrome 上提供的恐龙跑跑游戏,在我们断网或者直接在浏览器输入地址“chrome://dino/”都可以进入游戏。

Python 恐龙跑跑小游戏实现流程

今天我们就来给大家演示下,用Python来自己做一个仿制的“小恐龙游戏”!?

废话不多说,让我们愉快地开始吧~先给你们看一下运你效果???

Python 恐龙跑跑小游戏实现流程

正文:

开发工具:

Python版本:3.6.4

相关模块:

pygame模块;以及一些python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

在终端运行如下命令即可:

python Game7.py

素材准备

首先我们准备下游戏所需的素材,比如恐龙图片,仙人掌图片,天空,地面等等,我们统一放到 dino 文件夹下

Python 恐龙跑跑小游戏实现流程

游戏逻辑

我们使用 Pygame 来制作游戏,先进行游戏页面的初始化

import pygame
 
# 初始化
pygame.init()
pygame.mixer.init()
# 设置窗口大小
screen = pygame.display.set_mode((900, 200))
# 设置标题
pygame.display.set_caption("恐龙跳跳")
# 使用系统自带的字体
my_font = pygame.font.SysFont("arial", 20)
score = 0
# 背景色
bg_color = (218,220,225)

接下来,我们来考虑一下,游戏中有哪些游戏元素:

小恐龙:由玩家控制以躲避路上的障碍物;

路面:游戏的背景;

云:游戏的背景;

仙人掌:路上的障碍物之一,小恐龙碰上就会死掉;

记分板:记录当前的分数和历史最高分。

接下来我们将各种素材加载进内存

Python 恐龙跑跑小游戏实现流程

Python 恐龙跑跑小游戏实现流程

Python 恐龙跑跑小游戏实现流程

# 加载正常恐龙
dino_list = []
temp = ""
for i in range(1, 7):
    temp = pygame.image.load(f"dino/dino_run{i}.png")
    dino_list.append(temp)
dino_rect = temp.get_rect()
index = 0
 
# x 初始值
dino_rect.x = 100
# y 初始值
dino_rect.y = 150
# print(dino_rect)
 
# 设置y轴上的初速度为0
y_speed = 0
# 起跳初速度
jumpSpeed = -20
# 模拟重力
gravity = 2
 
 加载地面
ground = pygame.image.load("dino/ground.png")
 
# 加载仙人掌
cactus = pygame.image.load("dino/cactus1.png")
cactus_rect = cactus.get_rect()
cactus_rect.x,cactus_rect.y = 900,140
 
# 加载重新再来
restart = pygame.image.load("dino/restart.png")
restart_rect = restart.get_rect()
restart_rect.x,restart_rect.y = (900-restart.get_rect().width)/2,(200-restart.get_rect().height)/2+50
# 加载 gameover
gameover = pygame.image.load("dino/gameover.png")
gameover_rect = gameover.get_rect()
gameover_rect.x, gameover_rect.y = (
    900-gameover.get_rect().width)/2, (200-gameover.get_rect().height)/2
# 地面移动速度与距离
ground_speed = 10
ground_move_distance = 0
 
# 时钟
clock = pygame.time.Clock()
 
# 重新再来一次
is_restart = False
text_color = (0,0,0)

再接下来,我们通过一个 while 死循环来保持游戏进程

while True:
    # 每秒30次
    clock.tick(30)
    ...

在上面的循环当中,我们需要两个检测机制,事件检测和碰撞检测

事件检测

# 事件侦测
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            if result_flag:
                with open("result.ini", "w+") as f:
                    f.write(str(best))
            sys.exit()
        # 空格键侦测
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and dino_rect.y==150:
                y_speed = jumpSpeed

主要检测退出事件和空格键事件

碰撞检测

# 碰撞检测
    if dino_rect.colliderect(cactus_rect):
        while not is_restart:
            # 事件侦测
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    if result_flag:
                        with open("result.ini", "w+") as f:
                            f.write(str(best))
                    sys.exit()
                # 空格键侦测
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        is_restart = True
                        bg_color = (218,220,225)
                        ground_speed = 10
            # 设置重新再来图片
            screen.blit(restart, restart_rect)
            screen.blit(gameover, gameover_rect)
            pygame.display.update()

对于碰撞,只要恐龙碰撞到了仙人掌,那么游戏结束,展示重新再来图片

由于我们希望游戏可以记录我们的最好成绩,所以这里使用了本地文件存储游戏记录的方式,当游戏结束的时候,根据当前游戏成绩来判断是否将新的成绩写入文件当中

下面是计算跑动距离和最好成绩的代码

# 统计距离
    score += ground_speed
    score_surface = my_font.render("Distance: "+str(score), True, text_color)
 
    # 计算最好成绩
    result_flag = False
    if score >= best:
        best = score
        result_flag = True
    best_result = my_font.render("Best Result: " + str(best), True, text_color)

我们还需要给不同距离增加不同的游戏难度,毕竟跑起来,肯定距离越远,难度越大嘛

# 更换背景色,成绩大于4000
    if score > 4000:
        bg_color = (55,55,55)
        ground_speed = 15
        text_color = (255,255, 255)
# 更换背景色,成绩大于8000
    if score > 8000:
        bg_color = (220,20,60)
        ground_speed = 20
        text_color = (255, 255, 255)
 
    # 更换背景色,成绩大于12000
    if score > 12000:
        bg_color = (25,25,112)
        ground_speed = 25
        text_color = (255, 255, 255)
 
    # 设置背景色
    screen.fill(bg_color)

最后我们将所有加载到内存当中的元素都呈现在 screen 上

# 设置地面图片1
    screen.blit(ground, (0-ground_move_distance, 180))
    # 设置地面图片2,在右边边界外
    screen.blit(ground, (900-ground_move_distance, 180))
    # 设置恐龙图片
    screen.blit(dino_list[index % 6], dino_rect)
    # 设置仙人掌图片
    screen.blit(cactus, cactus_rect)
    # 设置分数
    screen.blit(score_surface,(780,20))
    # 设置最好成绩
    screen.blit(best_result, (20, 20))
 
    pygame.display.update()

为了增加游戏性,我们再增加背景音乐和跳跃音效

pygame.mixer.music.load("background.mp3")
pygame.mixer.music.play(-1, 0)
sound = pygame.mixer.Sound('preview.mp3')

结尾:

这样,一个简单易用的恐龙跑跑游戏就完成了,今天的分享就到这里,喜欢就点个赞吧!

到此这篇关于Python 恐龙跑跑小游戏实现流程的文章就介绍到这了,更多相关Python 恐龙跑跑内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python脚本实现网卡流量监控
Feb 14 Python
python读写二进制文件的方法
May 09 Python
python中zip和unzip数据的方法
May 27 Python
华为校园招聘上机笔试题 扑克牌大小(python)
Apr 22 Python
Python基本语法之运算符功能与用法详解
Oct 22 Python
flask框架配置mysql数据库操作详解
Nov 29 Python
基于Python检测动态物体颜色过程解析
Dec 04 Python
python实现低通滤波器代码
Feb 26 Python
用 Python 制作地球仪的方法
Apr 24 Python
python 利用matplotlib在3D空间中绘制平面的案例
Feb 06 Python
Python基础之元组与文件知识总结
May 19 Python
详解PyTorch模型保存与加载
Apr 28 Python
详解Python+OpenCV进行基础的图像操作
Appium中scroll和drag_and_drop根据元素位置滑动
Feb 15 #Python
python 远程执行命令的详细代码
Feb 15 #Python
python 详解turtle画爱心代码
python分分钟绘制精美地图海报
基于PyQT5制作一个桌面摸鱼工具
Feb 15 #Python
python接口测试返回数据为字典取值方式
Feb 12 #Python
You might like
MySQL授权问题总结
2007/05/06 PHP
php正则表达式获取内容所有链接
2015/07/24 PHP
Discuz!X中SESSION机制实例详解
2015/09/23 PHP
php常用图片处理类
2016/03/16 PHP
Laravel如何同时连接多个数据库详解
2019/08/13 PHP
漂亮的仿flash菜单,来自蓝色经典
2006/06/26 Javascript
javascript同步Import,同步调用外部js的方法
2008/07/08 Javascript
javascript 打开页面window.location和window.open的区别
2010/03/17 Javascript
点弹代码 点击页面任何位置都可以弹出页面效果代码
2012/09/17 Javascript
根据经纬度计算地球上两点之间的距离js实现代码
2013/03/05 Javascript
JavaScript在浏览器标题栏上显示当前日期和时间的方法
2015/03/19 Javascript
async/await与promise(nodejs中的异步操作问题)
2017/03/03 NodeJs
Angular.JS内置服务$http对数据库的增删改使用教程
2017/05/07 Javascript
Node.js进阶之核心模块https入门
2018/05/23 Javascript
vue-router+nginx 非根路径配置方法
2018/06/30 Javascript
Vue中使用create-keyframe-animation与动画钩子完成复杂动画
2019/04/09 Javascript
微信小程序云开发获取文件夹下所有文件(推荐)
2019/11/14 Javascript
解决Vue router-link绑定事件不生效的问题
2020/07/22 Javascript
Python实现获取网站PR及百度权重
2015/01/21 Python
python中的break、continue、exit()、pass全面解析
2017/08/05 Python
有关pycharm登录github时有的时候会报错connection reset的问题
2020/09/15 Python
Python使用eval函数执行动态标表达式过程详解
2020/10/17 Python
详解Django中的FBV和CBV对比分析
2021/03/01 Python
基于Html5实现的react拖拽排序组件示例
2018/08/13 HTML / CSS
canvas实现有递增动画的环形进度条的实现方法
2019/07/10 HTML / CSS
WEB控件可以激发服务端事件,请谈谈服务端事件是怎么发生并解释其原理?自动传回是什么?为什么要使用自动传回?
2012/02/21 面试题
群众路线教育实践活动心得体会
2014/03/07 职场文书
座谈会主持词
2014/03/20 职场文书
移风易俗倡议书
2014/04/15 职场文书
校园文化标语
2014/06/18 职场文书
考试作弊检讨书1000字(5篇)
2014/10/19 职场文书
2014年基层党支部工作总结
2014/12/04 职场文书
宿舍卫生管理制度
2015/08/05 职场文书
如何理解及使用Python闭包
2021/06/01 Python
Python实现天气查询软件
2021/06/07 Python
部分武汉产收音机展览
2022/04/07 无线电