Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(1 edit)

Sorry for the late reply, just saw this now! Here's some commented code for Ninja Punch Zone. It's been long enough that I had to go through it and figure out what every line did again, and while doing that I found a couple of things I think I can optimize further. So thanks for that! :D

EDIT: Itch's comment system mangles the formatting so I've put an easier-to-read version up here.

--INITIALIZING VARIABLES--
--the number '30' came up a lot in the math stuff bleow,
--so i assigned it to a variable to save characters
t=30
--the player's x position
x=t
--the player's FIST position, relative to the player
--(it's always four pixels away from the player in the direction they're facing,
--but storing the offset in a variable saves chars when drawing later on)
f=4
--making aliases for some commonly used functions to save chars later on
z=rectfill
y=rnd
a=abs
--cooldown timer for the player's punch
c=0
--x position of red ninjas coming in from the right/left respectively
--every time we do this we randomize their position slightly,
--so the ninjas always come at the player at different times
r=130+y(30)
l=y(30)-82
--the player's score and the high score
s=0
h=0
--changing to low res mode (64x64 resolution)
poke(0x5f2c,3)
--the UPDATE LOOP--
function _update60()
 --if the player presses left or right then move them in that direction
 if btn(⬅️)
  then x=max(x-1,3)
  f=-4
 end
 if btn(➡️)then
  x=min(x+1,62)
  f=4 end
 end
 --if the player presses 'O' then punch,
 --but only if the cooldown timer from the last punch is finished
 if c==0 then
  if(btnp(🅾️)) c=10
 end
 --updating the cooldown timer for this frame
 c=max(c-1,0)
 --moving the two red ninjas towards the player
 l+=.7
 r-=.7
 --if the left ninja has run all the way off screen then reset them
 --also: some more character optimization seems possible here!
 --must have missed it at the time :-D
 if l>70 then
  l=y(t)-t
 end
 -- likewise, if the right ninja has ran all the way off the screen then reset them
 if(r<-10) r=y(t)+70
 --ALSO: ^^^^ do i even need to check for the ninjas running off screen?
 --they should collide with the player and reset before they reach the other side.
 --going to look into this, I might be able to create an even more optimized version! \o/
 --PUNCH CHECKING. if the punch cooldown is greater than zero,
 --then check if the player's arm is close enough to an enemy ninja to hit them
 --if it is then we add one to the score and reset the ninja's position off-screen
 if c>0 then
  if a(x-l+f)<4 then
   s+=1l=y(t)-t
  end
  if a(x-r+f)<4 then
   s+=1r=y(t)+70
  end
 end
 -- if the player has beaten the high score then we update the hi-score
 if(s>h) h=s
 -- if the player has collided with either of the two ninjas
 -- we start a new game (reset the score and move the two ninjas to their start points)
 if a(x-r)<2or a(x-l)<2 then
  s=0
  r=y(t)+130
  l=y(t)-82
 end
-- finally, the DRAW LOOP--
-- (naughtily done in _update60() to save chars)
 -- clear the screen
 cls()
 --draw two rectangles for the ground and sky
 z(0,14,64,49,12)
 z(0,43,64,49,5)
 --draw the player
 ?"웃",x-4,39,0
 --if the player is punching, draw a "-" for the player's extended arm
 if c>0 then
  ?"-",x+f-2,39,0
 end
 --draw the two red ninjas
 ?"웃",l-3,39,8
 ?"웃",r-3,39,8
 --print the score and high score at the top
 ?s,6,16,10
 ?h,54,16,10
end