This repository was archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.s
More file actions
1810 lines (1473 loc) · 45.7 KB
/
Copy pathpong.s
File metadata and controls
1810 lines (1473 loc) · 45.7 KB
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.data
# coordenadas del dibujo
tablero: .space 4
tablero_size: .space 4
ancho_dibujo: .word 50
alto_dibujo: .word 17
proporcion_x: .space 4
proporcion_y: .space 4
# configuracion del juego
ancho_campo: .word 2000000
alto_campo: .word 1000000
aceleracion_rebote_x: .word 25
aceleracion_rebote_y: .word 150
aceleracion_uniforme: .word 0
longitud_pala: .word 3
velocidad_pala: .word 100 # Porcentaje
aceleracion_pelota_x: .word 0
aceleracion_incremento: .word 0
alto_jugador: .space 4
velocidad_jugador: .space 4
velocidad_inicial_x: .word 300
velocidad_inicial_y: .word 0
velocidad_inicial_aleatoria: .word 0
puntuacion_maxima: .word 3
girar_campo: .word 0
acelerar_paredes: .word 0
# teclas con las que jugar
arriba_jugador_izq: .word 97
abajo_jugador_izq: .word 122
arriba_jugador_der: .word 107
abajo_jugador_der: .word 109
tecla_salir: .word 120
tecla_pausa: .word 112
# configuracion de video
fps_max: .word 5
tablero_pared: .word '#'
tablero_pelota: .word 'O'
tablero_numero: .word '*'
tablero_pala_der: .word '['
tablero_pala_izq: .word ']'
# estado de la partida actual
posicion_pelota_x: .space 4
posicion_pelota_y: .space 4
velocidad_pelota_x: .space 4
velocidad_pelota_y: .space 4
posicion_jugador_izquierdo_x: .space 4
posicion_jugador_izquierdo_y: .space 4
posicion_jugador_derecho_x: .space 4
posicion_jugador_derecho_y: .space 4
puntuacion_jugador_izquierdo: .space 4
puntuacion_jugador_derecho: .space 4
pausar_juego: .word 0
posicion_inicial_pelota_x: .space 4
posicion_inicial_pelota_y: .space 4
posicion_inicial_jugador_izquierdo_y: .space 4
posicion_inicial_jugador_derecho_y: .space 4
####################################################################################################################
## CONSTANTES
####################################################################################################################
#Representacion de los numeros
num_0: .byte 0x1, 0x2, 0x3, 0x4, 0x6, 0x7, 0x9, 0xA, 0xC, 0xD, 0xE, 0xF,0
num_1: .byte 0x1, 0x4, 0x7, 0xA, 0xD, 0
num_2: .byte 0x1, 0x2, 0x3, 0x6, 0x7, 0x8, 0x9, 0xA, 0xD, 0xE, 0xF, 0
num_3: .byte 0x1, 0x2, 0x3, 0x6, 0x8, 0x9, 0xC, 0xD, 0xE, 0xF, 0
num_4: .byte 0x1, 0x3, 0x4, 0x6, 0x7, 0x8, 0x9, 0xC, 0xF, 0
num_5: .byte 0x1, 0x2, 0x3, 0x4, 0x7, 0x8, 0x9, 0xC, 0xD, 0xE, 0xF, 0
num_6: .byte 0x1, 0x2, 0x3, 0x4, 0x7, 0x8, 0x9, 0xA, 0xC, 0xD, 0xE, 0xF, 0
num_7: .byte 0x1, 0x2, 0x3, 0x6, 0x9, 0xC, 0xF, 0
num_8: .byte 0x1, 0x2, 0x3, 0x4, 0x6, 0x7, 0x8, 0x9, 0xA, 0xC, 0xD, 0xE, 0xF, 0
num_9: .byte 0x1, 0x2, 0x3, 0x4, 0x6, 0x7, 0x8, 0x9, 0xC, 0xD, 0xE, 0xF, 0
# Cadenas comunes
msg_espacio: .asciiz " "
msg_cr: .asciiz "\n"
msg_tab: .asciiz "\t"
msg_dos_puntos: .asciiz ": "
# Mensajes de los menus
msg_titulo: .asciiz "PONGO: Dissasembled Edition\n\t1 - Iniciar Partida\n\t2 - Opciones del Juego\n\t3 - Instrucciones\n\n\tX - Salir\n"
msg_instrucciones: .asciiz "INSTRUCCIONES PARA JUGAR AL PONGO:\n\t1. Si no sabes jugar, retirate... en serio...\n\nPulse cualquier tecla para continuar..."
msg_opciones: .asciiz "MENU DE OPCIONES\n\t1 - Configurar Teclado\n\t2 - Opciones del Juego\n\n\tX - Atr�s\n"
msg_opciones_juego: .asciiz "OPCIONES DEL JUEGO\n\t- Opciones de Pantalla -\n\t1 - Ajustar el n�mero de fotogramas por segundo\n\t2 - Establecer la altura del campo de juego\n\t3 - Establecer el ancho del campo de juego\n\t4 - Longitud de las palas\n\n\t- F�sica del juego -\n\t5 - Velocidad de movimiento de las palas\n\t6 - Velocidad inicial de la pelota en horizontal\n\t7 - Velocidad inicial de la pelota en vertical\n\t8 - Pelota aleatoria al iniciar\n\t9 - Aceleraci�n uniforme de la pelota\n\t0 - Aceleraci�n horizontal de la pelota al chocar con una pala\n\tA - Aceleraci�n vertical de la pelota al chocar con una pala\n\tB - Aceleraci�n de la pelota al chocar con una pared\n\tC - Puntuacion m�xima\n\n\tX - Atras\nOPCION: "
msg_ganador: .asciiz " ..... d*##$.\n zP\"\"\"\"\"$e. $\" $o\n 4$ '$ $\" $\n '$ '$ J$ $F\n 'b $k $> $\n $k $r J$ d$\n '$ $ $\" $~\n '$ \"$ '$E $\n $ $L $\" $F ...\n $. 4B $ $$$*\"\"\"*b\n '$ $. $$ $$ $F\n \"$ R$ $F $\" $\n $k ?$ u* dF .$\n ^$. $$\" z$ u$$$$e\n #$b $E. $\" ?$\n #$ .o$$# d$$$$c ?F\n $ .d$$#\" . zo$> #$r .uF\n $L .u$*\" $&$$$k .$$d$$F\n $$\" \"\"^\"$$$P\"$P9$\n JP .o$$$$u:$P $$\n $ ..ue$\" \"\" $\"\n d$ $F $\n $$ ....udE 4B\n #$ \"\"\"\"` $r @$\n ^$L '$ $F\n RN 4N $\n"
msg_ganador_derecha: .asciiz "�� FELICIDADES JUGADOR DERECHO !! �� HAS GANADO !!\n"
msg_ganador_izquierda: .asciiz "�� FELICIDADES JUGADOR IZQUIERDO !! �� HAS GANADO !!\n"
msg_opciones_1: .asciiz "\nN�mero de fotogramas por segundo [5 / 20]: "
msg_opciones_2: .asciiz "\nEstablecer la altura del campo de juego [5 / 25]: "
msg_opciones_3: .asciiz "\nEstablecer el ancho del campo de juego [20 / 100]: "
msg_opciones_4: .asciiz "\nLongitud de las palas [1 / 10]: "
msg_opciones_5: .asciiz "\nVelocidad de las palas en porcentaje [50 / 200]: "
msg_opciones_6: .asciiz "\nVelocidad inicial de la pelota en horizontal [50 / 500]: "
msg_opciones_7: .asciiz "\nVelocidad inicial de la pelota en vertical [-100 / 100]: "
msg_opciones_8: .asciiz "\n�Hacer que la bola comience con direcci�n y velocidad aleatorias? [0 / 1] "
msg_opciones_9: .asciiz "\nAceleraci�n uniforme de la pelota [0 / 10]: "
msg_opciones_0: .asciiz "\nAceleraci�n horizontal de la pelota al chocar con una pala [0 / 500]: "
msg_opciones_a: .asciiz "\nAceleraci�n vertical de la pelota al chocar con una pala [0 / 100]: "
msg_opciones_b: .asciiz "\nAceleraci�n de la pelota al chocar con una pared [0 / 1]: "
msg_opciones_c: .asciiz "\nPuntuaci�n que se debe alcanzar para ganar la partida [10 / 100]:"
msg_jug_izq: .asciiz "\nJugador izquierdo"
msg_jug_der: .asciiz "\nJugador derecho"
msg_arriba: .asciiz "\nArriba: "
msg_abajo: .asciiz "\nAbajo: "
msg_tecla_pause: .asciiz "\nTecla para pausar el juego: "
msg_tecla_salir: .asciiz "\nTecla para salir del juego: "
# Procedimiento leer_entero
msg_leer_entero_error_max: .asciiz "El numero debe ser como maximo "
msg_leer_entero_error_min: .asciiz "El numero debe ser como minimo "
# procedimiento leer_minuscula
msg_leer_caracter_error: .asciiz "\nLa tecla debe ser una letra de la A a la Z o un numero: "
.text
.globl main
####################################################################################################################
## INICIO DEL CAOS
####################################################################################################################
main:
addi $sp, $sp, -16
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
################
main_menu_principal:
jal menu_principal
iniciar_juego:
jal iniciar_variables
li $t0, 1000
lw $t1, fps_max
div $s0, $t0, $t1 # tiempo_pausa = 1000 / fps_max
li $v0, 30 # Tiempo de inicio del juego
syscall
move $s1, $a0 # $s1 = antes.lo
bucle_principal_juego:
jal poll_entrada
beqz $v0, main_menu_principal
li $v0, 30 # syscall time
syscall
subu $s2, $a0, $s1 # transcurrido = ahora.lo - antes.lo
ble $s2, $s0, bucle_principal_juego # Si (transcurrido < tiempo_pausa)
move $s1, $a0 # antes.lo = ahora.lo
move $a0, $s2
jal avanza_pong # avanza_pong(transcurrido)
jal dibujar_campo
j bucle_principal_juego
salir_programa:
################
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
addi $sp, $sp, 16
li $v0, 10
syscall
jr $ra
####################################################################################################################
## AVANZA_PONG
####################################################################################################################
#######################################################
# aceleracion: Aumenta la velocidad uniformemente
aceleracion:
lw $t0, velocidad_pelota_x
lw $t1, aceleracion_incremento
lw $t2, aceleracion_pelota_x
add $t2, $t2, $t1
bltz $t0, aceleracion_negativa
add $t0, $t0, $t2
j aceleracion_fin
aceleracion_negativa:
sub $t0, $t0, $t2
aceleracion_fin:
sw $t2, aceleracion_pelota_x
sw $t0, velocidad_pelota_x
jr $ra
# Mueve la pelota y detecta colisiones
# $a0: transcurrido: indica cuantos milisegundos han pasado desde la ultima actualizacion
avanza_pong:
# Si el juego esta pausado, no hace nada, termina el metodo
lw $t0, pausar_juego
beqz $t0, avanza_pong_no_pausado
jr $ra
avanza_pong_no_pausado:
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
move $a2, $a0
jal aceleracion
lw $a0, velocidad_pelota_x
lw $a1, velocidad_pelota_y
jal prod_vector_int # prod_vector_int(velocidad_pelota_x, velocidad_pelota_y, transcurrido)
lw $a0, posicion_pelota_x
lw $a1, posicion_pelota_y
move $a2, $v0
move $a3, $v1
jal suma_vector # suma_vector(posicion_pelota_x, posicion_pelota_y, $v0, $v1)
sw $v0, posicion_pelota_x
sw $v1, posicion_pelota_y
move $s0, $v0 # $s0: posicion_pelota_x (para preservar despues de llamadas)
move $s1, $v1 # $s1: posicion_pelota_y (para preservar despues de llamadas)
lw $t0, alto_jugador
################################################################
# rebote con pala derecha
lw $t1, posicion_jugador_derecho_x
ble $s0, $t1, l06a # posicion_pelota_x <= posicion_jugador_derecho_x
lw $t2, posicion_jugador_derecho_y
blt $s1, $t2, l06a # posicion_pelota_y < posicion_jugador_derecho_y
add $t3, $t2, $t0 # posicion_jugador_derecho_y + alto_jugador
bge $s1, $t3, l06a # posicion_pelota_y >= posicion_jugador_derecho_y + alto_jugador
## cambio de direccion y aceleracion horizontal
sub $s0, $s0, $t1 # posicion_pelota_x - posicion_jugador_derecho_x
sub $s0, $t1, $s0 # posicion_jugador_derecho_x - (posicion_pelota_x - posicion_jugador_derecho_x)
sw $s0, posicion_pelota_x
lw $t4, velocidad_pelota_x
sub $t4, $0, $t4 # -velocidad_pelota
lw $t5, aceleracion_rebote_x
sub $t4, $t4, $t5 # -velocidad_pelota - aceleracion_rebote_x
sw $t4, velocidad_pelota_x
## aceleracion vertical
sra $t5, $t0, 1 # alto_jugador / 2
add $t6, $t2, $t5 # centro_pala_y = posicion_jugador_derecho_y + alto_jugador / 2
sub $t6, $s1, $t6 # dist_pelota_centro_pala = posicion_pelota_y - centro_pala_y
mtc1 $t6, $f0
cvt.s.w $f0, $f0 # (float) dist_pelota_centro_pala
mtc1 $t5, $f1
cvt.s.w $f1, $f1 # (float) (alto_jugador / 2)
div.s $f0, $f0, $f1 # f = (float) dist_pelota_centro_pala / (float) alto_jugador
lwc1 $f1, aceleracion_rebote_y
cvt.s.w $f1, $f1 # (float) aceleracion_rebote_y
mul.s $f0, $f0, $f1 # f * ((float) aceleracion_rebote_y)
lwc1 $f1, velocidad_pelota_y
cvt.s.w $f1, $f1 # (float) velocidad_pelota_y
add.s $f0, $f1, $f0 # velocidad_pelota_y + f * ((float) aceleracion_rebote_y)
cvt.w.s $f0, $f0 # (int) (velocidad_pelota_y + f * ((float) aceleracion_rebote_y))
swc1 $f0, velocidad_pelota_y
li $a0, 60 #rebote en las palas 127
li $a1, 500 #duracion
li $a2, 127 #instrument
li $a3, 127 #volume
li $v0, 31
syscall
j l06f
#################################################
# rebote con pala izquierda
l06a:
lw $t1, posicion_jugador_izquierdo_x
bge $s0, $t1, l06b # posicion_pelota_x >= posicion_jugador_izquierdo_x
lw $t2, posicion_jugador_izquierdo_y
blt $s1, $t2, l06b # posicion_pelota_y < posicion_jugador_izquierdo_y
add $t3, $t2, $t0 # posicion_jugador_izquierdo_y + alto_jugador
bge $s1, $t3, l06b # posicion_pelota_y >= posicion_jugador_izquierdo_y + alto_jugador
## cambio de dirección y aceleración horizontal
sub $s0, $t1, $s0 # posicion_jugador_izquierdo_x - posicion_pelota_x
add $s0, $t1, $s0 # posicion_jugador_izquierdo_x + (posicion_jugador_izquierdo.x - posicion_pelota.x);
sw $s0, posicion_pelota_x
lw $t4, velocidad_pelota_x
sub $t4, $0, $t4 # -velocidad_pelota
lw $t5, aceleracion_rebote_x
add $t4, $t4, $t5 # -velocidad_pelota + aceleracion_rebote_x
sw $t4, velocidad_pelota_x
## aceleracion vertical
sra $t5, $t0, 1 # alto_jugador / 2
add $t6, $t2, $t5 # centro_pala_y = posicion_jugador_izquierdo_y + alto_jugador / 2
sub $t6, $s1, $t6 # dist_pelota_centro_pala = posicion_pelota_y - centro_pala_y
mtc1 $t6, $f0
cvt.s.w $f0, $f0 # (float) dist_pelota_centro_pala
mtc1 $t5, $f1
cvt.s.w $f1, $f1 # (float) (alto_jugador / 2)
div.s $f0, $f0, $f1 # f = (float) dist_pelota_centro_pala / (float) alto_jugador
lwc1 $f1, aceleracion_rebote_y
cvt.s.w $f1, $f1 # (float) aceleracion_rebote_y
mul.s $f0, $f0, $f1 # f * ((float) aceleracion_rebote_y)
lwc1 $f1, velocidad_pelota_y
cvt.s.w $f1, $f1 # (float) velocidad_pelota_y
add.s $f0, $f1, $f0 # velocidad_pelota_y + f * ((float) aceleracion_rebote_y)
cvt.w.s $f0, $f0 # (int) (velocidad_pelota_y + f * ((float) aceleracion_rebote_y))
swc1 $f0, velocidad_pelota_y
li $a0, 60 #rebote en las palas 127
li $a1, 500 #duracion
li $a2, 127 #instrument
li $a3, 127 #volume
li $v0, 31
syscall
j l06f
# pared derecha: GOL
l06b:
lw $t1, ancho_campo
ble $s0, $t1, l06c # posicion_pelota_x <= ancho_campo
lw $t1, puntuacion_jugador_izquierdo
addi $t1, $t1, 1 # puntuacion_jugador_izquierdo + 1
sw $t1, puntuacion_jugador_izquierdo
lw $t0, puntuacion_maxima # puntuacion_maxima?
bge $t1, $t0, izquierdo_gana
j izquierdo_aun_no_gana
izquierdo_gana:
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
j we_are_the_champions
izquierdo_aun_no_gana:
jal actualizar_marcadores
li $a0, 1 # derecha
jal iniciar_bola
j l06f
# pared izquierda: GOL
l06c:
bge $s0, $0, l06d # posicion_pelota_x >= 0
lw $t1, puntuacion_jugador_derecho
addi $t1, $t1, 1 # puntuacion_jugador_derecho + 1
sw $t1, puntuacion_jugador_derecho
lw $t0, puntuacion_maxima # puntuacion_maxima?
bge $t1, $t0, derecho_gana
j derecho_aun_no_gana
derecho_gana:
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
j we_are_the_champions
derecho_aun_no_gana:
jal actualizar_marcadores
li $a0, 0 # izquierda
jal iniciar_bola
j l06f
# pared inferior
l06d:
lw $t1, alto_campo
ble $s1, $t1, l06e # posicion_pelota_y <= alto_campo
sub $t2, $s1, $t1 # posicion_pelota_y - alto_campo
sub $s1, $t1, $t2 # alto_campo - (posicion_pelota_y - alto_campo)
sw $s1, posicion_pelota_y
lw $s1, velocidad_pelota_y
sub $s1, $0, $s1 # -velocidad_pelota_y
# Aceleracion aleatoria al eje Y
lw $t0, acelerar_paredes
beqz $t0, no_acelerar_pared_inferior
li $a0, -100
li $a1, 0
jal random_integer
add $s1, $v0, $s1
no_acelerar_pared_inferior:
#
sw $s1, velocidad_pelota_y
li $a0, 120 #rebote en las
li $a1, 350 #duracion
li $a2, 127 #instrument
li $a3, 127 #volume
li $v0, 31
syscall
j l06f
# pared superior
l06e:
bge $s1, $0, l06f # posicion_pelota_y >= 0
sub $s1, $0, $s1 # -posicion_pelota_y
sw $s1, posicion_pelota_y
lw $s1, velocidad_pelota_y
sub $s1, $0, $s1 # -velocidad_pelota_y
# Aceleracion aleatoria al eje Y
lw $t0, acelerar_paredes
beqz $t0, no_acelerar_pared_superior
li $a0, 0
li $a1, 100
jal random_integer
add $s1, $v0, $s1
no_acelerar_pared_superior:
#
sw $s1, velocidad_pelota_y
li $a0, 120 #rebote en las
li $a1, 350 #duracion
li $a2, 127 #instrument
li $a3, 127 #volume
li $v0, 31
syscall
j l06f
l06f:
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
####################################################################################################################
## MANEJO DE LAS TECLAS
####################################################################################################################
#######################################################
# keyio_poll_key: Realiza un sondeo del teclado
# RETURN:
# $v0 - Codigo del caracter correspondiente a la letra MINUSCULA de la ultima tecla pulsada o cero si no se pulso ninguna tecla
keyio_poll_key:
addi $sp, $sp, -4
sw $ra, 0($sp)
# APILA #
li $v0, 0
lb $t0, 0xffff0000 # carga registro de control del receptor
andi $t0, $t0, 0x00000001 # mira el bit 0
beqz $t0, keyio_poll_salida # si no hay nada disponible, devuelve 0.
lb $a0, 0xffff0004 # lee caracter del registro de datos del receptor
jal pasar_minusculas
keyio_poll_salida:
# DESAPILA #
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#######################################################
# poll_entrada: Comprueba si se ha pulsado alguna tecla y la procesa en caso afirmativo
# RETURN:
# $v0 - Cero si el juego debe ser detenido
poll_entrada:
addi $sp, $sp, -4
sw $ra, 0($sp)
# APILA #
jal keyio_poll_key
beqz $v0, poll_entrada_end1
lw $t0, pausar_juego
bnez $t0, keyin_pausa # Si el juego esta en pausa, ignora todas las teclas salvo la de pausa y la de salir
keyin_izq_arriba:
lw $t3, arriba_jugador_izq
bne $v0, $t3, keyin_izq_abajo
keyin_izq_arriba_pulsada:
lw $t0, posicion_jugador_izquierdo_y
blez $t0, poll_entrada_end1
lw $t1, velocidad_jugador
sub $t0, $t0, $t1 # posicion_jugador_izquierdo_y - velocidad_jugador
sw $t0, posicion_jugador_izquierdo_y
j poll_entrada_end1
keyin_izq_abajo:
lw $t3, abajo_jugador_izq
bne $v0, $t3, keyin_der_arriba
keyin_izq_abajo_pulsada:
lw $t0, posicion_jugador_izquierdo_y
lw $t1, alto_jugador
add $t1, $t0, $t1 # posicion_jugador_izquierdo_y + alto_jugador
lw $t2, alto_campo
bge $t1, $t2, poll_entrada_end1
lw $t1, velocidad_jugador
add $t0, $t0, $t1 # posicion_jugador_izquierdo_y +velocidad_jugador
sw $t0, posicion_jugador_izquierdo_y
j poll_entrada_end1
keyin_der_arriba:
lw $t3, arriba_jugador_der
bne $v0, $t3, keyin_der_abajo
keyin_der_arriba_pulsada:
lw $t0, posicion_jugador_derecho_y
blez $t0, poll_entrada_end1
lw $t1, velocidad_jugador
sub $t0, $t0, $t1 # posicion_jugador_derecho_y - velocidad_jugador
sw $t0, posicion_jugador_derecho_y
j poll_entrada_end1
keyin_der_abajo:
lw $t3, abajo_jugador_der
bne $v0, $t3, keyin_pausa
keyin_der_abajo_pulsada:
lw $t0, posicion_jugador_derecho_y
lw $t1, alto_jugador
add $t1, $t0, $t1 # posicion_jugador_izquierdo_y + alto_jugador
lw $t2, alto_campo
bge $t1, $t2, poll_entrada_end1
lw $t1, velocidad_jugador
add $t0, $t0, $t1 # posicion_jugador_izquierdo_y +velocidad_jugador
sw $t0, posicion_jugador_derecho_y
j poll_entrada_end1
keyin_pausa:
lw $t3, tecla_pausa
bne $v0, $t3, keyin_salir
keyin_pausa_pulsada:
lw $t0, pausar_juego
not $t0, $t0
sw $t0, pausar_juego
j poll_entrada_end1
keyin_salir:
lw $t3, tecla_salir
bne $v0, $t3, poll_entrada_end1
poll_entrada_end0:
li $v0, 0
j poll_entrada_end
poll_entrada_end1:
li $v0, 1
poll_entrada_end:
# DESAPILA #
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
####################################################################################################################
## INICIO DE VARIABLES
####################################################################################################################
#######################################################
# iniciar_variables: Inicia todas las variables necesarias antes de comenzar el juego
iniciar_variables:
addi $sp, $sp, -36
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
sw $s3, 12($sp)
sw $s4, 16($sp)
sw $s5, 20($sp)
sw $s6, 24($sp)
sw $s7, 28($sp)
sw $ra, 32($sp)
#APILA#
lw $s0, alto_campo
lw $s1, ancho_campo
lw $s2, alto_dibujo
lw $s3, ancho_dibujo
# 1 - Pausa = 0
sw $0, pausar_juego
# 2 - Proporciones X e Y
lwc1 $f0, ancho_dibujo
lwc1 $f1, ancho_campo
cvt.s.w $f0, $f0 # (float) ancho_dibujo
cvt.s.w $f1, $f1 # (float) ancho_campo
div.s $f0, $f1, $f0 # f0: proporcion_x = (float) ancho_dibujo / (float) ancho_campo
swc1 $f0, proporcion_x
cvt.w.s $f0, $f0
mfc1 $s5, $f0
lwc1 $f0, alto_dibujo
lwc1 $f1, alto_campo
cvt.s.w $f0, $f0 # (float) alto_dibujo
cvt.s.w $f1, $f1 # (float) alto_campo
div.s $f0, $f1, $f0 # f0: proporcion_y = (float) alto_dibujo / (float) alto_campo
swc1 $f0, proporcion_y
cvt.w.s $f0, $f0
mfc1 $s4, $f0
# 3 - Alto Jugador = Proporcion_y * Longitud de pala
lw $s6, longitud_pala
mul $t0, $s6, $s5
sw $t0, alto_jugador
# 4 - Velocidad Jugador = (Proporcion_y *velocidad_pala)/100
lw $s7, velocidad_pala
li $t0, 100
mul $t1, $s4, $s7
div $s7, $t1, $t0
sw $s7, velocidad_jugador
# 5 - Tama�o del buffer
addi $t0, $s2, 8
addi $t1, $s3, 1
mul $a0, $t1, $t0
sw $a0, tablero_size
addi $a0, $a0, 1 # Pedimos 1 byte mas para guardar un 0x0 final
li $v0, 9 # Pide memoria al SO
syscall
sw $v0, tablero
# 6 - Posiciones iniciales de palas y pelota
srl $t0, $s0, 1
srl $t1, $s1, 1
sw $t0, posicion_inicial_pelota_y
sw $t1, posicion_inicial_pelota_x
mul $t4, $s6, $s5
srl $t2, $t4, 1
sub $t3, $t0, $t2
sw $t3, posicion_inicial_jugador_izquierdo_y
sw $t3, posicion_inicial_jugador_derecho_y
srl $t0, $s5, 1
sw $t0, posicion_jugador_izquierdo_x # A media proporcion_x de la izq
sub $t3, $s1, $t0
sw $t3, posicion_jugador_derecho_x # A media proporcion_x de la der
# 7 - Puntuaciones
sw $0, puntuacion_jugador_derecho
sw $0, puntuacion_jugador_izquierdo
# 8 - Tablero
jal iniciar_tablero
jal actualizar_marcadores
# 9 - Pelota
la $a0, -1
jal iniciar_bola
#DESAPILA#
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $s3, 12($sp)
lw $s4, 16($sp)
lw $s5, 20($sp)
lw $s6, 24($sp)
lw $s7, 28($sp)
lw $ra, 32($sp)
addi $sp, $sp, 36
jr $ra
#######################################################
# iniciar_bola: Pone la bola en el centro del campo y centra las palas
# ARGS:
# $a0 - Direccion inicial de la bola. -1 = aleatorio, 0 = Izquierda, 1 = Derecha
iniciar_bola:
addi $sp, $sp, -12
sw $s0, 8($sp)
sw $s1, 4($sp)
sw $ra, 0($sp)
#APILA#
# 1 - Reestablece la aceleracion
sw $zero, aceleracion_pelota_x
# 2 - Cargamos las posiciones iniciales de la pelota y las palas
# Notese que las posiciones X de las palas se establencen constantes en iniciar_variables
lw $t0, posicion_inicial_pelota_x
sw $t0, posicion_pelota_x
lw $t0, posicion_inicial_pelota_y
sw $t0, posicion_pelota_y
lw $t0, posicion_inicial_jugador_izquierdo_y
sw $t0, posicion_jugador_izquierdo_y
lw $t0, posicion_inicial_jugador_derecho_y
sw $t0, posicion_jugador_derecho_y
# 3 - Calculando velocidad inicial de la pelota
lw $t0, velocidad_inicial_aleatoria
beqz $t0, inicia_pelota_velocidad_fija
inicia_pelota_velocidad_aleatoria:
move $s1, $a0 # Salvamos $a0 antes de las llamadas
li $a0, 150 # X
li $a1, 500
jal random_integer
move $s0, $v0
li $a0, -250 # Y
li $a1, 250
jal random_integer
move $a0, $s1 # Recuperamos $a0 tras las llamadas
move $s1, $v0
j inicia_pelota_direccion
inicia_pelota_velocidad_fija:
lw $s0, velocidad_inicial_x
lw $s1, velocidad_inicial_y
# 4 - Establecemos la direccion inicial de la pelota
inicia_pelota_direccion:
bgez $a0, inicia_pelota_establece_direccion # Si ($a0 == -1) entoces $a0 = random(0, 1)
li $a0, 0
li $a1, 1
jal random_integer
move $a0, $v0
inicia_pelota_establece_direccion:
beq $a0, 0, inicia_pelota_izquierda # Si ($a0 == 0) entonces {NADA} sino {velocidad_x *= -1}
j inicia_pelota_fin
inicia_pelota_izquierda:
sub $s0, $0, $s0
inicia_pelota_fin:
sw $s0, velocidad_pelota_x
sw $s1, velocidad_pelota_y
#DESAPILA#
lw $s0, 8($sp)
lw $s1, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 12
jr $ra
####################################################################################################################
## PROCEDIMIENTOS - MENUS
####################################################################################################################
#######################################################
# menu_principal: Muestra el menu por pantalla
menu_principal:
jal limpiar_pantalla
la $a0, msg_titulo
jal leer_caracter
beq $v0, 49, menu_inicio_juego
beq $v0, 50, menu_opciones
beq $v0, 51, menu_instrucciones
j salir_programa
menu_inicio_juego:
j iniciar_juego
menu_opciones:
jal limpiar_pantalla
la $a0, msg_opciones
jal leer_caracter
beq $v0, 49, menu_opciones_teclado
beq $v0, 50, menu_opciones_juego
j menu_principal
menu_opciones_teclado:
jal limpiar_pantalla
# salir
la $a0, msg_tecla_salir
jal leer_caracter
sw $v0, tecla_salir
# pause
la $a0, msg_tecla_pause
jal leer_caracter
sw $v0, tecla_pausa
# Jugador Izquierdo
la $a0, msg_jug_izq
jal imprimir_cadena
# Arriba
la $a0, msg_arriba
jal leer_caracter
sw $v0, arriba_jugador_izq
# Abajo
la $a0, msg_abajo
jal leer_caracter
sw $v0, abajo_jugador_izq
# Jugador Derecho
la $a0, msg_jug_der
jal imprimir_cadena
# Arriba
la $a0, msg_arriba
jal leer_caracter
sw $v0, arriba_jugador_der
# Abajo
la $a0, msg_abajo
jal leer_caracter
sw $v0, abajo_jugador_der
j menu_opciones
menu_opciones_juego:
jal limpiar_pantalla
la $a0, msg_opciones_juego
jal leer_caracter
beq $v0, 49, menu_opciones_juego_1
beq $v0, 50, menu_opciones_juego_2
beq $v0, 51, menu_opciones_juego_3
beq $v0, 52, menu_opciones_juego_4
beq $v0, 53, menu_opciones_juego_5
beq $v0, 54, menu_opciones_juego_6
beq $v0, 55, menu_opciones_juego_7
beq $v0, 56, menu_opciones_juego_8
beq $v0, 57, menu_opciones_juego_9
beq $v0, 48, menu_opciones_juego_0
beq $v0, 97, menu_opciones_juego_a
beq $v0, 98, menu_opciones_juego_b
beq $v0, 99, menu_opciones_juego_c
j menu_opciones
menu_opciones_juego_1:
la $a0, msg_opciones_1
li $a1, 5
li $a2, 20
jal leer_entero
sw $v0, fps_max
j menu_opciones_juego
menu_opciones_juego_2:
la $a0, msg_opciones_2
li $a1, 5
li $a2, 25
jal leer_entero
sw $v0, alto_dibujo
j menu_opciones_juego
menu_opciones_juego_3:
la $a0, msg_opciones_3
li $a1, 20
li $a2, 100
jal leer_entero
sw $v0, ancho_dibujo
j menu_opciones_juego
menu_opciones_juego_4:
la $a0, msg_opciones_4
li $a1, 1
li $a2, 10
jal leer_entero
sw $v0, longitud_pala
j menu_opciones_juego
menu_opciones_juego_5:
la $a0, msg_opciones_5
li $a1, 50
li $a2, 200
jal leer_entero
sw $v0, velocidad_pala
j menu_opciones_juego
menu_opciones_juego_6:
la $a0, msg_opciones_6
li $a1, 50
li $a2, 500
jal leer_entero
sw $v0, velocidad_inicial_x
j menu_opciones_juego
menu_opciones_juego_7:
la $a0, msg_opciones_7
li $a1, -100
li $a2, 100
jal leer_entero
sw $v0, velocidad_inicial_y
j menu_opciones_juego
menu_opciones_juego_8:
la $a0, msg_opciones_8
li $a1, 0
li $a2, 1
jal leer_entero
sw $v0, velocidad_inicial_aleatoria
j menu_opciones_juego
menu_opciones_juego_9:
la $a0, msg_opciones_9
li $a1, 0
li $a2, 10
jal leer_entero
sw $v0, aceleracion_incremento
j menu_opciones_juego
menu_opciones_juego_0:
la $a0, msg_opciones_0
li $a1, 0
li $a2, 500
jal leer_entero
sw $v0, aceleracion_rebote_x
j menu_opciones_juego
menu_opciones_juego_a:
la $a0, msg_opciones_a
li $a1, 0
li $a2, 100
jal leer_entero
sw $v0, aceleracion_rebote_y
j menu_opciones_juego
menu_opciones_juego_b:
la $a0, msg_opciones_b
li $a1, 0
li $a2, 1
jal leer_entero
sw $v0, acelerar_paredes
j menu_opciones_juego
menu_opciones_juego_c:
la $a0, msg_opciones_c
li $a1, 10
li $a2, 100
jal leer_entero
sw $v0, puntuacion_maxima
j menu_opciones_juego
j menu_opciones
menu_instrucciones:
jal limpiar_pantalla
la $a0, msg_instrucciones
jal imprimir_cadena
li $v0, 12
syscall
j menu_principal