Author Topic: Lander project  (Read 4592 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Lander project
« on: June 04, 2018, 02:26:26 AM »
Well I have it setup the way I want to do it with InForm:

In SmallBASIC I could do a message box, here I used the _title for messages.
Code: QB64: [Select]
  1. _TITLE "Lander B+ started 2018-06-02"
  2. ' Lander update.bas SmallBASIC 0.12.11 (B+=MGA) 2018-06-01
  3.  
  4. 'modified code from my 2nd mod of:
  5. 'Lander mod 2.txt for JB v2 B+ 2018-05-29 big mod of
  6. 'Lander by Carl mod Rod mod B+.txt for JB v2 started 2018-05-26
  7. 'where I rewired controls and changed physics of Lander Model.
  8.  
  9. 'This will further depart from Carls's original by hand drawing Lander
  10. 'at different angles instead of using sprites and, alas, landscape will
  11. 'have to be updated each frame because there is no drawing on top of images
  12. 'in SmallBASIC.
  13.  
  14. ' INSTRUCTIONS:
  15. 'Use the left or right arrow keys to rotate Lander left or right.
  16. 'Use the up arrow for thruster burst. These moves cost fuel!
  17. 'The Fuel Gage is Red Horizontal line below landscape.
  18. 'The fuel level is Yellow.
  19.  
  20. 'You must make a VERY gentle and level landing
  21. 'on one of the flat areas!
  22.  
  23. 'Horizontal location, speed in green.
  24. '  Vertical location, speed inblue
  25.  
  26. DIM SHARED main&
  27. CONST xmax = 1200
  28. CONST ymax = 720
  29. main& = _NEWIMAGE(xmax, ymax, 32)
  30. SCREEN main&
  31. _SCREENMOVE 100, 10
  32.  
  33. CONST ns = 75
  34.  
  35. DIM SHARED pi, d2r
  36. pi = _PI
  37. d2r = pi / 180
  38.  
  39. 'stars
  40. DIM SHARED sx(ns), sy(ns), sr(ns), sc&(ns)
  41. 'terrain
  42. DIM SHARED terraH(xmax), terraC(xmax)
  43. 'vehicle globals
  44. DIM SHARED fuel, vda, speed, vx, vy, dx, dy, dg, dat
  45.  
  46. restart: ' =========================================   initialize Game
  47. makeStars
  48. makeTerra
  49. fuel = 500 'this is the space vehicle's fuel
  50.  
  51. 'vda is vehicle degree angle = orientation of the vehicle, mainly it's thrusters
  52. vda = 0 'the vehicle is traveling right across screen due East = 0 degrees = 0 Radians
  53. speed = 6 'this is the speed the vehicle is moving in the vda direction
  54. vx = 50 'this is current x position of vehicle 10 pixles from left side
  55. vy = 30 'this is current y position of vehicle 10 pixels down from top of screen
  56.  
  57. 'd stands for delta with stands for change dx = change in x, dy = change in y
  58. 'dg is change due to gravity (vertical)
  59. 'dat is change of acceleration due to thrust
  60. dx = speed * COS(d2r * vda) 'this is the horizontal x change on screen due to speed and angle
  61. dy = speed * SIN(d2r * vda) 'this is the vertical y change on screen due to speed and angle
  62. dg = .1 'this is the constant acceleration gravity applies to the vehicle
  63. dat = 2 'this is burst of acceleration a thrust or reverse thrust will apply to speed and angle
  64. COLOR _RGB32(0, 0, 0), _RGB32(0, 45, 90)
  65. 'buttons
  66. drwbtn 290, ymax - 80, "Rotate Left"
  67. drwbtn 500, ymax - 80, "Forward Thrust"
  68. drwbtn 710, ymax - 80, "Rotate Right"
  69.     'respond to button clicks
  70.     mx = _MOUSEX
  71.     my = _MOUSEY
  72.     mb = _MOUSEBUTTON(1)
  73.     IF mb THEN
  74.         IF my > ymax - 80 AND my < ymax - 30 THEN
  75.             IF mx > 290 AND mx < 490 THEN
  76.                 moveLeft
  77.             ELSEIF mx > 500 AND mx < 700 THEN
  78.                 moveUp
  79.             ELSEIF mx > 710 AND mx < 910 THEN
  80.                 moveRight
  81.             END IF
  82.         END IF
  83.     END IF
  84.     'respond to key press
  85.     k$ = INKEY$
  86.     IF LEN(k$) = 2 THEN
  87.         SELECT CASE ASC(RIGHT$(k$, 1))
  88.             CASE 72: moveUp
  89.             CASE 75: moveLeft
  90.             CASE 77: moveRight
  91.         END SELECT
  92.     ELSEIF LEN(k$) = 1 THEN
  93.         IF ASC(k$) = 27 THEN END
  94.     END IF
  95.     scene
  96.     'fuel line
  97.     rgb 300
  98.     recf 10, ymax - 25, xmax - 10, ymax - 5
  99.     ff = fuel / 500 * (xmax - 20)
  100.     rgb 860
  101.     recf 10, ymax - 20, ff + 10, ymax - 10
  102.     COLOR _RGB32(200, 200, 250), _RGB32(0, 45, 90)
  103.     _PRINTSTRING (10, ymax - 70), "Horizontal:" + STR$(INT(vx)) + "," + STR$(INT(dx))
  104.     _PRINTSTRING (10, ymax - 50), "  Vertical:" + STR$(INT(vy)) + "," + STR$(INT(dy))
  105.  
  106.     'vehicle falls faster and faster, because gravity effects the vertical speed
  107.     dy = dy + dg 'speed up falling due to gravity acceleration
  108.  
  109.     'new position = last postion plus the horizontal and vertical changes from momentum
  110.     vx = vx + dx
  111.     vy = vy + dy
  112.     Lander vx, vy, d2r * vda
  113.  
  114.     IF vx < 30 OR vx > xmax - 30 OR vy < -50 THEN 'edit keep Lander legs inside boundries of terraH()
  115.         _TITLE "You have drifted off screen. Press p to play again..."
  116.         EXIT WHILE
  117.     END IF
  118.  
  119.     IF vy > terraH(vx) OR fuel <= 0 THEN
  120.         crash$ = ""
  121.         IF fuel <= 0 THEN
  122.             crash$ = crash$ + "Ran out of fuel. "
  123.         ELSE
  124.             IF vda <> 270 THEN crash$ = crash$ + "Vehicle not upright. "
  125.             IF dy > 4 THEN crash$ = crash$ + "Came down too fast. "
  126.             IF ABS(dx) > 4 THEN crash$ = crash$ + "Still moving hoizontally too fast. "
  127.             IF terraH(vx - 10) <> terraH(vx + 10) THEN crash$ = crash$ + "Did not land on level site. "
  128.         END IF
  129.         IF crash$ <> "" THEN
  130.             _TITLE "You crashed! because: " + crash$ + " Press p to play again..."
  131.         ELSE
  132.             _TITLE "Nice job! Successful landing!  Press p to play again..."
  133.         END IF
  134.         EXIT WHILE
  135.     END IF
  136.     _DISPLAY
  137.     _LIMIT 10
  138. k$ = ""
  139. drwbtn 990, ymax - 80, "Restart"
  140. WHILE LEN(k$) = 0
  141.     k$ = INKEY$
  142.     mx = _MOUSEX
  143.     my = _MOUSEY
  144.     mb = _MOUSEBUTTON(1)
  145.     IF mb THEN
  146.         IF my > ymax - 80 AND my < ymax - 30 THEN
  147.             IF mx > 990 AND mx < 1190 THEN
  148.                 k$ = "p"
  149.             END IF
  150.         END IF
  151.     END IF
  152.     _LIMIT 200
  153. IF k$ = "p" THEN GOTO restart
  154.  
  155. SUB scene
  156.     rgb 101
  157.     recf 4, 4, xmax - 5, ymax - 85
  158.     FOR i = 0 TO ns
  159.         COLOR sc&(i)
  160.         fcirc sx(i), sy(i), sr(i)
  161.     NEXT
  162.     FOR i = 4 TO xmax - 5
  163.         rgb terraC(i) * 100 + terraC(i) * 10 + terraC(i)
  164.         ln i, terraH(i), i, ymax - 86
  165.     NEXT
  166. '                              arrow + esc key
  167. SUB moveUp
  168.     'here is the vertical and horizontal change from a burst of fuel for thrust
  169.     thrustx = dat * COS(d2r * vda)
  170.     thrusty = dat * SIN(d2r * vda)
  171.  
  172.     'now change the horizontal and vertical momentums from the thrust
  173.     dx = dx + thrustx
  174.     dy = dy + thrusty
  175.  
  176.     'update the position
  177.     vx = vx + dx
  178.     vy = vy + dy
  179.     rgb 990
  180.     fcirc vx, vy, 5
  181.     _DISPLAY
  182.  
  183.     'the thrust cost fuel
  184.     fuel = fuel - 10
  185.  
  186. SUB moveLeft
  187.     x1 = vx + 10 * COS(d2r * vda + .5 * pi)
  188.     y1 = vy + 10 * SIN(d2r * vda + .5 * pi)
  189.     rgb 990
  190.     fcirc x1, y1, 5
  191.     _DISPLAY
  192.     vda = vda - 22.5
  193.     IF vda < -0.01 THEN vda = 360
  194.     fuel = fuel - 10
  195.  
  196. SUB moveRight
  197.     x1 = vx + 10 * COS(d2r * vda - .5 * pi)
  198.     y1 = vy + 10 * SIN(d2r * vda - .5 * pi)
  199.     rgb 990
  200.     fcirc x1, y1, 5
  201.     _DISPLAY
  202.     vda = vda + 22.5
  203.     IF vda > 337.51 THEN vda = 0
  204.     fuel = fuel - 10
  205.  
  206. SUB Lander (x0, y0, rAngle) 'rebuilt from ground up literally!
  207.     'x0, y0 are at the base of the lander, the rocket will point rAngle up when landing
  208.     rgb 333
  209.     x1 = x0 + 10 * COS(rAngle - .5 * pi)
  210.     y1 = y0 + 10 * SIN(rAngle - .5 * pi)
  211.     x2 = x0 + 10 * COS(rAngle + .5 * pi)
  212.     y2 = y0 + 10 * SIN(rAngle + .5 * pi)
  213.     x3 = x0 + 10 * COS(rAngle)
  214.     y3 = y0 + 10 * SIN(rAngle)
  215.     x4 = x0 + 25 * COS(rAngle)
  216.     y4 = y0 + 25 * SIN(rAngle)
  217.     'legs/fins
  218.     ln x3, y3, x1, y1
  219.     ln x3, y3, x2, y2
  220.     ln x4, y4, x1, y1
  221.     ln x4, y4, x2, y2
  222.     pangle = 2 * pi / 5
  223.     COLOR _RGB32(20, 0, 0)
  224.     FOR i = 0 TO 5
  225.         SELECT CASE i
  226.             CASE 0, 5: r = 20
  227.             CASE 2, 3: r = 15
  228.             CASE 1, 4: r = 25
  229.         END SELECT
  230.         x1 = x4 + r * COS(i * pangle + rAngle)
  231.         y1 = y4 + r * SIN(i * pangle + rAngle)
  232.         IF i <> 0 THEN ln lx, ly, x1, y1
  233.         lx = x1: ly = y1
  234.     NEXT
  235.     PAINT (x4, y4), _RGB(160, 120, 120), _RGB32(20, 0, 0)
  236.  
  237. SUB ln (x1, y1, x2, y2)
  238.     LINE (x1, y1)-(x2, y2)
  239.  
  240. SUB rec (x1, y1, x2, y2)
  241.     LINE (x1, y1)-(x2, y2), , B
  242.  
  243. SUB recf (x1, y1, x2, y2)
  244.     LINE (x1, y1)-(x2, y2), , BF
  245.  
  246. SUB rgb (n) ' New (even less typing!) New Color System 1000 colors with up to 3 digits
  247.     s3$ = RIGHT$("000" + LTRIM$(STR$(n)), 3)
  248.     r = VAL(MID$(s3$, 1, 1)): IF r THEN r = 28 * r + 3
  249.     g = VAL(MID$(s3$, 2, 1)): IF g THEN g = 28 * g + 3
  250.     b = VAL(MID$(s3$, 3, 1)): IF b THEN b = 28 * b + 3
  251.     COLOR _RGB32(r, g, b)
  252.  
  253. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  254. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  255.     DIM subRadius AS LONG, RadiusError AS LONG
  256.     DIM X AS LONG, Y AS LONG
  257.  
  258.     subRadius = ABS(R)
  259.     RadiusError = -subRadius
  260.     X = subRadius
  261.     Y = 0
  262.  
  263.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  264.  
  265.     ' Draw the middle span here so we don't draw it twice in the main loop,
  266.     ' which would be a problem with blending turned on.
  267.     LINE (CX - X, CY)-(CX + X, CY), , BF
  268.  
  269.     WHILE X > Y
  270.         RadiusError = RadiusError + Y * 2 + 1
  271.         IF RadiusError >= 0 THEN
  272.             IF X <> Y + 1 THEN
  273.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  274.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  275.             END IF
  276.             X = X - 1
  277.             RadiusError = RadiusError - X * 2
  278.         END IF
  279.         Y = Y + 1
  280.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  281.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  282.     WEND
  283.  
  284. FUNCTION min (a, b)
  285.     IF a > b THEN min = b ELSE min = a
  286.  
  287. FUNCTION max (a, b)
  288.     IF a > b THEN max = a ELSE max = b
  289.  
  290. SUB drwbtn (x, y, s$)
  291.     th = 16: tw = LEN(s$) * 8
  292.     rgb 0
  293.     recf x, y, x + 200, y + 50
  294.     rgb 999
  295.     recf x, y, x + 198, y + 48
  296.     rgb 666
  297.     recf x + 2, y + 2, x + 198, y + 48
  298.     xoff = 100 - tw \ 2: yoff = 25 - th \ 2
  299.     COLOR _RGB(0, 0, 0), _RGB32(171, 171, 171)
  300.     _PRINTSTRING (x + xoff, y + yoff), s$
  301.     COLOR _RGB(0, 0, 0), _RGB(0, 0, 0)
  302.  
  303. SUB makeStars
  304.     FOR i = 0 TO ns
  305.         sx(i) = RND * (xmax - 16) + 8
  306.         sy(i) = RND * (ymax - 96) + 8
  307.         r = RND
  308.         IF r < .8 THEN
  309.             sr(i) = 1
  310.         ELSEIF r < .95 THEN
  311.             sr(i) = 2
  312.         ELSE
  313.             sr(i) = 3
  314.         END IF
  315.         sc&(i) = _RGB32(RND * 74 + 180, RND * 74 + 180, RND * 74 + 180)
  316.     NEXT
  317.  
  318. SUB makeTerra
  319.     FOR x = 4 TO xmax - 5
  320.         IF x > 5 AND RND < 0.06 THEN
  321.             xstop = min(xmax - 5, x + 50)
  322.             FOR lz = x TO xstop
  323.                 terraH(lz) = y
  324.                 c = INT(RND * 3) + 1
  325.                 terraC(lz) = c
  326.             NEXT
  327.             x = lz - 1
  328.         ELSE
  329.             xstop = min(xmax - 5, x + RND * 25)
  330.             IF RND < .5 THEN yd = 1 ELSE yd = -1
  331.             yd = yd * RND * 2
  332.             FOR xx = x TO xstop
  333.                 y = min(ymax - 90, y + yd)
  334.                 y = max(y, ymax - 240)
  335.                 terraH(xx) = y
  336.                 c = INT(RND * 2) + 1
  337.                 terraC(xx) = c
  338.             NEXT
  339.             x = xx - 1
  340.         END IF
  341.     NEXT
  342.  

2nd EDIT to fix the reasons for crash report.
3rd EDIT need ABS(dx) in crash report because could be moving too fast in minus direction.
« Last Edit: June 04, 2018, 11:19:53 AM by bplus »

Offline roadbloc

  • Newbie
  • Posts: 7
  • I am me.
    • roadbloc
Re: Lander project
« Reply #1 on: June 04, 2018, 07:16:55 AM »
This is neat! Took quite a few goes before I managed to do it. I got an error on line 130 whenever I got too close to the edge of the window. :)
Loading Signature....

Offline johnno56

  • Forum Resident
  • Posts: 999
  • Live long and prosper.
Re: Lander project
« Reply #2 on: June 04, 2018, 09:17:47 AM »
This is pretty cool.. First time I tried it, I landed as slow as I could, then a 'restart' button pops up. I figured I landed ok because I didn't see any of the 'crash' type comments. Usually, when I play 'lander-type' games, my ship hits the ground, bounces a little, then scatters itself across the terrain... lol Landing good. Crashing bad. Still a cool game... Well done!

J

ps: Just noticed the messages popping up on the Title Bar... Hey. I did land it ok... Who woulda thought?
« Last Edit: June 04, 2018, 09:24:07 AM by johnno56 »
Logic is the beginning of wisdom.

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: Lander project
« Reply #3 on: June 04, 2018, 09:33:50 AM »
Hi roadbloc,

This edit should fix the boundary problem (right after Lander vx, vy updated and Lander drawn in main loop):
Code: QB64: [Select]
  1.     IF vx < 30 OR vx > xmax - 30 OR vy < -50 THEN 'edit keep Lander legs inside boundries of terraH()
  2.         _TITLE "You have drifted off screen. Press p to play again..."
  3.         EXIT WHILE
  4.     END IF
  5.  
I will change it in OP (original post). When I changed picture window and TerraH(), I forgot to update this part.
Thanks for your report and interest. (OK changed.)

