-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbouncingball.asm
361 lines (282 loc) · 5.79 KB
/
bouncingball.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
;; Copyright (c) 2014 Tiina Romu and Heidi Lehtevä
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
;; Variables
default_colour equ PAPER_BLACK+INK_GREEN+BRIGHT
end_colour equ PAPER_BLACK+INK_GREEN+BRIGHT+FLASH
loop_counter db 0
direction_y db 1
direction_x db 1
string:
db 'Awesome!',0
bouncingball:
;; Clear screen before doing anything else
call ClearScreen
;; Border colour
ld a, INK_BLACK
out ($fe),a
;; We want to set backgroung colour
;; we have 768 bytes and we start from address pointer $5800
ld hl,$5800
ld bc,768
background_loop:
;; Load the colour we want to the 8x8 pixel cell
ld (hl),default_colour
;; Increase hl and decrease bc
inc hl
dec bc
;; Check if bc is completely 0
ld a,b
or c
;; Continue loop if not 0
jr nz,background_loop
call DrawHome
;; set value needed in draw-loop
ld b,3
ld c,3
draw_loop:
;; Clear previous pixel drawn and change coordinates
call ClearBall
ld a, (direction_x)
ld h, a
ld a, c
add a, h
ld c, a
ld a, (direction_y)
ld h, a
ld a, b
add a, h
ld b, a
;; Stop loop after
ld a, (loop_counter)
cp 10 ;; max bumbs
jr z, FinishLoop
;; Check if Y is over 191
ld a,b
cp 189
jr z, SetValueYToMax
;; Check if Y is less than 0
ld a,b
cp 2
jr z, SetValueYToZero
;; Check if X is over 255
ld a,c
cp 253
jr z, SetValueXToMax
;; Check if Y is less than 0
ld a,c
cp 2
jr z, SetValueXToZero
;; Draw ball to screen
call DrawBall
push bc
;; Wait a while before moving ball
ld b,1
halt
djnz $-1
pop bc
jr draw_loop
;; Our very own functions to set values to X and Y when we meet borders
FinishLoop ld a, INK_BLACK
out ($fe),a
ld hl, string
ld b, 10
ld c, 12
call PutString
ld hl,$5800
ld bc,768
jp EndLoop
DrawHome ld a,%11111100
ld ($400F),a
ld ($410F),a
ld ($420F),a
ld ($430F),a
ld a,%00111111
ld ($400E),a
ld ($410E),a
ld ($420E),a
ld ($430E),a
ld a,%11111111
ld ($440F),a
ld ($440E),a
ld ($450F),a
ld ($450E),a
ret
;; Load the colour we want to the 8x8 pixel cell
EndLoop ld (hl),end_colour
;; Increase hl and decrease bc
inc hl
dec bc
;; Check if bc is completely 0
ld a,b
or c
;; Continue loop if not 0
jr nz, EndLoop
ret
SetValueYToMax ld a, 255
ld (direction_y), a
ld a, INK_MAGENTA
out ($fe),a
jp IncreaseCounter
SetValueYToZero ld a, 1
ld (direction_y), a
ld a, INK_BLUE
out ($fe),a
jp IncreaseCounter
SetValueXToMax ld a, 255
ld (direction_x), a
ld a, INK_CYAN
out ($fe),a
jp IncreaseCounter
SetValueXToZero ld a, 1
ld (direction_x), a
ld a, INK_YELLOW
out ($fe),a
jp IncreaseCounter
Draw5Pixels call SetPixel
push bc
;; y=2
ld a, b
add a, 2
ld b, a
call SetPixel
;; y=1
ld a, b
add a, -1
ld b, a
call SetPixel
;; y=-1
ld a, b
add a, -2
ld b, a
call SetPixel
;; y=-2
ld a, b
add a, -1
ld b, a
call SetPixel
pop bc
ret
Draw3Pixels call SetPixel
push bc
;; y=1
ld a, b
add a, 1
ld b, a
call SetPixel
;; y=-1
ld a, b
add a, -2
ld b, a
call SetPixel
pop bc
ret
Clear5Pixels call ClearPixel
push bc
;; y=2
ld a, b
add a, 2
ld b, a
call ClearPixel
;; y=1
ld a, b
add a, -1
ld b, a
call ClearPixel
;; y=-1
ld a, b
add a, -2
ld b, a
call ClearPixel
;; y=-2
ld a, b
add a, -1
ld b, a
call ClearPixel
pop bc
ret
Clear3Pixels call ClearPixel
push bc
;; y=1
ld a, b
add a, 1
ld b, a
call ClearPixel
;; y=-1
ld a, b
add a, -2
ld b, a
call ClearPixel
pop bc
ret
DrawBall call Draw5Pixels
push bc
;; x=1
ld a, c
add a, 1
ld c, a
call Draw5Pixels
;; x=-1
ld a, c
add a, -2
ld c, a
call Draw5Pixels
;; x=-2
ld a, c
add a, -1
ld c, a
call Draw3Pixels
;; x=2
ld a, c
add a, 4
ld c, a
call Draw3Pixels
pop bc
ret
ClearBall call Clear5Pixels
push bc
;; x=1
ld a, c
add a, 1
ld c, a
call Clear5Pixels
;; x=-1
ld a, c
add a, -2
ld c, a
call Clear5Pixels
;; x=-2
ld a, c
add a, -1
ld c, a
call Clear3Pixels
;; x=2
ld a, c
add a, 4
ld c, a
call Clear3Pixels
call DrawHome
pop bc
ret
IncreaseCounter ld a, (loop_counter)
ld h, a
inc h
ld a, h
ld (loop_counter), a
jp draw_loop