Sometimes, I wonder how you code every game efficiently using min() or max() functions.
This is a piece of code to animate a Santa Claus :
- cell sprite animation
- move santa from left to the right
- make santa jump up and down
Please advice me how to make the code efficient, perhaps using min() or max() functions ?
Thank you.
Code:
'santa claus animation
'----------------------
'Cell Animation
draw image santa.run,santa.move,santa.jump,santa.cell
if santa.cell<11 then
santa.cell = santa.cell + dt*20
else
santa.cell = 0
endif
'Running Santa
if santa.move > 520 then
santa.move = -50
else
santa.move = santa.move + dt*50
endif
'Santa Position
if keydown(KEY_SPACE) then
santa.pos = 1 'santa position 0=stay 1=up 2=down
endif
if santa.pos=1 then
santa.jump = santa.jump - dt*100
elseif santa.pos=2 then
santa.jump = santa.jump + dt*100
else
santa.jump = 175
endif
if santa.jump <= 80 then
santa.pos = 2
elseif santa.jump >= 175 then
santa.pos = 0
endif
11-16-2024, 10:25 AM (This post was last modified: 11-16-2024, 02:01 PM by Marcus.)
Here are some modifications:
Code:
'=========================================================================
' Winter Christmas
' It's almost Christmas ! Santa Claus is running to the north pole.
' Please be careful, Yeti didn't like Santa Claus.
'
' Control keys
' - SPACE BAR = jump
' - DOWN = slow down game speed
' - LEFT = snow direction left to right
' - RIGHT = snow direction right to left
'
' Resources
' - Slow motion effect by Marcus
' https://naalaa.com/forum/thread-155-post-1124.html#pid1124
' - Wind moderate strong JPN by freesound community
' https://pixabay.com/sound-effects/wind-moderate-strong-jpn-78086/
' - Yeti King sprite sheet submitter by madhattervx
' https://www.spriters-resource.com/pc_computer/spelunky2/sheet/182550/
' - Santa Claus Sprite Sheet
' https://www.gameart2d.com/santa-claus-free-sprites.html
'
'=========================================================================
'----------------
' INITIALIZATION
'----------------
#win32
set window "Winter Christmas", 512, 256,false,2
set redraw off
'color definition
deepblue = [20,30,40]
white = [255,255,255]
visible j 'timer for snow direction
visible direction 'snow direction
message = ""
' snow
snow = []
for i = 1 to 1000
c = 64 + rnd(256)
snow[sizeof(snow)] = [
x: rnd(512), y: rnd(512),
dy: 25 + rnd(200),
c: [c, c, c],
Update: function(dt,direction)
this.y = (this.y + this.dy*dt)%230
if direction then
if this.x > 0 then
this.x = this.x - rnd(0,1)
else
this.x = 512
endif
else
if this.x < 512 then
this.x = this.x + rnd(0,1)
else
this.x = 0
endif
endif
endfunc,
Draw: function()
set color this.c
draw pixel this.x, this.y
endfunc]
next
' Game speed.
gameSpeed = 1
' Sound effect
wind = loadmusic("assets/wind1.wav")
play music wind,1
' Santa Claus
santa = []
santa.run = loadimage("assets/santaclaus.png",11,1)
santa.cell = 0
santa.move = -50
santa.jump = 175
santa.pos = 0 'santa position 0=stay 1=up 2=down
santa.deltay = 0
'--------------
' MAIN PROGRAM
'--------------
prevTime = clock()
while not keydown(KEY_ESCAPE)
' Delta time
t = clock()
dt = (min(t - prevTime, 66))/1000
prevTime = t
' Slow down by pressing DOWN
if keydown(KEY_DOWN) then
gameSpeed = max(gameSpeed - dt/5, 0.25)
else
gameSpeed = min(gameSpeed + dt/5, 1)
endif
dt = dt*gameSpeed
set color deepblue ; cls 'clear background
set color white ; draw rect 0,220,512,10,1 'white ground
'yeti animation
draw image yeti.walk,yeti.move,170, yeti.cell 'yeti walk
'if yeti.cell <8 then
' yeti.cell = yeti.cell + dt*3
'else
' yeti.cell = 0
'endif
yeti.cell = (yeti.cell + dt*3)%8
if direction = 1 then 'depends on wind direction
if yeti.move < -60 then
yeti.move = 520
else
yeti.move = yeti.move - dt*20
endif
endif
'snow animation
if j < 3000
direction = 1 'move from right to left
message = "to the left"
elseif (j > 3000 and j < 3500)
direction = 0 'move from left to right
message = "to the right"
elseif j>3500
j = 0
endif
j = j + 1
if keydown(KEY_RIGHT) then j = 3000
if keydown(KEY_LEFT) then j = 0
foreach s in snow s.Update(dt,direction)
foreach s in snow s.Draw()
'info
set color white
set caret 10,235; wln "xxx"
set caret 480,235; wln "xxx"
redraw
'wait 1
fwait 60
wend
free music wind
I use the % (modulus) operator for the animation.
min and max just returns the smallest or largest of two numbers.
Thanks a lot, you made the code more efficient with modulus % operator
sometimes, I make too many [ if...then...else.... ] and it's time to get rid of them