前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >chrome 浏览器小恐龙彩蛋游戏

chrome 浏览器小恐龙彩蛋游戏

作者头像
叶子陪你玩
发布2021-07-05 18:11:29
1.5K0
发布2021-07-05 18:11:29
举报
文章被收录于专栏:叶子陪你玩编程

相信很多人都玩过chrome 浏览器小恐龙彩蛋游戏,没玩过的可以断网或者直接输入 chrome://dino试试。

自己搞一个试试,暂时做了一个简单的效果。

这里讲讲一般做小游戏的思路:

1.自底向上,线性增加,从最简单的部分开始,不断增加新的功能。

之前写过重构13岁小孩游戏的文章,原作者的游戏就是这样做出来的。

重构13岁小孩写的游戏(上)

重构13岁小孩写的游戏(下)

对于初学者这种方法非常合适,我第一次教别人也是这样的思路。

这种方法优点是前后逻辑非常强,层层递进,比较容易理解,缺点是从头再看代码觉得非常乱,增加新功能比较麻烦。

2.使用函数拆分功能

方法还是和前面的一样,只不过很多代码封装成函数了,优点是代码拆分成一块一块,比较容易理解,缺点是函数之间耦合较多(涉及到变量的修改),变量的作用域需要考虑好,比较麻烦。

3.使用类的方法来重构代码,优点是比较容易管理代码,并且增加游戏的功能也非常方便,即容易扩展,缺点可能就是初学者可能不喜欢类的方式组织代码。

这里放上原始的纯面向过程的代码,代码比较乱,感兴趣的可以自己修改。

代码语言:javascript
复制
# 导入库
import pygame
import sys

# 初始化
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)
pygame.mixer.music.load("funkyrobot.mp3")
pygame.mixer.music.play(-1, 0)
sound = pygame.mixer.Sound('pop.mp3')
# 加载正常恐龙
dino_list = []
for i in range(1, 7):
    temp = pygame.image.load(f"dino/dino_run{i}.png")
    dino_rect = temp.get_rect()
    dino_list.append(temp)
index = 0
print(dino_rect)
# 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 True:
    # 每秒30次
    clock.tick(30)

    # 事件侦测
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        # 空格键侦测
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and dino_rect.y==150:
                y_speed = jumpSpeed
                sound.play()

    # 恐龙图片索引加 1
    index +=1
    # 地面移动
    ground_move_distance += ground_speed
    if ground_move_distance>=900:
        ground_move_distance = 0

    # 跳跃
    dino_rect.y += y_speed # 更新y坐标
    y_speed += gravity  # 重力作用下改变速度
    if dino_rect.y>=150:
        dino_rect.y = 150

    # 仙人掌移动
    cactus_rect.x = 900-ground_move_distance

    # 碰撞检测
    if dino_rect.colliderect(cactus_rect):
        while not is_restart:
            # 事件侦测
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                # 空格键侦测
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        is_restart = True
            # 设置重新再来图片
            screen.blit(restart, restart_rect)
            screen.blit(gameover, gameover_rect)
            pygame.display.update()

        score = 0
        is_restart = False
        ground_move_distance = 0
        # 恐龙 x,y 初始值
        dino_rect.x, dino_rect.y = 100, 150
        # 仙人掌 x,y 初始值
        cactus_rect.x, cactus_rect.y = 900, 140

    # 统计距离
    score += ground_speed
    score_surface = my_font.render("distance:"+str(score), True, text_color)

    # 更换背景色
    if score>2000:
        bg_color = (55,55,55)
        ground_speed = 15
        text_color = (255,255, 255)

    # 设置背景色
    screen.fill(bg_color)
    # 设置地面图片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))

    pygame.display.update()

需要素材的可以留言。

(全文完)


欢迎关注公众微信号:叶子陪你玩编程

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 叶子陪你玩编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档