Simple script to make a block move around a 800x600 window using the arrow keys.
(Extracted and modified from the "Simple Break-Out Clone" script by: ArtBlink & Figgy/CtZ & amyren, found elsewhere on this forum)
@DISPLAY{WIDTH = 800, HEIGHT = 600, MODE = "WINDOWED"} ;Create the window
CreateBrush(1,32,32,#GREEN) ; CREATE BLOCK-MAN
BM_Xpos = #CENTER ; Set initial X-position
BM_Ypos = #CENTER ; Set initial Y-position
SPEED = 5 ; Set the movement speed
Function p_UpdateScreen()
;Check Key-presses and move Block-Man:
If IsKeyDown("LEFT") = True Then BM_XPos = BM_XPos - SPEED
If IsKeyDown("RIGHT") = True Then BM_XPos = BM_XPos + SPEED
If IsKeyDown("UP") = True Then BM_YPos = BM_YPos - SPEED
If IsKeyDown("DOWN") = True Then BM_Ypos = BM_Ypos + SPEED
;Check if Block-Man leaves the screen and reposition if he does:
If BM_XPos < 0 Then BM_XPos = 0
If BM_XPos > 768 Then BM_XPos = 768
If BM_Ypos < 0 Then BM_Ypos = 0
If BM_Ypos > 568 Then BM_Ypos = 568
DisplayBrush(1, BM_Xpos, BM_Ypos)
EndFunction
BeginDoubleBuffer()
EscapeQuit(True)
Repeat
Cls()
p_UpdateScreen()
Flip()
Forever
EndDoubleBuffer()
Two issue: How can I use the initial position without it being instantly updated before any key is pressed?
How can I avoid the speed increase when moving diagonally?
The program runs in a so called "busy loop" without waits, which is very bad coding practise (you can't emphasize this enough). It makes your computer run unnecessary hot, waste electricity, and leaves less CPU time for other programs in your system. So please read the documentation how to code main loops properly, and I hope the original example wasn't doing it like this or if it was, hopefully it hasn't been copied too many times
Sorry a bit hurry to reply to the actual questions now... but just had to tell this
The program runs in a so called "busy loop" without waits, which is very bad coding practise (you can't emphasize this enough). It makes your computer run unnecessary hot, waste electricity, and leaves less CPU time for other programs in your system. So please read the documentation how to code main loops properly, and I hope the original example wasn't doing it like this or if it was, hopefully it hasn't been copied too many times
Sorry a bit hurry to reply to the actual questions now... but just had to tell this
Thanks for letting me know. I don't think that was discussed in the original thread. Will update the original post with a warning.
How might this be rectfied? Sorry, I am still very much a beginner and this was meant for like-minded folk.
EDIT: I actually can't edit the original post. WARNING! Don't do this, guys. It's BAD practice.
Argh, sorry, I was actually wrong with this, now when looking the code with better time, Flip() without argument defaults to True and actually waits for a screen refresh and you don't need to sync to a certain framerate after all. So it doesn't loop wildly. It would do that if you'd give False for it...
So nothing wrong unless you want to make it run in a specific framerate. But maybe this was a good reminder for not doing it wrong
To the second issue:
You have to use the speed as a vector and split it into x- and y-parts. This is quite simple here, because the box moves only in an angle of 45°, so the horizontal and vertical parts are equal. I made a quick hack with your code:
@DISPLAY{WIDTH = 800, HEIGHT = 600, MODE = "WINDOWED"} ;Create the window
CreateBrush(1,32,32,#GREEN) ; CREATE BLOCK-MAN
BM_Xpos = #CENTER ; Set initial X-position
BM_Ypos = #CENTER ; Set initial Y-position
SPEED = 5 ; Set the movement speed
SPEED45=Sqrt(SPEED^2/2)
/*
speed as vector, Pythagoras:
SPEEDX^2+SPEEDY^2 = SPEED^2
SPEEDX^2 = SPEED^2-SPEEDY^2
SPEEDX = Sqrt(SPEED^2-SPEEDY^2)
SPEEDY = Sqrt(SPEED^2-SPEEDX^2)
*/
Function p_UpdateScreen()
;Check Key-presses and move Block-Man:
;SPEEDX=Sqrt(SPEED^2/2)
SPEEDX=SPEED45 ; calculate this only one time
SPEEDY=SPEEDX ; (because 45°)
If Not IsKeyDown("LEFT") And Not IsKeyDown("RIGHT")
SPEEDX=0
SPEEDY=SPEED
EndIf
If Not IsKeyDown("UP") And Not IsKeyDown("DOWN")
SPEEDY=0
SPEEDX=SPEED
EndIf
If IsKeyDown("LEFT") = True Then BM_XPos = BM_XPos - SPEEDX
If IsKeyDown("RIGHT") = True Then BM_XPos = BM_XPos + SPEEDX
If IsKeyDown("UP") = True Then BM_YPos = BM_YPos - SPEEDY
If IsKeyDown("DOWN") = True Then BM_Ypos = BM_Ypos + SPEEDY
;Check if Block-Man leaves the screen and reposition if he does:
If BM_XPos < 0 Then BM_XPos = 0
If BM_XPos > 768 Then BM_XPos = 768
If BM_Ypos < 0 Then BM_Ypos = 0
If BM_Ypos > 568 Then BM_Ypos = 568
DisplayBrush(1, BM_Xpos, BM_Ypos)
EndFunction
BeginDoubleBuffer()
EscapeQuit(True)
Repeat
Cls()
p_UpdateScreen()
Flip()
Forever
EndDoubleBuffer()
Flinx wrote: ↑Wed Dec 14, 2022 12:36 pm
To the second issue:
You have to use the speed as a vector and split it into x- and y-parts. This is quite simple here, because the box moves only in an angle of 45°, so the horizontal and vertical parts are equal. I made a quick hack with your code: