Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : lunar lander revisit
Author | Message | ||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2127 |
Hi, there was a mention of lunar lander on the forum and I had a go at coding it vga 640x320. Like most games it's not as easy as it looks. I'm stuck with gravity and trust vs gravity and left right momentum. I got so far as the graphics and keys. A joint effort to make picomite vga loony lander? it was this prog and var% that caused the bother. Any advice welcome to make pico lander. 'lunar lander vga 640x480 OPTION EXPLICIT ' OPTION BASE 1 option autorun on 'do 'print asc(inkey$); ' 200 ' 'loop ' dim integer lx(43)=(0,5,9,13,3,18,25,30,48,53,57,47,32,11,5,9,57,89,108,149,170,220,262,300,320,400,447,493,510,545,572,600,638,605,590,577,560,513,495,470,457,500,639) ' 1 5 10 15 20 25 30 35 dim integer ly(43)=(0,7,15,25,40,70,100,130,130,160,193,253,298,346,420,430,430,400,370,457,450,400,440,466,424,400,460,460,452,403,373,320,240,210,190,170,130,120,137,110,60,26,0) ' 1 5 10 15 20 25 30 35 dim integer l,o,fno,fnc,spnum,k dim lander_x,lander_y,gravity,inertia lander_x=260:lander_y=10:gravity=0.1:inertia=0 ' mode 1 cls ' sprites 'set up lander sprites text 170,180,"d shift s shift f s f" for l=1 to 6 sprite WRITE l,l*80,200 NEXT ' 'pause 5000 text 200,120,"Press any key to start" do loop until inkey$<> "" 'end FRAMEBUFFER CREATE F FRAMEBUFFER WRITE f cls RGB(black) Line GRAPH lx(),ly()'draw landscape ' spnum=1 do k = asc(inkey$) if k then select case k Case 100 'd up spnum=2 'lander_y=lander_y-gravity:gravity=gravity-gravity/200 Case 115 's left spnum=5 'lander_y=lander_y-gravity:gravity=gravity-gravity/500 'lander_x=lander_x+-inertia 'inertia=inertia-inertia/500 Case 102 'f right spnum=6 'lander_y=lander_y-gravity:gravity=gravity-gravity/500 'lander_x=lander_x+inertia 'inertia=inertia+inertia/500 case 83 'shift s up/right spnum=3 'lander_y=lander_y-gravity:gravity=gravity-gravity/200 'lander_x=lander_x+inertia 'inertia=inertia+inertia/500 case 70 'shift f up/left spnum=4 'lander_y=lander_y-gravity:gravity=gravity-gravity/200 'lander_x=lander_x-inertia 'inertia=inertia-inertia/500 end select else lander_y=lander_y+gravity:gravity=gravity+gravity/500 endif ' inc fnc: if fnc=20 then fnc=0:fno=not fno:spnum=1'which sprite for animation if fno=0 then'draw noflame lander box lander_x,lander_y,30,39,,0,0 sprite WRITE 1,lander_x,lander_y else'draw flame sprite box lander_x,lander_y,30,39,,0,0 sprite WRITE spnum,lander_x,lander_y end if framebuffer COPY f,N pause 10 loop end ' sub sprites ship sprite READ 1,86,88,28,39'no flame ship triangle 96,114,100,126,104,114,1'flame straight sprite READ 2,86,88,28,39 ship triangle 96,114,100,126,104,114,1'flame straight triangle 107,107,113,110,107,112,1'right thrust sprite READ 3,86,88,28,39 ship triangle 96,114,100,126,104,114,1'flame straight triangle 92,107,86,110,92,113,1'left thrust sprite READ 4,86,88,28,39 ship triangle 107,107,113,110,107,112,1'right thrust sprite READ 5,86,88,28,39 ship triangle 92,107,86,110,92,113,1'left thrust sprite READ 6,86,88,28,39 cls end sub ' sub ship'draw ship cls circle 100,98,8 box 92,106,16,8 line 92,114,88,126 line 107,114,111,126 line 86,126,90,126 line 109,126,113,126 end sub sub explode for inertia=1 to 20 circle lander_x+15,lander_y+10,inertia,1 pause 20 next for inertia=1 to 20 circle lander_x+15,lander_y+10,inertia, pause 20 next end sub Edited 2024-03-06 05:23 by stanleyella |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4228 |
Hi Stanleyella, For Flappy Bird I emulated gravity. When you assume gravity within your view window a constant factor (on earth 1G) then the impact on speed of an object is also a constant. When you assume no friction from air (lunar lander), the math becomes very simple. Any movement can be split up in a vertical and horizontal component. The horizontal component will only change by thrust of the engine. Lets assume the horizontal velocity is 3m/s, and a thrust boost has a horizontal component of -2m/s, the resulting speed will be 3-2=1m/s forever... The vertical component is influenced by 2 parameters: the thrust (similar as before the horizontal thrust vector). But secondly, there will be a constant pull downwards to the surface of the moon. In Flappy Bird, I play with the vertical velocity as follows. v=0 'vertical velocity at start do 'game loop if thrust then v=8 'vertical component ,this can be adjusted for optimal game play height=height+v v=v-1 'gravity constantly lowers rising speed, and increases falling speed loop In case of a bird, you can assume it rises with each wing movement, the lunar lander can be in any position, so it thruse could also increase the downward speed towards the moon surface. If angle is angle in radians of lander versus surface (0 = perpendicular). v=thrust*cos(angle) 'vertical component h=thrust*sin(angle) 'horizontal component Regards, Volhout PicomiteVGA PETSCII ROBOTS |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2127 |
Hi Volhout. I looked at picorocks for inertia but "decays". lander would be "gravity increasing", left or right "thrust momentum" keep constant and opposite thrust reduces lateral x momentum. I want only 1 pixel movement max so floats useful. only one sprite to consider I'll revisit my code. ps birds fly with no thrust most of the time :) Edited 2024-03-08 08:48 by stanleyella |
||||
vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1109 |
I've been messing with moonscape data from the original 1973 LUNARLANDER by Jack Burness. Visit here for more information. With much array manipulation, I've got the following demo working for the PicoMiteVGA. This uses a Wii controller to zoom and shift the terrain. (Right joystick up/down for zoom, left joystick left and right.) PicoLander02.zip Visit Vegipete's *Mite Library for cool programs. |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4228 |
Hi Pete, Works nice... You can zoom and scroll with the controller. Volhout PicomiteVGA PETSCII ROBOTS |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2127 |
no controller . unzipped. check again later. got to sort my lander gravity,got left right inertia drifting working. brain ache, keyboard not ideal it seems. stan |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2127 |
I finally got a simple demo to "work", vga 640x480. uses keyboard but bodged for no key pressed but a key was. fuel runs out so you crash. only 1 land scape and 2 flat landing areas. just a simple first demo if of any interest bugs like fuel<0 and odd lander just explodes no reason. 'lunar lander vga 640x480 PicoMiteVGA MMBasic USB Edition 5.09.00b5 OPTION EXPLICIT OPTION BASE 1 option autorun on 'landscape data dim integer lx(43)=(0,5,9,13,3,18,25,30,48,53,57,47,32,11,5,9,57,89,108,149,170,220,262,300,320,400,447,493,510,545,572,600,638,605,590,577,560,513,495,470,457,500,639) dim integer ly(43)=(9,17,15,25,40,70,100,130,130,160,193,253,298,346,420,430,430,400,370,457,450,400,440,466,424,400,460,460,452,403,373,320,240,210,190,170,130,120,137,110,60,26,9) dim integer l,o,fno,fnc,spnum,k,spc dim lander_x,lander_y,last_lander_x,last_lander_y,gravity,inertia,antigravity,fuel lander_x=260:lander_y=10:gravity=0.0001:inertia=0:fuel=100 ' mode 1 cls ' sprites 'set up lander sprites text 170,180,"d g a s f" 'keys for l=1 to 6 sprite WRITE l,l*80,200 'draw all sprites NEXT ' text 210,120,"Press any key to start" do:loop until inkey$<> "" ' FRAMEBUFFER CREATE F FRAMEBUFFER WRITE f cls RGB(black) Line GRAPH lx(),ly()'draw landscape ' text 0,0,"FUEL" spnum=1 '-------------------------------------- do if fuel>0 then text 48,0,str$(fuel)," " k = asc(inkey$) if k then 'key presses spc=20 'set counts until no flame select case k Case 100 'd up spnum=2 gravity=gravity-.005 inc fuel,-.8 Case 115 's left spnum=5 inertia=inertia-0.005 inc fuel,-.4 Case 102 'f right spnum=6 inertia=inertia+0.005 inc fuel,-.4 case 103 'g up/right spnum=4 inertia=inertia+0.005 gravity=gravity-.005 inc fuel,-1.2 case 97 'a up/left spnum=3 inertia=inertia-0.005 gravity=gravity-.005 inc fuel,-1.2 end select endif end if ' last_lander_x=lander_x:last_lander_y=lander_y lander_y=lander_y+gravity lander_x=lander_x+inertia gravity=gravity+.00015 ' 'has lander landed, check 2 pixels under each foot all lit if pixel(lander_x,lander_y+40)=rgb(white) then if pixel(lander_x+3,lander_y+40)=rgb(white) then if pixel(lander_x+23,lander_y+40)=rgb(white) then if pixel(lander_x+27,lander_y+40)=rgb(white)then text 240,150, "Landed": framebuffer COPY f,N:end'-------- end if end if end if end if ' 'has lander hit landscape, check pixels around it, any lit crashed o= pixel(lander_x,lander_y)+ pixel(lander_x,lander_y+10)+ pixel(lander_x,lander_y+20)+ pixel(lander_x,lander_y+30)+ pixel(lander_x,lander_y+40)+ pixel(lander_x+29,lander_y) o=o+pixel(lander_x+29,lander_y+10)+pixel(lander_x+29,lander_y+20)+pixel(lander_x+29,lander_y+30)+pixel(lander_x+29,lander_y+40)+pixel(lander_x+7,lander_y) o=o+ pixel(lander_x+14,lander_y)+ pixel(lander_x+21,lander_y)+ pixel(lander_x+7,lander_y+39)+ pixel(lander_x+14,lander_y+39)+ pixel(lander_x+21,lander_y+39) if o>0 then explode:end'-------- ' inc spc,-1:if spc <1 then spnum=1 'no flame if no nav key pressed inc fnc: if fnc=10 then fnc=0:fno=not fno 'which lander for animation if spc>0 box last_lander_x,last_lander_y,30,39,,0,0 'erase lander if fno=0 then sprite WRITE 1,lander_x,lander_y 'draw noflame lander else sprite WRITE spnum,lander_x,lander_y 'draw flame lander if nav key pressed end if ' framebuffer COPY f,N pause 10 loop end ' sub sprites ship sprite READ 1,86,88,28,39'no flame ' ship triangle 96,114,100,126,104,114,1'flame straight sprite READ 2,86,88,28,39 ' ship triangle 96,114,100,126,104,114,1'flame straight triangle 107,107,113,110,107,112,1'right thrust sprite READ 3,86,88,28,39 ' ship triangle 96,114,100,126,104,114,1'flame straight triangle 92,107,86,110,92,113,1'left thrust sprite READ 4,86,88,28,39 ' ship triangle 107,107,113,110,107,112,1'right thrust sprite READ 5,86,88,28,39 ' ship triangle 92,107,86,110,92,113,1'left thrust sprite READ 6,86,88,28,39 cls end sub ' sub ship'draw ship cls circle 100,98,8 box 92,106,16,8 line 92,114,88,126 line 107,114,111,126 line 86,126,90,126 line 109,126,113,126 end sub ' sub explode for o=1 to 24 circle lander_x+15,lander_y+20,o,1,1,rgb(white) framebuffer COPY f,N pause 20 next for o=1 to 24 circle lander_x+15,lander_y+20,o,1,1,rgb(black) framebuffer COPY f,N pause 20 next end sub Edited 2024-03-25 08:51 by stanleyella |
||||
Print this page |