Hi Johnno,

Glad you found the message center, way up top. Yikes!
Good, you are landing OK so maybe one you can win at? ;-))

Maybe points given for amount of fuel left?

And a Text Box for messages nearer the controls, maybe under the fuel gage.

Hint: the arrow keys (left right and up) work a little better than clicking buttons but with buttons it should work with touch screens.

update: my touch screen not very responsive to my button "clicks".
« Last Edit: June 04, 2018, 09:55:10 AM by bplus »

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: Lander project
« Reply #4 on: June 04, 2018, 10:33:24 AM »
Found another bug due to last minute changes before post.


I was working from a list of reasons for crash, building up a report of all things wrong, that worked fine.

But when you run out of gas, all the other reasons don't make sense, so I tried to isolate that one reason with the remainder list. I used ELSEIF and meant ELSE here is the fix, that I will also use to edit OP:
Code: QB64: [Select]
  1.         IF fuel <= 0 THEN
  2.             crash$ = crash$ + "Ran out of fuel. "
  3.         ELSE
  4.             IF vda <> 270 THEN crash$ = crash$ + "Vehicle not upright. "
  5.             IF dy > 4 THEN crash$ = crash$ + "Came down too fast. "
  6.             IF dx > 4 THEN crash$ = crash$ + "Still moving hoizontally too fast. "
  7.             IF terraH(vx - 10) <> terraH(vx + 10) THEN crash$ = crash$ + "Did not land on level site. "
  8.         END IF
  9.  

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: Lander project
« Reply #5 on: June 04, 2018, 11:23:46 AM »
Dang, another one. In crash report, it is suppose to be a crash if you moving too fast in either direction horizontally (the dx variable) correction made in 3rd edit of OP.

