制作一款打飞机游戏教程5:添加子弹

在上一篇中,我们设计了一个原型来测试飞船的移动和射击感觉。

我们注意到飞船在左右转向时的动画响应不一致。通过调整动画的中心值和范围,我们解决了这个问题,使得飞船在左右转向时的动画响应更加对称和一致。

为了简化代码并提高可读性,我们使用了三元运算符来替换一些冗长的if-else语句。虽然这次优化节省的不多,但它使代码更加紧凑和易于维护。

接下来,我们开始实现射击功能。首先,我们设计了一个简单的子弹精灵,并编写了代码来控制子弹的发射和移动。

我们通过在按下射击键时创建一个新的子弹对象,并将其添加到子弹数组中来实现子弹的发射。然后,在更新函数中遍历子弹数组,更新每个子弹的位置。

为了避免子弹发射过快导致的视觉混乱,我们实现了一个射击频率控制机制。通过设置子弹的发射间隔,我们可以控制子弹的发射速度,使其看起来更加合理。

为了增强游戏的视觉效果和玩家反馈,我们计划添加一些特效,如枪口火焰和爆炸效果。

我们设计了一个简单的枪口火焰动画,并将其与射击动作同步播放。这样,当玩家按下射击键时,不仅能看到子弹射出,还能看到枪口冒出的火焰,增强了射击的真实感。

复制代码
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
-- t: make basic movement feel nice
--    ❎: fixed cobblestoning
--    ❎: what is a good movement speed
--    t: do banking

-- t: make shooting feel nice!

-- x: normalized diagonals

butarr={1,2,0,3,5,6,3,4,8,7,4,0,1,2,0}

dirx={-1,1, 0,0, -0.7, 0.7,0.7,-0.7}
diry={ 0,0,-1,1, -0.7,-0.7,0.7,0.7}

butarr[0]=0

shiparr={1,3,5,7,9}

shipspr=0

shots={}
shotwait=0

function _init()
 px,py=64,64
 spd=1.4
 lastdir=0
 
 cls(0)
end

function _draw()
 cls(12)
  
  
  
 spr(shiparr[flr(shipspr*2.4+3.5)],px,py,2,2)
 --pset(px,py,8)
 
 for s in all(shots) do
  spr(11,s.x,s.y)
 end
 
 local btnv=btn()&0b1111
 print(btn(),5,5,7)
 print(btnv,5,11,7)
 print(butarr[btnv],5,17,7)
 print(shotwait,5,23,7)
end

function _update60()
 local dir=butarr[btn()&0b1111]
 
 if lastdir!=dir and dir>=5 then
  --anti-cobblestone
  px=flr(px)+0.5
  py=flr(py)+0.5
 end
 
 local dshipspr=0
 
 if dir>0 then
  px+=dirx[dir]*spd
  py+=diry[dir]*spd
  dshipspr=mysgn(dirx[dir])
 end
  
 shipspr+=mysgn(dshipspr-shipspr)*0.15
 shipspr=mid(-1,shipspr,1)
 
 lastdir=dir
 
 if shotwait>0 then
  shotwait-=1
 end
 
 if btn(❎) then
  if shotwait<=0 then
   add(shots,{
    x=px,
    y=py,
    sx=0,
    sy=-1
   })
   shotwait=7
  end
 end
 
 doshots()
end

function doshots()
 for s in all(shots) do
  s.x+=s.sx
  s.y+=s.sy
 end
end

function mysgn(v) 
 return v==0 and 0 or sgn(v)
end
__gfx__
00000000000000011000000000000001100000000000000110000000000000011000000000000001100000000990000000000000000000000000000000000000
00000000000000167100000000000016710000000000001661000000000000167100000000000016710000009779000000000000000000000000000000000000
00700700000000111d100000000000111d1000000000011111100000000001d111000000000001d1110000009779000000000000000000000000000000000000
00077000000001ccc1100000000001ccc1100000000001cccc1000000000011ccc1000000000011ccc1000009aa9000000000000000000000000000000000000
0007700000001c77cc11000000001c77cc11000000001c77ccc10000000011c77cc10000000011c77cc100009aa9000000000000000000000000000000000000
0070070000001c7c1c11000000001c7c1c11000000001c7cc1c10000000011c7c1c10000000011c7c1c100000990000000000000000000000000000000000000
0000000000001c111c11100000011c111c11100000111c1111c11100000111c111c11000000111c111c100000000000000000000000000000000000000000000
0000000000001c111c17100000011c111c17100000171c1111c17100000171c111c11000000171c111c100000000000000000000000000000000000000000000
0000000000011cc17116100000161cc1171610000016117117116100000161711cc16100000161171cc110000000000000000000000000000000000000000000
000000000001d1c711d6d1000016d1c77116d10001d6d117711d6d10001d61177c1d6100001d6d117c1d10000000000000000000000000000000000000000000
00000000001ddd111d6d7d1001dddd1111dd6710176d6d1111d6d6710176dd1111dddd1001d7d6d111ddd1000000000000000000000000000000000000000000
00000000001dd6ddd6d66d1001dd66dddd6d66101666d6dddd6d66610166d6dddd66dd1001d66d6ddd6dd1000000000000000000000000000000000000000000
00000000001d676676d6dd1001d6676676d6dd101dd6d676676d6dd101dd6d6766766d1001dd6d676676d1000000000000000000000000000000000000000000
00000000000176d167d1110000117dd1171111000111171661711110001111711dd7110000111d761d6710000000000000000000000000000000000000000000
000000000000151155d110000001655155d11000000161511516100000011d551556100000011d55115100000000000000000000000000000000000000000000
00000000000001111111000000001111111100000000111111110000000011111111000000001111111000000000000000000000000000000000000000000000
相关推荐
点金石游戏出海6 小时前
每周资讯 | Krafton斥资750亿日元收购日本动画公司ADK;《崩坏:星穹铁道》新版本首日登顶iOS畅销榜
游戏·ios·业界资讯·apple·崩坏星穹铁道
wsdchong之小马过河6 小时前
2025《烈焰之刃》游戏攻略
游戏
Alfred king20 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
向宇it1 天前
【unity游戏开发——网络】网络游戏通信方案——强联网游戏(Socket长连接)、 弱联网游戏(HTTP短连接)
网络·http·游戏·unity·c#·编辑器·游戏引擎
ii_best1 天前
按键精灵 安卓脚本开发:游戏实战之自动切换账号辅助工具
游戏
Alfred king11 天前
面试150跳跃游戏
python·leetcode·游戏·贪心算法
D1555408805811 天前
游戏护航小程序源码游戏派单小程序搭建游戏代练小程序源码
游戏
旧物有情12 天前
Unity2D 街机风太空射击游戏 学习记录 #12QFramework引入
学习·游戏
帅_shuai_12 天前
UE5 游戏模板 —— FirstShootGame
游戏·ue5
Jooolin12 天前
【Python】什么?Python 可以用来写 Galgame?
python·游戏·ai编程