Author Topic: Pick the Stick  (Read 2707 times)

cvirus

  • Guest
Pick the Stick
« on: January 31, 2013, 02:09:03 PM »
This is a small demo for learning how to use and clip sprites from a spritesheet and perform explosions and functions i hope....

Code: [Select]
screen(640,480,0,"Pick the Stick ")
backcolor(0,255,0)
setframetimer(20)

colorkey(0,0,0)
local player={image = loadimage("bluegirl.png"),x = screenwidth()/2,y= screenheight()/2,
speed = 3}

colorkey(255,255,255)
local stick = loadimage("stick.png")
colorkey(0,0,0)
local explosion = loadimage("explo.png")
local down = 0;leftd = 1; rightd = 2;up = 3
local step = 0;explosions = false
local key = 0;sticks = 0;x = rnd()*100;y =rnd()*100;expx = 0
local keyCodes = {LEFTDIR = 276,RIGHTDIR = 275,UPDIR = 273,DOWNDIR = 274,ESC=27}
local side = down; stickx = 0;sticky = 0;timer=0

colorkey(0,0,0)
myfont = loadbmpfont("font.bmp",3,20," !     '()  ,-./0123456789:;   ? ABCDEFGHIJKLMNOPQRSTUVWXYZ_")
sound = loadsound("boom.wav")

--'function for the animation, not needed, just for function introduction
function SpriteAnim(width,height,xcoord,ycoord,image,step,side)
    drawimage(width*step,height*side,width,height,xcoord,ycoord,image)
end
--' end of function animation

repeat
  key=getkey()
  cls()

  if key == keyCodes.LEFTDIR then
     side = leftd
     player.x = player.x - player.speed
     step = step +1
  end
  if key == keyCodes.RIGHTDIR then
     side = rightd
     player.x = player.x + player.speed
     step = step +1
  end
  if key == keyCodes.UPDIR then
   side = up
   player.y = player.y - player.speed
   step = step + 1
end
 if key == keyCodes.DOWNDIR then
   side = down
   player.y = player.y + player.speed
   step = step + 1
 end

 if step >= 3 then
    step = 0
 end

 if player.x < 0 then
    side = down
    step = 0
    player.x = 0
 end
 if player.x > screenwidth() - 32 then
    player.x = screenwidth() - 32
    side = down
    step = 0
 end

 if player.y < 0 then
    player.y = 0
    side = down
    step = 0
 end
 if player.y > screenheight() -32 then
    player.y = screenheight() -32
    side = up
    step = 0
 end

 if boxcoll(player.x,player.y,imagewidth(player.image)/4,imageheight(player.image)/4,x,y,imagewidth(stick),imageheight(stick)) then
    stickx = x
    sticky = y
    x = rnd()*screenwidth() - 32
    y = rnd()*screenheight() -32
    explosions = true
    sticks = sticks + 1
 end
 SpriteAnim(32,32,player.x,player.y,player.image,step,side)

 putimage(x,y,stick)

 if explosions ==true then
    drawimage(32*expx,0,32,32,stickx,sticky,explosion)
    playsound (sound,60, 2, 0)
 end

 expx = expx + 1

 if expx ==7 then
    expx = 0
    explosions=false
 end

bmptext(0,0,uppercase("Stick's - ")..sticks,myfont)
setcaption("Pick the Stick "..sticks)
sync()
until key == keyCodes.ESC
closewindow()                                                     

cvirus

  • Guest
Re: Pick the Stick
« Reply #1 on: February 04, 2013, 11:13:51 AM »
Added a delay because of the explosions, they were too fast.

Code: [Select]
screen(640,480,0,"Pick the Stick ")
backcolor(0,255,0)
setframetimer(20)

colorkey(0,0,0)
local player={image = loadimage("bluegirl.png"),x = screenwidth()/2,y= screenheight()/2,
speed = 3}

colorkey(255,255,255)
local stick = loadimage("stick.png")
colorkey(0,0,0)
local explosion = loadimage("explo.png")
local down = 0;leftd = 1; rightd = 2;up = 3
local step = 0;explosions = false
local key = 0;sticks = 0;x = rnd()*100;y =rnd()*100;expx = 0
local keyCodes = {LEFTDIR = 276,RIGHTDIR = 275,UPDIR = 273,DOWNDIR = 274,ESC=27}
local side = down; stickx = 0;sticky = 0;delay=0

colorkey(0,0,0)
myfont = loadbmpfont("font.bmp",3,20," !     '()  ,-./0123456789:;   ? ABCDEFGHIJKLMNOPQRSTUVWXYZ_")
sound = loadsound("boom.wav")

--'function for the animation, not needed, just for function introduction
function SpriteAnim(width,height,xcoord,ycoord,image,step,side)
    drawimage(width*step,height*side,width,height,xcoord,ycoord,image)
end
--' end of function animation

repeat
  key=getkey()
  cls()

  if key == keyCodes.LEFTDIR then
     side = leftd
     player.x = player.x - player.speed
     step = step +1
  end
  if key == keyCodes.RIGHTDIR then
     side = rightd
     player.x = player.x + player.speed
     step = step +1
  end
  if key == keyCodes.UPDIR then
   side = up
   player.y = player.y - player.speed
   step = step + 1
