|
Post by Sprout on Jul 12, 2020 21:25:27 GMT
I have made a simple game in Just BASIC! You avoid the fireballs that bounce off the walls
Here is the code:
[start]
loadbmp "playerBmp", "player.bmp" loadbmp "fireBmp", "fire.bmp"
nomainwin
WindowWidth = 1024 WindowHeight = 512
open "Avoid Fireballs" for graphics_nsb_nf as #window
print #window, "trapclose [quit]" print #window, "setfocus" print #window, "when mouseMove [timer]"
index = 0
dim bounceUp(14) dim bounceDown(14) dim bounceLeft(14) dim bounceRight(14)
dim fX(14) dim fY(14)
for index = 0 to 14
bounceUp(index) = 0 bounceDown(index) = 1 bounceLeft(index) = 0 bounceRight(index) = 1
fX(index) = index * 100 fY(index) = index * 120
next
pWidth = 24 pHeight = 28 fWidth = 15 fHeight = 15
prepareFrames = 500 prepareFramesToggle = 0
[timer]
timer 10, [update]
[update]
print #window, "fill white; flush"
for index = 0 to 14
if fX(index) <= 0 then
bounceRight(index) = 1 bounceLeft(index) = 0
end if
if fX(index) + fWidth >= 1024 then
bounceLeft(index) = 1 bounceRight(index) = 0
end if
if fY(index) <= 0 then
bounceDown(index) = 1 bounceUp(index) = 0
end if
if fY(index) + fHeight >= 512 then
bounceUp(index) = 1 bounceDown(index) = 0
end if
if bounceRight(index) = 1 then fX(index) = fX(index) + 3 if bounceLeft(index) = 1 then fX(index) = fX(index) - 2 if bounceDown(index) = 1 then fY(index) = fY(index) + 2 if bounceUp(index) = 1 then fY(index) = fY(index) - 3
next
if prepareFramesToggle = 1 then
prepareFrames = prepareFrames - 1
if prepareFrames = 0 then
prepareFrames = 60 prepareFramesToggle = 0
end if
end if
print #window, "drawbmp playerBmp "; MouseX; " "; MouseY
for index = 0 to 14
print #window, "drawbmp fireBmp "; fX(index); " "; fY(index)
next
if prepareFramesToggle = 0 then
for index = 0 to 14
if collision(MouseX, MouseY, fX(index), fY(index), pWidth, pHeight, fWidth, fHeight) then
prepareFramesToggle = 1 confirm "You hit a fireball! Do you want to quit?"; answer$ if answer$ <> "yes" then wait close #window end
end if
next
end if
wait
function collision(x1, y1, x2, y2, w1, h1, w2, h2)
if x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2 then
collision = 1
else
collision = 0
end if
end function
[quit]
close #window end
I haven't learned how to use sprites in Just BASIC, but this seemed to work fine.
One problem that I have noticed is that since I use a for loop to draw all the fireballs, the last few fireballs that are drawn with the for loop have a flicker to them, if that makes sense. It may not happen on computers that are more powerful than mine. I haven't tested it on any other computers.
|
|
|
Post by cundo on Jul 13, 2020 1:00:03 GMT
Thanks for sharing your first game! Sprites will take rid of flickerings, they are drawn to memory first then you 'show' them altogether.
|
|
|
Post by Sprout on Jul 13, 2020 1:45:23 GMT
Good to know! I'll use sprites for the next project I make.
|
|
|
Post by B+ on Jul 13, 2020 4:04:47 GMT
Allot better than my first game I ever posted! Nice job!
|
|
|
Post by Rod on Jul 13, 2020 14:35:45 GMT
Yes, good job indeed. I made a couple of changes to the way the timer runs and the mouse is tracked. You need to manage the timer off and on when the game pauses. I also seperated the mouse tracking from the timed drawing loop. The mouse tracking happens frequently and lightning fast. So the little [trackit] routine simply gets MouseX MouseY it does not go on and update the game, that happens when the timer event triggers. You can read keypresses and mouse input this way, recording the change then using it in the timed drawing loop.
[start]
loadbmp "playerBmp", "player.bmp" loadbmp "fireBmp", "fire.bmp"
nomainwin
WindowWidth = 1024 WindowHeight = 512
open "Avoid Fireballs" for graphics_nsb_nf as #window
print #window, "trapclose [quit]" print #window, "setfocus" print #window, "when mouseMove [trackit]"
index = 0
dim bounceUp(14) dim bounceDown(14) dim bounceLeft(14) dim bounceRight(14)
dim fX(14) dim fY(14)
for index = 0 to 14
bounceUp(index) = 0 bounceDown(index) = 1 bounceLeft(index) = 0 bounceRight(index) = 1
fX(index) = index * 100 fY(index) = index * 120
next
pWidth = 24 pHeight = 28 fWidth = 15 fHeight = 15 speed=47
prepareFrames = 500 prepareFramesToggle = 0
[timer]
timer speed, [update] wait
[trackit] 'whenever the mouse moves MouseX and MouseY are updated 'we use MouseX and MouseY to place the player later in the drawing loop wait
[update] 'this is called every 100ms by the timer event print #window, "fill white; flush"
for index = 0 to 14
if fX(index) <= 0 then
bounceRight(index) = 1 bounceLeft(index) = 0
end if
if fX(index) + fWidth >= 1024 then
bounceLeft(index) = 1 bounceRight(index) = 0
end if
if fY(index) <= 0 then
bounceDown(index) = 1 bounceUp(index) = 0
end if
if fY(index) + fHeight >= 512 then
bounceUp(index) = 1 bounceDown(index) = 0
end if
if bounceRight(index) = 1 then fX(index) = fX(index) + 3 if bounceLeft(index) = 1 then fX(index) = fX(index) - 2 if bounceDown(index) = 1 then fY(index) = fY(index) + 2 if bounceUp(index) = 1 then fY(index) = fY(index) - 3
next
if prepareFramesToggle = 1 then
prepareFrames = prepareFrames - 1
if prepareFrames = 0 then
prepareFrames = 60 prepareFramesToggle = 0
end if
end if
print #window, "drawbmp playerBmp "; MouseX; " "; MouseY
for index = 0 to 14
print #window, "drawbmp fireBmp "; fX(index); " "; fY(index)
next
if prepareFramesToggle = 0 then
for index = 0 to 14
if collision(MouseX, MouseY, fX(index), fY(index), pWidth, pHeight, fWidth, fHeight) then
'if you pause to interact with the player you need to turn the timer off timer 0 prepareFramesToggle = 1 confirm "You hit a fireball! Do you want to quit?"; answer$ if answer$ <> "yes" then timer speed, [update] wait else close #window end end if end if
next
end if
wait
function collision(x1, y1, x2, y2, w1, h1, w2, h2)
if x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2 then
collision = 1
else
collision = 0
end if
end function
[quit] timer 0 close #window end
|
|
|
Post by tsh73 on Jul 14, 2020 20:59:38 GMT
Good job Played with it a bit added score printed (number of frames survived), frames immune, started in immune mode Tried to hide cursor - at least turned to less visible - a cross And added that "AjustWindow" code to make sure client size is 1024x512 as program thinks.
[start]
loadbmp "playerBmp", "player.bmp" '24x28 loadbmp "fireBmp", "fire.bmp" '15x15
nomainwin
desiredWidth = 1024 desiredHeight = 512 gosub [ajustWindow]
open "Avoid Fireballs" for graphics_nsb_nf as #window
print #window, "trapclose [quit]" print #window, "setfocus" print #window, "when mouseMove [trackit]"
index = 0 N=14 'number of fires cursor CROSSHAIR 'can't turn it off but could make less visible
dim bounceUp(N) dim bounceDown(N) dim bounceLeft(N) dim bounceRight(N)
dim fX(N) dim fY(N)
for index = 0 to N
bounceUp(index) = 0 bounceDown(index) = 1 bounceLeft(index) = 0 bounceRight(index) = 1
fX(index) = index * 100 fY(index) = index * 120
next
pWidth = 24 pHeight = 28 fWidth = 15 fHeight = 15 speed=47
prepareFrames = 500 prepareFramesToggle = 1 'start in immune mode
[timer]
timer speed, [update] wait
[trackit] 'whenever the mouse moves MouseX and MouseY are updated 'we use MouseX and MouseY to place the player later in the drawing loop wait
[update] 'this is called every 100ms by the timer event print #window, "fill white; flush" 'score block if prepareFramesToggle then #window "place 10 20;\";"immune next ";prepareFrames; " frames" #window "\survived ";score;" " #window "\max survived ";maxScore else #window "place 10 20;\";space$(30) #window "\survived ";score;" " #window "\max survived ";maxScore end if
for index = 0 to N
if fX(index) <= 0 then
bounceRight(index) = 1 bounceLeft(index) = 0
end if
if fX(index) + fWidth >= 1024 then
bounceLeft(index) = 1 bounceRight(index) = 0
end if
if fY(index) <= 0 then
bounceDown(index) = 1 bounceUp(index) = 0
end if
if fY(index) + fHeight >= 512 then
bounceUp(index) = 1 bounceDown(index) = 0
end if
if bounceRight(index) = 1 then fX(index) = fX(index) + 3 if bounceLeft(index) = 1 then fX(index) = fX(index) - 2 if bounceDown(index) = 1 then fY(index) = fY(index) + 2 if bounceUp(index) = 1 then fY(index) = fY(index) - 3
next
if prepareFramesToggle = 1 then
prepareFrames = prepareFrames - 1
if prepareFrames = 0 then
prepareFrames = 60 prepareFramesToggle = 0
end if
end if
print #window, "drawbmp playerBmp "; MouseX; " "; MouseY
for index = 0 to N
print #window, "drawbmp fireBmp "; fX(index); " "; fY(index) print #window, "line "; fX(index); " "; fY(index);" ";fX(index)+15; " "; fY(index) print #window, "line "; fX(index); " "; fY(index);" ";fX(index); " "; fY(index)+15
next
if prepareFramesToggle = 0 then score = score+1 if score>maxScore then maxScore=score for index = 0 to N
if collision(MouseX, MouseY, fX(index), fY(index), pWidth, pHeight, fWidth, fHeight) then
'if you pause to interact with the player you need to turn the timer off timer 0 prepareFramesToggle = 1 confirm "You hit a fireball! Do you want to quit?"; answer$ if answer$ <> "yes" then score=0 timer speed, [update] wait else close #window end end if end if
next
end if
wait
function collision(x1, y1, x2, y2, w1, h1, w2, h2)
if x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2 then
collision = 1
else
collision = 0
end if
end function
[quit] timer 0 close #window end
'------------------------------------------------- [ajustWindow] UpperLeftX = 1:UpperLeftY = 1 WindowWidth = 200 '100 seems to be too much - works different WindowHeight = 200 'MENU #gr, "dummy" 'enable if you have menu to account for open "Ajusting..." for graphics_nsb_nf as #gr 'possible other window types ' graphics ' graphics_nsb ' graphics_nsb_nf
#gr, "home ; down ; posxy x y" 'x, y give us width, height width = 2*x : height = 2*y close #gr
slackX = 200-width slackY = 200-height
WindowWidth = desiredWidth + slackX WindowHeight = desiredHeight + slackY return
|
|
|
Post by tsh73 on Jul 15, 2020 13:06:26 GMT
Just to say. Spent yesterday some may be hour trying to make fire reflect from bottom. There is "ajust window" code, made just for that and it worked with X axis like a charm, from the first time but on the Y axis, it kept reflection too early. Like, 20 pixels above bottom line. I even printed Y position where it reflected (and it was OK) Finally I measured pixel size of window - and it was too long After all it happened to be this line 'MENU #gr, "dummy" 'enable if you have menu to account for (comment was added after I found that out!) It was my code initially but I totally forgot that
|
|
|
Post by Rod on Jul 15, 2020 19:45:19 GMT
Ha, menu. I came across that very recently. Should have mentioned it. The test window needs to use the same components as the eventual window.
|
|
|
Post by Sprout on Jul 16, 2020 1:01:19 GMT
Thanks for all the fixes and additions to the game!
|
|