Offline Petr

  • Forum Resident
  • Posts: 1558
  • The best code is the DNA of the hops.
Re: Lander project
« Reply #6 on: June 04, 2018, 11:47:58 AM »
Nice work, Bplus!

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: Lander project
« Reply #7 on: June 04, 2018, 12:00:28 PM »
Thanks Petr,

I am hoping to get it going with InForm beta 7. I am hoping the buttons will work better, be more responsive to clicks. Problems are either over rotate or not rotate at all or thruster jams a bit, some of the time. Probably could be tweaked in regular code? The arrow keys work great!

Offline johnno56

  • Forum Resident
  • Posts: 999
  • Live long and prosper.
Re: Lander project
« Reply #8 on: June 04, 2018, 05:08:14 PM »
Bplus,

May I suggest that the 'controls' be 'not so responsive'? Just like accelerating an object, then release the thrust, then 'gradually' slow down. It may add to the 'realism' and possibly the difficulty... Hmm... Nuts! More difficult means I might crash more... How about some sound effects instead? lol

J

ps: Thought of this AFTER I logged out... Joystick. Does QB64 interface with a joystick? I know it sounds nuts, and most people may not have a 'stick', but it would be so cool... My suggestion is a wee bit selfish as my poor joystick has been gathering dust for quite some time... Fight sims of ANY kind do not pass by very often... lol
« Last Edit: June 04, 2018, 05:45:53 PM by johnno56 »
Logic is the beginning of wisdom.

