置信很多人都玩过 chrome 浏览器上提供的恐龙跑跑游戏,在咱们断网或者间接在浏览器输出地址“chrome://dino/”都能够进入游戏

明天咱们就是用 Python 来制作一个相似的小游戏

素材筹备

首先咱们筹备下游戏所需的素材,比方恐龙图片,仙人掌图片,天空,高空等等,咱们对立放到 dino 文件夹下

游戏逻辑

咱们应用 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)

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

# 加载失常恐龙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轴上的初速度为0y_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# 加载 gameovergameover = 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 = 10ground_move_distance = 0# 时钟clock = pygame.time.Clock()# 从新再来一次is_restart = Falsetext_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技术。
对文章有何见解,或者有何技术问题,欢送在评论区一起留言探讨!