end
 if key == keyCodes.DOWNDIR then
   side = down
   player.y = player.y + player.speed
   step = step + 1
 end

 if step >= 3 then
    step = 0
 end

 if player.x < 0 then
    side = down
    step = 0
    player.x = 0
 end
 if player.x > screenwidth() - 32 then
    player.x = screenwidth() - 32
    side = down
    step = 0
 end

 if player.y < 0 then
    player.y = 0
    side = down
    step = 0
 end
 if player.y > screenheight() -32 then
    player.y = screenheight() -32
    side = up
    step = 0
 end

 if boxcoll(player.x,player.y,imagewidth(player.image)/4,imageheight(player.image)/4,x,y,imagewidth(stick),imageheight(stick)) then
    stickx = x
    sticky = y
    x = rnd()*screenwidth() - 32
    y = rnd()*screenheight() -32
    explosions = true
    sticks = sticks + 1
 end
 SpriteAnim(32,32,player.x,player.y,player.image,step,side)

 putimage(x,y,stick)

 if explosions ==true then
    drawimage(32*expx,0,32,32,stickx,sticky,explosion)
    playsound (sound,60, 2, 0)
 end

 delay = delay + 0.2
 if delay >= 1 then
    delay = 0
    expx = expx + 1
    if expx > 7 then
       expx = 0
       explosions = false
    end
 end

bmptext(0,0,uppercase("Stick's - ")..sticks,myfont)
setcaption("Pick the Stick "..sticks)
sync()
until key == keyCodes.ESC
closewindow() 

cvirus

  • Guest
Re: Pick the Stick
« Reply #2 on: February 07, 2013, 02:50:20 PM »
Added a background grass

Code: [Select]
screen(640,480,0,"Pick the Stick ")
backcolor(0,255,0)
setframetimer(20)

colorkey(0,0,0)
local player={image = loadimage("bluegirl.png"),x = screenwidth()/2,y= screenheight()/2,
speed = 3}

colorkey(255,255,255)
local stick = loadimage("stick.png")
colorkey(0,0,0)
local explosion = loadimage("explo.png")
local down = 0;leftd = 1; rightd = 2;up = 3
local step = 0;explosions = false
local key = 0;sticks = 0;x = rnd()*100;y =rnd()*100;expx = 0
local keyCodes = {LEFTDIR = 276,RIGHTDIR = 275,UPDIR = 273,DOWNDIR = 274,ESC=27}
local side = down; stickx = 0;sticky = 0;delay=0
local grass = loadimage("grass.bmp")
colorkey(0,0,0)
myfont = loadbmpfont("font.bmp",3,20," !     '()  ,-./0123456789:;   ? ABCDEFGHIJKLMNOPQRSTUVWXYZ_")
sound = loadsound("boom.wav")

--'function for the animation, not needed, just for function introduction
function SpriteAnim(width,height,xcoord,ycoord,image,step,side)
    drawimage(width*step,height*side,width,height,xcoord,ycoord,image)
end
--' end of function animation

repeat
  key=getkey()
  cls()

  for x = 0,screenwidth()/imagewidth(grass) do
      for y = 0,screenheight()/imageheight(grass) do
          putimage(x*imagewidth(grass),y*imageheight(grass),grass)
      end
  end

  if key == keyCodes.LEFTDIR then
     side = leftd
     player.x = player.x - player.speed
     step = step +1
  end
  if key == keyCodes.RIGHTDIR then
     side = rightd
     player.x = player.x + player.speed
     step = step +1
  end
  if key == keyCodes.UPDIR then
   side = up
   player.y = player.y - player.speed
   step = step + 1
end
 if key == keyCodes.DOWNDIR then
   side = down
   player.y = player.y + player.speed
   step = step + 1
 end

 if step >= 3 then
    step = 0
 end

 if player.x < 0 then
    side = down
    step = 0
    player.x = 0
 end
 if player.x > screenwidth() - 32 then
    player.x = screenwidth() - 32
    side = down
    step = 0
 end

 if player.y < 0 then
    player.y = 0
    side = down
    step = 0
 end
 if player.y > screenheight() -32 then
    player.y = screenheight() -32
    side = up
    step = 0
 end

 if boxcoll(player.x,player.y,imagewidth(player.image)/4,imageheight(player.image)/4,x,y,imagewidth(stick),imageheight(stick)) then
    stickx = x
    sticky = y
    x = rnd()*screenwidth() - 32
    y = rnd()*screenheight() -32
    explosions = true
    sticks = sticks + 1
 end
 SpriteAnim(32,32,player.x,player.y,player.image,step,side)

 putimage(x,y,stick)

 if explosions ==true then
    drawimage(32*expx,0,32,32,stickx,sticky,explosion)
    playsound (sound,60, 2, 0)
 end

 delay = delay + 0.2
 if delay >= 1 then
    delay = 0
    expx = expx + 1
    if expx > 7 then
       expx = 0
       explosions = false
    end
 end

bmptext(0,0,uppercase("Stick's - ")..sticks,myfont)
setcaption("Pick the Stick "..sticks)
sync()
until key == keyCodes.ESC
closewindow()                                           

This bmp file is from ESGL example.