-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.inc
734 lines (430 loc) · 11.4 KB
/
funcs.inc
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
INCLUDE \masm32\include\winmm.inc
INCLUDELIB \masm32\lib\winmm.lib
;Inclde libraries for sound
.CODE
startPos PROC
;Start of set starting positions function
MOV player1.pos.x, playerW * 2
MOV player1.pos.y, playerMid
MOV player2.pos.x, xlen - (playerW * 3)
MOV player2.pos.y, playerMid
MOV ball.pos.x, ballMidX
MOV ball.pos.y, ballMidY
;Starting positions for all images
RET
startPos ENDP
gameMenu PROC
;Start of main menu function
loopb:
PUSHA
INVOKE drd_processMessages
INVOKE drd_imageDraw, OFFSET BG1.info, BG1.pos.x, BG1.pos.y
INVOKE drd_imageDraw, OFFSET BG2.info, BG2.pos.x, BG2.pos.y
INVOKE drd_imageDraw, OFFSET menu.info, menu.pos.x, menu.pos.y
INVOKE drd_flip
;Draw the main menu
INVOKE GetAsyncKeyState, space
CMP AX, 0
JNE startGame
;Check if space is entered
POPA
JMP loopb
startGame:
;Exits the function and starts the game
POPA
RET
gameMenu ENDP
bgcmp PROC adrObj:DWORD, ballObj:DWORD
;Start of background comparing function
PUSHA
MOV EAX, adrObj
MOV ECX, ballObj
;Move addresses of the objects into registers
MOV ECX, [ECX + 8]
MOV [EAX + 8], ECX
;Put the X direction of the ball in the background object
MOV EBX, xlen
;Put the length of the window in a register
CMP [EAX], EBX
JE bgmv
NEG EBX
CMP [EAX], EBX
JE bgmv
;Check if the background object fully went out of the windows in either sides
POPA
RET
bgmv:
NEG EBX
MOV [EAX], EBX
;Put the background object on the opposite side of the window for scrolling effect
POPA
RET
bgcmp ENDP
pauseGame PROC
;Start of game pausing function
PUSHA
INVOKE GetAsyncKeyState, PKEY
CMP AX, 0
JNE doIt
;Jump to doIt if the P key is pressed
JMP skipTimeout
;Jump to skipTimeout if we don't want to pause the game
doIt:
INVOKE Sleep, 200
;Timeout for the user to release the key
POPA
;Making sure any registers don't get bamboozled in the process
check:
PUSHA
INVOKE drd_processMessages
;Make sure we get key inputs
INVOKE GetAsyncKeyState, PKEY
CMP AX, 0
JNE unPauseGame
;Unpause the game if p is pressed again
POPA
JMP check
unPauseGame:
INVOKE Sleep, 200
;Unpressing timeout
skipTimeout:
;Skip the unpressing timeout if we didn't pause the game
POPA
;If it's quirky but it works clap your hands
RET
pauseGame ENDP
moveObject PROC adrObj:DWORD
;Start of moving objects function
PUSHA
MOV ECX, adrObj
;Put the object address in a register
MOV EDX, [ECX + 8]
ADD [ECX], EDX
MOV EDX, [ECX + 12]
ADD [ECX + 4], EDX
;Take X direction and Y direction of the object and add them to the according position
POPA
RET
moveObject ENDP
moveWithKeys PROC adrObj:DWORD
;Start of moving an object with user input function
PUSHA
MOV ECX, adrObj
MOV EDX, [ECX + 16]
INVOKE GetAsyncKeyState, EDX
CMP AX, 0
JNE goUp
MOV ECX, adrObj
MOV EDX, [ECX + 20]
INVOKE GetAsyncKeyState, EDX
CMP AX, 0
JNE goDown
;Put upward and downward moving keys of the object in a register and check if they are pressed
jmp endIt
goUp:
MOV EAX, -1
MOV ECX, adrObj
MOV [ECX + 12], EAX
;Put -1 in the Y direction of the object so it will go upwards
JMP endIt
goDown:
MOV EAX, 1
MOV ECX, adrObj
MOV [ECX + 12], EAX
;Put 1 in the Y direction of the object so it will go downwards
endIt:
POPA
RET
moveWithKeys ENDP
stopMovement PROC adrObj:DWORD
;Start of stopping movement function
PUSHA
MOV ECX, adrObj
MOV EDX, 0
MOV [ECX + 12], EDX
;Reset the Y direction of the object so that it won't keep moving after a movement key is unpressed
POPA
RET
stopMovement ENDP
playerBorder PROC adrObj:DWORD
;Start of limiting the player within the window function
PUSHA
MOV ECX, adrObj
MOV EDX, 0
MOV EAX, yhitbox
;Put the edges of the window into registers
CMP [ECX + 4], EDX
JE xdontMove
CMP [ECX + 4], EAX
JE ydontMove
;Check if the player is hitting the border of the window
JMP theEnd
xdontMove:
INVOKE GetAsyncKeyState, [ECX + 16]
CMP AX, 0
JE theEnd
;Check if the player is still pressing the button to move in the direction of the same edge
JMP change
ydontMove:
INVOKE GetAsyncKeyState, [ECX + 20]
CMP AX, 0
JE theEnd
;Same as xdontMove but on the other edge
change:
MOV EDX, 0
MOV ECX, adrObj
MOV [ECX + 12], EDX
;Change the players direction to 0 so he can't keep moving in the same direction
theEnd:
POPA
RET
playerBorder ENDP
ballHitting PROC playerObj:DWORD, ballObj:DWORD
;Start of ball collision with players function
PUSHA
MOV EAX, playerObj
MOV EBX, ballObj
MOV ECX, [EAX]
ADD ECX, playerW
;Take X position of the player and add the width of the player image
CMP [EBX], ECX
JE p1col
;check if the X positions of the ball and player collide
MOV EDX, [EBX]
ADD EDX, ballBorder
;Same as second to last comment but with the ball object
CMP [EAX],EDX
JE p2col
;Same as the last compare but for the other player
JMP endIt
p1col:
MOV ECX, [EAX + 4]
SUB ECX, (ballBorder / 3)
;Take Y position of the player and subtract a bit of the balls height for expanding the hitbox a bit
CMP [EBX + 4],ECX
JGE y1isgood
;Check if the balls Y is bigger than the player Y
JMP endIt
y1isgood:
MOV EDX, [EAX + 4]
ADD EDX, (playerH + (ballBorder / 3))
;Take player Y and add to it the height of the player image plus the hitbox breathing room
CMP [EBX + 4], EDX
JLE p1finalSolution
;Check if the balls Y is smaller than the image of the player (plus the breathing room for the hitbox)
JMP endIt
p2col:
;All of this is pretty much the same as p1col but for the other side of the player
MOV ECX, [EAX + 4]
SUB ECX, (ballBorder / 3)
CMP [EBX + 4], ECX
JGE y2isgood
JMP endIt
y2isgood:
MOV EDX, [EAX + 4]
ADD EDX, playerH
ADD EDX, (ballBorder / 3)
CMP [EBX + 4], EDX
JLE p2finalSolution
JMP endIt
p1finalSolution:
MOV EDX, 0
CMP [EBX + 8], EDX
JG endIt
;Check if the X direction of the ball is different than thee side of the player we want to collide with
MOV ECX, [EBX + 8]
NEG ECX
MOV [EBX + 8], ECX
;Negate the X direction of the ball
JMP endIt
p2finalSolution:
;Same as p1finalSolution but for the other side
MOV EDX, 0
CMP [EBX + 8], EDX
JL endIt
MOV ECX, [EBX + 8]
NEG ECX
MOV [EBX + 8], ECX
endIt:
POPA
RET
ballHitting ENDP
ballToWall PROC adrObj:DWORD
;Start of ball collision with the celing and floor function
PUSHA
MOV EAX, adrObj
MOV EDX, 0
CMP [EAX + 4], EDX
JE hitWall
;Check if the ball hit the upper edge of the window
MOV EBX, ylen
SUB EBX, ballBorder
;Take the length of the window and subtract the length of the ball image
CMP [EAX + 4], EBX
JE hitWall
;Check if the ball hit the downward edge of the window
JMP goAway
hitWall:
MOV ECX, [EAX + 12]
NEG ECX
MOV [EAX + 12], ECX
;Negate the Y direction of the ball
goAway:
POPA
RET
ballToWall ENDP
scoring PROC ballObj:DWORD, p1Obj:DWORD, p2Obj:DWORD, p1Obj2:DWORD, p2Obj2:DWORD
;Start of score taking function
PUSHA
MOV EAX, ballObj
MOV EDX, 0
MOV EBX, 10
MOV ECX, xlen
SUB ECX, ballBorder
CMP [EAX], ECX
JE scoreP1
CMP [EAX], EDX
JE scoreP2
;Check if the ball hit either the right or the left edge of the screen
JMP plsGo
scoreP1:
INC p1Score
INC pScored
;Increase the score of player 1 and pScored for timeout
INVOKE startPos
;Reset game objects
MOV ECX, p1Obj
MOV EAX, p1Score
DIV EBX
;Divide the score by 10
CMP EDX, 0
JE addTenP1
;Check if modulo 10 of the score is 0
MOV EDX, scoreBorder
ADD [ECX + 44], EDX
;Move one number forward in the sprite sheet for the score
JMP plsGo
scoreP2:
;Same as scoreP1 but for the score of the other player
INC p2Score
INC pScored
INVOKE startPos
MOV ECX, p2Obj
MOV EAX, p2Score
DIV EBX
CMP EDX, 0
JE addTenP2
MOV EDX, scoreBorder
ADD [ECX + 44], EDX
JMP plsGo
addTenP1:
MOV EAX, p1Obj2
MOV EBX, scoreBorder
MOV EDX, 0
MOV [ECX + 44], EDX
ADD [EAX + 44], EBX
;Move forward in the sprite sheet for the second digit in the score and reset the first digit
JMP plsGo
addTenP2:
;Same as addTenP1 but for the score of the other player
MOV EAX, p2Obj2
MOV EBX, scoreBorder
MOV EDX, 0
MOV [ECX + 44], EDX
ADD [EAX + 44], EBX
plsGo:
POPA
RET
scoring ENDP
restartGame PROC p1:DWORD, p2:DWORD, p12:DWORD, p22:DWORD
;Start of restart game function
PUSHA
INVOKE GetAsyncKeyState, RKEY
CMP AX, 0
JNE ree
;Check if the R key is pressed
JMP goNow
ree:
INC pScored
;Puts a timeout after the restart
MOV p1Score, 0
MOV p2Score, 0
;Reset player scores
MOV EAX, p1
MOV EBX, p2
MOV ECX, 0
MOV [EAX + 44], ECX
MOV [EBX + 44], ECX
MOV EAX, p12
MOV EBX, p22
MOV [EAX + 44], ECX
MOV [EBX + 44], ECX
;Reset all score images to 0
INVOKE startPos
;Put all players back in their starting positions
INC restarted
;Increments restarted for letting the endGame function (below) know the game restarted
INVOKE PlaySound, NULL, NULL, SND_ASYNC
INVOKE PlaySound, OFFSET hddMusic, NULL, SND_ASYNC OR SND_LOOP
;Restart the music from the start
goNow:
POPA
RET
restartGame ENDP
endGame PROC
;Start of end game function
PUSHA
CMP p1Score, endScore
JE p1wins
CMP p2Score, endScore
JE p2wins
;Check if either player 1 or 2 achieved the winning score
INVOKE GetAsyncKeyState, escape
CMP AX, 0
JNE endIt
;Checks if the escape key is pressed
POPA
RET
p1wins:
MOV EAX, 0
MOV EBX, p2Msg
INVOKE drd_imageDraw, OFFSET BG1.info, BG1.pos.x, BG1.pos.y
INVOKE drd_imageDraw, OFFSET BG2.info, BG2.pos.x, BG2.pos.y
INVOKE drd_imageDraw, OFFSET winMsg.info, EAX, EAX
INVOKE drd_imageDraw, OFFSET loseMsg.info, EBX, EAX
INVOKE drd_imageDraw, OFFSET resMsg.info, resMsg.pos.x, resMsg.pos.y
INVOKE drd_flip
;Draws end screen for when player 1 wins
INVOKE PlaySound, OFFSET hddEnd, NULL, SND_ASYNC OR SND_LOOP
;Play end music
JMP loopc
p2wins:
MOV EAX, 0
MOV EBX, p2Msg
INVOKE drd_imageDraw, OFFSET BG1.info, BG1.pos.x, BG1.pos.y
INVOKE drd_imageDraw, OFFSET BG2.info, BG2.pos.x, BG2.pos.y
INVOKE drd_imageDraw, OFFSET winMsg.info, EBX, EAX
INVOKE drd_imageDraw, OFFSET loseMsg.info, EAX, EAX
INVOKE drd_imageDraw, OFFSET resMsg.info, resMsg.pos.x, resMsg.pos.y
INVOKE drd_flip
;Draws end screen for when player 2 wins
INVOKE PlaySound, OFFSET hddEnd, NULL, SND_ASYNC OR SND_LOOP
;Play end music
loopc:
INVOKE drd_processMessages
INVOKE restartGame, OFFSET scoreImgP1, OFFSET scoreImgP2, OFFSET scoreImgP12, OFFSET scoreImgP22
CMP restarted, 1
JE okNow
;Checks if you pressed R to restart the game
JMP loopc
okNow:
DEC restarted
;Reset the byte for later use
JMP lol
endIt:
INC endTheGame
;Increments the byte for closing the window
lol:
POPA
RET
endGame ENDP