Thank you so much and now I understand global variables and select and case. At least moor than I did. I need some direction with something else now. I want to make my sprite, the one I move (ELEMENT) bounce off of obstacles on the background and the border but I haven’t figured it out.
Attempted code to move my sprite around minus the border.
`UP
if upkey()=1
y=y-3
` y can not be less than 0
else
if y <= 0 then y=y
endif
endif
`DOWN
if downkey()=1
y=y+3
` y can not be greater than 650
else
if y >= 650 then y=y
endif
endif
`RIGHT
if rightkey()=1
x=x+3
` x can not be less than 0
else
if x <= 0 then x=x
endif
endif
`LEFT
if leftkey()=1
x=x-3
` x can not be greater than 990
else
if x >= 990 then x=x
endif
endif
Code that makes fire move automatically.
REM move fires
REM FIRE VARIABLES
firex = firex + speedx
firey = firey + speedy
REM check condition for firey
if firey > 650 - border
firey = 650 - border
speedy = speedy * -1
else
if firey < border
firey = border
speedy = speedy * -1
endif
endif
REM check condition for firex
if firex > 950 - border
firex = 950 - border
speedx = speedx * -1
else
if firex < border
firex = border
speedx = speedx * -1
endif
endif
Full Game Code
REM SETUP ----------------------------
set display mode 1024, 768, 32
sync on : sync rate 60 : hide mouse
` Global variables are seen inside functions
global endx
global endy
global firex
global firey
global speedx
global speedy
global border
global x
global x
global y
global y
REM INIT / LOAD SPRITES ----------------------------
` Don't need this line because 0,0,0 is the default transparent color
`set image colorkey 0, 0, 0
REM ELEMENT
create animated sprite 1, "element.bmp", 19, 1, 1
REM END
create animated sprite 2, "end.bmp", 5, 1, 2
REM FIRE
create animated sprite 3, "fire.bmp", 2, 1, 3
REM CLOUD
create animated sprite 4, "clouds.bmp", 3, 1, 4
REM WALLS
`create animated sprite 5, "walls.bmp", 1, 1, 5
REM PUDDLE
`create animated sprite 6, "puddle.bmp", 4, 1, 6
REM LEVEL VALUE
Level = 1
REM LOAD BACKGROUND
` Instead of using "load bitmap" use "load image"
` That way you can just paste the background every loop
Load image "bg1.bmp",1001,1
Load image "bg2.bmp",1002,1
Load image "bg3.bmp",1003,1
REM PRE MAIN
set current bitmap 0
` Setup the first level
LoadLevel(Level)
REM MAIN LOOP
do
` Paste the levels background
paste image 1000+Level,0,0,1
REM ELEMENT SPRITE
sprite 1, x, y, 1
play sprite 1, 1, 19, 30
REM END SPRITE
sprite 2, endx, endy, 2
play sprite 2, 1, 5, 100
REM fire SPRITE
sprite 3, firex, firey, 3
play sprite 3, 1, 2, 50
REM ELEMENT SPRITES MOVEMENTS
`UP
if upkey()=1
y=y-3
` y can not be less than 0
endif
`DOWN
if downkey()=1
y=y+3
` y can not be greater than 650
endif
`RIGHT
if rightkey()=1
x=x+3
` x can not be less than 0
endif
`LEFT
if leftkey()=1
x=x-3
` x can not be greater than 990
endif
REM COLLISIONS
REM Collision to change levels
if sprite hit (1, 2)
set cursor 300, 300
Print "Player made it to Next Level"
set cursor 295, 320
Print "Press any key for Next Level"
` Anytime you want to update the screen you need a "sync"
` Without this the player won't see the next level text
` and not know he/she needs to hit any key to continue
sync
wait key
` Increase Level
inc Level
` Setup next level
LoadLevel(Level)
endif
REM Collision with FIRE
if sprite hit (1, 3)
set cursor 300, 300
print "player collided with fire GAME OVER"
hide sprite 1
sprite 4, x, y, 4
play sprite 4, 1, 3, 400
endif
REM move fires
REM FIRE VARIABLES
firex = firex + speedx
firey = firey + speedy
REM check condition for firey
if firey > 650 - border
firey = 650 - border
speedy = speedy * -1
else
if firey < border
firey = border
speedy = speedy * -1
endif
endif
REM check condition for firex
if firex > 950 - border
firex = 950 - border
speedx = speedx * -1
else
if firex < border
firex = border
speedx = speedx * -1
endif
endif
REM END MAIN LOOP
fastsync
loop
` Level Setup
function LoadLevel(CLevel)
` Pick level
select CLevel
` Level 1
case 1
endx = 890
endy = 640
firex = rnd(950)
firey = rnd(650)
speedx = 1
speedy = -1
border = 25
endcase
` Level 2
case 2
endx = 890
endy = 100
firex = rnd(950)
firey = rnd(650)
speedx = 1
speedy = -1
border = 25
x = 0
y = 0
endcase
` Level 3
case 3
endx = 200
endy = 300
firex = rnd(950)
firey = rnd(650)
speedx = 1
speedy = -1
border = 25
x = 0
y = 0
endcase
` Level 4
case 4
end
endcase
endselect
endfunction
FNG