Offline Petr

  • Forum Resident
  • Posts: 1558
  • The best code is the DNA of the hops.
Re: Lander project
« Reply #9 on: June 04, 2018, 05:43:16 PM »
Hi Johnno56,
 yes joystick IS supported. BUT i never try it because have none. But: STICK function return joystick coordinates, STRIG(0) return if is fire pressed, for initializing use ON STRIG GOSUB....
Hi Bplus,
For delay after mouse operations - limit it with TIMER in this way: IF _MOUSEBUTTON(1) AND TimeLimit > TIMER THEN TimeLimit = TIMER + .01 : your action

Offline johnno56

  • Forum Resident
  • Posts: 999
  • Live long and prosper.
Re: Lander project
« Reply #10 on: June 04, 2018, 05:48:57 PM »
Thanks Petr. I will check it out...

J
Logic is the beginning of wisdom.

Offline johnno56

  • Forum Resident
  • Posts: 999
  • Live long and prosper.
Re: Lander project
« Reply #11 on: June 04, 2018, 06:47:15 PM »
I popped in the stick() and strig() commands and they worked. A little too well. x/y controls were 'very' sensitive. (a little 'heavy handed one time and the ship spun like a top... lol) Strig() worked fine. One click. One thruster burn... Took me 3 attempts before I got the hang of using a joystick... I think I might stick with the cursor keys... lol

J
Logic is the beginning of wisdom.

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: Lander project
« Reply #12 on: June 04, 2018, 07:24:31 PM »
Hi Petr,

Yes I have used the delay trick many a time, not just QB64, it also will likely help Johnno with his joystick too.

I don't think a timer need be involved, just a _DELAY x might do?

I am saving that problem for later and want to see performance in InForm'd system. I have the form mapped out and plan on adding code tonight.

Hi Fellippe,

I do have a question. Under the Edit Menu of UiEditor, I found this (see attached) should I use Windows option if I have Windows?

Offline FellippeHeitor

  • QB64 Developer
  • Forum Resident
  • Posts: 2825
  • Let it go, this too shall pass.
    • QB64.org
Re: Lander project
« Reply #13 on: June 04, 2018, 09:14:16 PM »
Use codepage 437 for English-only programs; use codepage Windows-1252 for any programs that need to display diacritic marks, like "maçã" or something like that. I basically save my forms with codepage 1252 when I write programs in Portuguese (not related to the target operating system, despite its name - see more here: https://msdn.microsoft.com/en-us/library/cc195054.aspx).

I mention (and demonstrate) that in the video for Beta 6. See it at ~2:33: https://youtu.be/S4d5nmu_gjQ?t=153
« Last Edit: June 04, 2018, 09:17:56 PM by FellippeHeitor »

Offline bplus

  • Forum Resident
  • Posts: 6303
  • What could possibly go wrong?
Re: Lander project
« Reply #14 on: June 05, 2018, 01:30:02 AM »
Still have to rework numbers but basic program is working under InForm.

And! as I suspected the response to button clicking was accurate, no jamming. I have included some notes in comments in .bas file.