Post by bplus on Feb 16, 2024 11:12:21 GMT -5
And I mean little!
Comments:
line 1: sw and sh are short for screen width and screen height. We are setting up a call to custom size our Screen with _NewImage(sw, sh, 32). With old QB stuff we were stuck with a few screen choices, 12, 13 probably better ones for graphics, and had absolutely No Control over screen size, yuck!!!
line 2: makes that call and the 32 there is specifying 32 bit color which gives us millions of color options through the _RGB32(red, green, blue, alphaOptional) color specification function. You can also use &Hrrggbbaa specifing colors hexidecimally.
line 3: x = .... and y = .... x, y are used for locating our yellow rectangle the subject or "hero" of game you the player control with arrow keypresses. Line 3 gets us started smack in middle of screen.
line 4: we enter our main loop
line 5: we clear off the screen from last loop to rebuild anew
line 6: we poll the keyboard for any key presses and store into kh&. _KeyHit is better than old Inkey$ system because we have numbers rather than one or 2 chars (= ascii byte codes) to analyse.
Now we get to the heavy duty calculation! Lines 7 and 8, x = .... and y = .... moving x, y location of player according to arrow keys pressed.
(kh& = 19200) returns -1 or 0 (true or false respectively) if the left arrow was pressed
(x > 0) returns -1 or 0 (true or false respectively) if x > 0
when both these are true the we have -1 * -1 = 1 and we multiply that by -5 so x = x - 5 !!!
likewise for (kh& = 19712) * (x < sw) when both of those are true, we get x = x + 5*(-1)*(-1) ' same as x = x + 5
and likewise for the y line when the up arrow is pressed and y > 0 then y = y -5 OR if the down arrow is pressed and y < sh then y = y + 5.
These move the player around the screen but prevents the player from going too far up, down, left or right. Nicely done in 2 lines of code!
The next line draws the rectangle according to where x, y are.
_Display ' updates the screen without nasty blinking we did not have available in old QB stuff we use to have to black out old position before redrawing new skipping CLS because of the blinking it caused.
_Limit ' controls how fast to loop around the Do... Loop here no faster than 300 updates per second. BTW if drawing takes longer then it will be less than 300 updates per sec, _Limit just sets an upper limit on how fast it can loop around. Again we had No Handy control like this in the old QB stuff and we did all kinds of crap to try and slow down the updates, never very well!
If kh& = 27 that's the escape key press, the user wants to quit. System not only exits the program but also clears the screen, you may want to leave showing the last screen shot, then use END instead.
Next avoiding obstacles
sw = 800: sh = 600
Screen _NewImage(sw, sh, 32)
x = sw / 2: y = sh / 2
Do
Cls
kh& = _KeyHit
x = x + -5 * (kh& = 19200) * (x > 0) + 5 * (kh& = 19712) * (x < sw)
y = y + -5 * (kh& = 18432) * (y > 0) + 5 * (kh& = 20480) * (y < sh)
Line (x - 50, y - 50)-Step(100, 100), _RGB(255, 255, 0), BF
_Limit 300 ' 10 is too choppy 30 OK? 300! doesn't seem 10 X's faster than 30
_Display
Loop Until kh& = 27
System
Comments:
line 1: sw and sh are short for screen width and screen height. We are setting up a call to custom size our Screen with _NewImage(sw, sh, 32). With old QB stuff we were stuck with a few screen choices, 12, 13 probably better ones for graphics, and had absolutely No Control over screen size, yuck!!!
line 2: makes that call and the 32 there is specifying 32 bit color which gives us millions of color options through the _RGB32(red, green, blue, alphaOptional) color specification function. You can also use &Hrrggbbaa specifing colors hexidecimally.
line 3: x = .... and y = .... x, y are used for locating our yellow rectangle the subject or "hero" of game you the player control with arrow keypresses. Line 3 gets us started smack in middle of screen.
line 4: we enter our main loop
line 5: we clear off the screen from last loop to rebuild anew
line 6: we poll the keyboard for any key presses and store into kh&. _KeyHit is better than old Inkey$ system because we have numbers rather than one or 2 chars (= ascii byte codes) to analyse.
Now we get to the heavy duty calculation! Lines 7 and 8, x = .... and y = .... moving x, y location of player according to arrow keys pressed.
(kh& = 19200) returns -1 or 0 (true or false respectively) if the left arrow was pressed
(x > 0) returns -1 or 0 (true or false respectively) if x > 0
when both these are true the we have -1 * -1 = 1 and we multiply that by -5 so x = x - 5 !!!
likewise for (kh& = 19712) * (x < sw) when both of those are true, we get x = x + 5*(-1)*(-1) ' same as x = x + 5
and likewise for the y line when the up arrow is pressed and y > 0 then y = y -5 OR if the down arrow is pressed and y < sh then y = y + 5.
These move the player around the screen but prevents the player from going too far up, down, left or right. Nicely done in 2 lines of code!
The next line draws the rectangle according to where x, y are.
_Display ' updates the screen without nasty blinking we did not have available in old QB stuff we use to have to black out old position before redrawing new skipping CLS because of the blinking it caused.
_Limit ' controls how fast to loop around the Do... Loop here no faster than 300 updates per second. BTW if drawing takes longer then it will be less than 300 updates per sec, _Limit just sets an upper limit on how fast it can loop around. Again we had No Handy control like this in the old QB stuff and we did all kinds of crap to try and slow down the updates, never very well!
If kh& = 27 that's the escape key press, the user wants to quit. System not only exits the program but also clears the screen, you may want to leave showing the last screen shot, then use END instead.
Next avoiding obstacles