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

作者:迢迢x 时间:2021-10-23 19:42:59 

导语:

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

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')

结尾:

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

来源:https://blog.csdn.net/a55656aq/article/details/122594192

标签:Python,恐龙跑跑,游戏
0
投稿

猜你喜欢

  • 图文教程教你asp编译成dll组件

    2010-07-16 13:16:00
  • 白鸦:内容设计,初始内容

    2008-03-04 16:23:00
  • File input 的样式和文字的更改方法__适用于Firefox、IE等浏览器

    2009-10-13 21:05:00
  • 精简化的产品设计

    2008-07-26 12:22:00
  • JavaScript中使用Async实现异步控制

    2023-08-23 00:55:54
  • [译]图片优化 第五章:AlphaImageLoader

    2010-08-29 18:39:00
  • 浅析PHP中的字符串编码转换(自动识别原编码)

    2023-09-08 08:04:44
  • DTS构建组件及其如何完成数据转换服务

    2009-01-20 15:37:00
  • 利用ASP发送和接收XML数据的处理方法

    2009-02-02 08:57:00
  • JS代码混淆加密工具

    2008-05-25 13:49:00
  • 基于PHP实现解密或加密Cloudflar邮箱保护

    2023-07-22 13:22:37
  • 纯CSS去除按钮以及链接点击时虚线

    2009-05-21 10:40:00
  • Go语言中的闭包详解

    2023-06-30 05:27:14
  • innerHTML在Mozilla Firefox和Opera下执行的一个特例情况。

    2023-09-16 11:23:46
  • Python之日期与时间处理模块(date和datetime)

    2023-09-29 12:53:06
  • 2010 图标设计趋势

    2010-12-17 12:44:00
  • python opencv根据颜色进行目标检测的方法示例

    2021-09-29 03:53:41
  • 某一公司的ASP面试题

    2011-09-15 20:50:20
  • JS实现canvas简单小画板功能

    2023-08-21 07:40:49
  • 讲解数据库加密技术的功能特性与实现方法

    2008-12-18 14:24:00
  • asp之家 网络编程 m.aspxhome.com