Moon Lander Game

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: http://www.swissmicros.com/dm42/decoder/

You can then copy/paste the listing and post it in "code" tags.
deetee
Posts: 27
Joined: Mon Jul 22, 2019 6:01 am
Location: Vienna

Moon Lander Game

Hello all!

I made some kind of "Moon Lander Game" with simple graphics.
The distance from the left edge corresponds to the droping velocity:
start.bmp (12.31 KiB) Viewed 5612 times

Try to land in the lower left corner by burning (up to 20) retrorockets (0...9 per step):
landing.bmp (12.31 KiB) Viewed 5612 times

My best landing was with 2 retrorockets left.
This is the code:

Code: Select all

``````00 { 582-Byte Prgm }
01▸LBL "moon" @ MOON LANDER

@ Burn retrorockets due to keypress (0...9)
@
@ Display (Lander, retrorockets, velocity, height):
@
@  |<-- v -->|
@   ______________________
@  |         |            |
@  |         |            |
@  |---------L-----rrrrrr-|
@  |    ^    |            |
@  |    |    |            |
@  |    |    |            |
@  |    h    |            |
@  |    |    |            |
@  |    |    |            |
@  *____v____|____________|
@  ^
@  |___Try to
@      land here!
@
@ Simulation formulae:
@  Retrorockets r = r - burn (r0 = 20)
@  Acceleration a = burn - 1 (gravity = 1)
@  Height       h = h + v*t + a/2*t^2 (h0 = 100)
@  Velocity     v = v + a*t (v0 = -10)
@  Time         t = 1 per simulation step
@
@ End of game when: h <= 1
@
@ Success (hit) if: |h|<=1 AND |v|<=1
@

02 CLLCD
03 2 @ Set resolution to 200x120
04 STO "GrMod"
05 100
06 STO "h" @ Height
07 -10
08 STO "v" @ Velocity
09 20
10 STO "r" @ Retrorockets

11▸LBL 99 @ Display
12 120 @ Lander
13 RCL "h"
14 -
15 +/-
16 RCL "v"
17 6
18 ×
19 PIXEL @ Lander cross
20 CLA
21 223
22 XTOA
23 ├"1÷?"
24 XTOA
25 R↓
26 +/-
27 2
28 -
29 X<>Y
30 +/-
31 3
32 -
33 X<>Y
34 AGRAPH @ Lander sprite
35 ".?." @ Retrorockets
36 RCL "r"
37 X=0? @ No rockets left
38 GTO 98
39 1ᴇ3
40 ÷
41 1
42 +
43 STO "i" @ Loop counter
44▸LBL 44
45 120
46 RCL "h"
47 -
48 2
49 -
50 200
51 RCL "i"
52 IP
53 4
54 ×
55 -
56 AGRAPH
57 ISG "i"
58 GTO 44

59▸LBL 98
60 GETKEY @ Menu keypress for burn (0...9)
61 34 @ 0
62 X=Y?
63 GTO 01
64 R↓
65 29 @ 1
66 X=Y?
67 GTO 02
68 R↓
69 30 @ 2
70 X=Y?
71 GTO 03
72 R↓
73 31 @ 3
74 X=Y?
75 GTO 04
76 R↓
77 24 @ 4
78 X=Y?
79 GTO 05
80 R↓
81 25 @ 5
82 X=Y?
83 GTO 06
84 R↓
85 26 @ 6
86 X=Y?
87 GTO 07
88 R↓
89 19 @ 7
90 X=Y?
91 GTO 08
92 R↓
93 20 @ 8
94 X=Y?
95 GTO 09
96 R↓
97 21 @ 9
98 X=Y?
99 GTO 10

100▸LBL 01 @ Set burn due to keypress
101 CLX
102 GTO 88
103▸LBL 02
104 1
105 GTO 88
106▸LBL 03
107 2
108 GTO 88
109▸LBL 04
110 3
111 GTO 88
112▸LBL 05
113 4
114 GTO 88
115▸LBL 06
116 5
117 GTO 88
118▸LBL 07
119 6
120 GTO 88
121▸LBL 08
122 7
123 GTO 88
124▸LBL 09
125 8
126 GTO 88
127▸LBL 10
128 9

129▸LBL 88
130 STO "b" @ Burn

131 RCL "r" @ Calculate retrorockets r = r - b  (>=0)
132 X>Y? @ r > b
133 GTO 87
134 STO "b" @ b = r
135▸LBL 87
136 RCL "b"
137 STO- "r"
138 RCL "b" @ Calculate acceleration a = b - 1
139 1
140 -
141 STO "a" @ acceleration
142 2 @ Calculate height h = h + v + a/2
143 ÷
144 RCL "v"
145 +
146 RCL "h"
147 +
148 STO "h"
149 RCL "v" @ Calculate velocity v = v - a
150 RCL "a"
151 +
152 STO "v"

153 1 @ Still to high (x > 1)
154 RCL "h"
155 X>Y?
156 GTO 99

157 ABS @ HIT or FAIL?
158 1
159 X<Y? @ |h| > 1 ...
160 GTO 66
161 RCL "v"
162 ABS
163 1
164 X<Y? @ ... and |v| > 1 ... then fail
165 GTO 66

166 "~" @ Hit
167 255
168 XTOA
169 129
170 XTOA
171 247
172 XTOA
173 XTOA
174 129
175 XTOA
176 255
177 XTOA
178 129
179 XTOA
180 255
181 XTOA
182 253
183 XTOA
184 129
185 XTOA
186 253
187 XTOA
188 255
189 XTOA
190 ├"~"
191 5
192 183
193 AGRAPH
194 GETKEY
195 AVIEW
196 CLLCD
197 GTO 55

198▸LBL 66 @ Fail
199 "~"
200 255
201 XTOA
202 129
203 XTOA
204 245
205 XTOA
206 245
207 XTOA
208 253
209 XTOA
210 255
211 XTOA
212 129
213 XTOA
214 237
215 XTOA
216 237
217 XTOA
218 129
219 XTOA
220 255
221 XTOA
222 129
223 XTOA
224 255
225 XTOA
226 129
227 XTOA
228 191
229 XTOA
230 191
231 XTOA
232 255
233 XTOA
234 ├"~"
235 5
236 178
237 AGRAPH
238 GETKEY
239 AVIEW
240 CLLCD

241▸LBL 55 @ Exit
242 CLV "a"
243 CLV "i"
244 CLV "v"
245 CLV "h"
246 CLV "r"
247 CLV "b"``````
Or load the raw-file directly:
moon.raw