从零一个月制作pygame2.5D射击打怪类型游戏

前言

写在开头:本篇仅限于分享我个人的游戏开发经历,我也只是一个普通的在校本科生,游戏完全是从0开始一边学一边写的,陆陆续续写了一个多月,可能会有一些代码并不是很规范或者算法并不是最优解,希望大家能够取其精华去其糟粕,当然欢迎大家提出疑问或者更优方案,我们一起学习讨论。

使用 Python 和 Pygame 开发《星露谷物语》 游戏_哔哩哔哩_bilibili

从这个博主的视频中学到很多,也用了一些他的代码,在此特别鸣谢!

展示视频链接

用pygame写了一个月的打怪小游戏。。。。。。。。--0.o (bilibili.com)

第一次写类似文章,可能对代码解释不是很清晰,比较适合有一点基础的人看------0.o

初次尝试

本来是想写一个简单的跑酷游戏的,类似于天天酷跑

最开始用了一些比较简单的语句,然后学了学pygame的基本结构,首先导入库

然后
pygame.display.set_mode((screen_w, screen_h)) 设置显示屏宽高 

pygame.display.set_caption(game_name)   设置游戏名字

设置好后基本游戏事件循环

while True: 
   for event in pygame.event.get():

     if event.type == pygame.QUIT:

         pygame.quit()

         sys.exit()

对游戏事件开始遍历,创建Surface对象(set_mode也算Surface),用Fill(‘black’)把Surface底色调为黑色,用blit()函数把以指定大小,位置把背景放入Surface中,用time函数设置循环刷新帧数 初始我先设置为60帧

clock = pygame.time.Clock()
clock.tick(fps)

导入背景图片,网上先随便找了一张,导入人物图片,同样,两者叠放,初始化完参数大概是这样

然后用 update 和 flip 进行局部刷新和全局刷新

基本结构打好后添加玩法,添加角色移动

elif event.type == pygame.KEYDOWN:

    if event.key == pygame.K_a

用keydown 和 keytype 判断键入种类,把bilt()函数中的位置参数用x , y 接入,当d键入时向右增加x的参数 x += 1 ,同理给出上下左右四个方向 但由于横板2D先给出左右,优化参数,防止角色移动过快x+=0.01,分析出当不设置时间增量时键入一值会以最大循环帧执行,出现问题角色向左移动时x+=不会在右移时消失,那么

elif event.type == pygame.KEYUP: 

    speed_x = 0

判断键是否被抬起,把x+= 的值设置为speed 当抬起时, speed = 0

if event.key == pygame.K_w 

用w键设置跳跃,但跳跃运行形态与移动完全不一样,那么,设置重力,判断地面,当 y>630 时无法向下移动,给出一个恒定向下的速度,当跳跃时每次循环减少y坐标,但是出现问题,循环过快,人物在执行中卡在下边界,那么

if -5 <= x + speed_x <= 1400:

    x += speed_x

if 0 < y + speed_y <= 630:

    y += speed_y

边界减去人物减去移动距离 以此设为条件,执行后人物正常跳起降落,但是变成了僵硬的来回运动这效果太差了(O.0),于是设置重力加速度gravity 给定速度突变,

if 630 > y > 0:

    speed_y += gravity

重力加速度减少速度,速度移动单独拿出运行,人物能够正常运动了,设置左右移动运动范围,同样正常运动了,设置完后出现问题,人物在空中仍然可以跳跃,这应该是不被允许的,那么

设置if event.key == pygame.K_w and y >= 630 

设置为空中不可跳跃

基本运动设置完后还要更新图片,于是设置人物状态

pygame.init()

image1 = pygame.image.load("Scripts/pic/gamebg.png")

background = pygame.transform.scale(image1, (1500, 800))



image2 = pygame.image.load("Scripts/pic/gamehero.png")

hero_stand = pygame.transform.scale(image2, (112, 152))

hero_standleft = pygame.transform.flip(image2, True, False)



image3 = pygame.image.load("Scripts/pic/gamehero1.png")

hero_squat = pygame.transform.scale(image3, (133, 102))

hero_squatleft = pygame.transform.flip(image3, True, False)



image4 = pygame.image.load("Scripts/pic/gamehero2.png")

hero_jump = pygame.transform.scale(image4, (126, 135))

hero_jumpleft = pygame.transform.flip(image4, True, False)



image5 = pygame.image.load("Scripts/pic/spell1.jpg")

spell_image = pygame.transform.scale(image5, (180, 69))

导入几张图片

设置current_hero = hero_stand

人物初始为站立向右状态·

当向左移动时使用
hero_stand = pygame.transform.scale(image2, (112, 152))

导入人物水平镜像翻转实现人物转向,同时应用于跳跃,但是一大堆问题来了(O.0)

人物起跳无法转向

落地无法复原

转向后无法以转向起跳

每个动作都衔接下一个失败

加入下蹲动作,初衷是下蹲0.5秒后自动站起,但是无法站起

空中跳跃无法下蹲,检测地面才能

那么综合起来

elif event.type == pygame.KEYDOWN:

    if event.key == pygame.K_w and y >= 630 and current_hero != hero_squat and current_hero != hero_squatleft:

        if current_hero == hero_stand:

            current_hero = hero_jump

        if current_hero == hero_standleft:

            current_hero = hero_jumpleft

        speed_y = -jump_power

    if event.key == pygame.K_s and y == 630:

        if current_hero == hero_stand:

            current_hero = hero_squat

            start_squat = time.time()

            is_squat = True

        if current_hero == hero_standleft:

            current_hero = hero_squatleft

            start_squat = time.time()

    if event.key == pygame.K_a and x > 0:

        if current_hero == hero_stand:

            current_hero = hero_standleft

        if current_hero == hero_jump:

            current_hero = hero_jumpleft

        if current_hero == hero_squat:

            current_hero = hero_squatleft

        speed_x = -move_speed

    if event.key == pygame.K_d and x < 1300:

        if current_hero == hero_standleft:

            current_hero = hero_stand

        if current_hero == hero_jumpleft:

            current_hero = hero_jump

        if current_hero == hero_squatleft:

            current_hero = hero_squat

        speed_x = move_speed

elif event.type == pygame.KEYUP:

    speed_x = 0

给每个状态转换做了一个衔接,此外由于下蹲的存在设置了一个计时器

Pygame.time.get_tick()获取当前时间,

e
  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TTTTTytoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值