-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmicrobit-pong.ino
1721 lines (1539 loc) · 41.5 KB
/
microbit-pong.ino
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
//// N. Kendrick
////
//// PONG ball and paddle game for Atmel ATMega328P.
////
//// Modified version of Grant Searle's V1.4 code
////
//// See: http://searle.hostei.com/grant/AVRPong/index.html
//// http://searle.hostei.com/grant/AVRPong/GameFiles1.4.zip
////
//// This version: NK 1.00 25th Feb 2018
////
//// Changes from Grant's code:
////
//// * Senses a pin on startup to determine whether analogue or digital paddles should be used
//// * Defines 4 pins for detecting left/right up/down
//// * Has a counter to set how often the pins are read ('every 'n' frames')
//// * Has a value to determine by how much the paddles should be moved
//// * See the variables for default pins used. All my added variables start with 'NK'
////
//// NB: This code will only compile with Arduino environment 1.60. You can try with later
//// versions, but YMMV.
////
//// The Atmega fuses must be set correctly for proper game operation - Grant has a note on his site about
//// this. For the TL866 programmer, the following work (the first two are probably the relevant ones):
//// Uncheck all fuses but leave these ticked: SUT1=0, CKSEL3=0, SPIEN=0, BOOTSZ1=0, BOOTSZ0=0. This may
//// result in a verification error as the fuse settings differ from those in the hex file created by the
//// Arduino IDE, so either ignore the error or turn of 'verify after'.
//
// 1970's TV game recreation by Grant Searle 2013
// Emulation of the PAL version of the AY-3-8500 that was used in many TV games from that era
//
// current web page is http://searle.hostei.com/grant/AVRPong/index.html
// If this page is not found (moved etc) then search for "Grant Searle" using a search engine such as Google
// to find my electronics page. On there will be a link to this page
//
// Top half of this file is my code to recreate the 4 paddle games that were on the game consoles.
// Second half is a cut-down and modified version of the TV Out library (http://code.google.com/p/arduino-tvout/)
// Bitmap for the edges - faster than setting individual pixels
PROGMEM const unsigned char edge[] = {
0b10101010, 0b10101010, 0b10101010, 0b10101010
};
// Font bitmap for the 7 segment on-screen scoring display
PROGMEM const unsigned char sevenSegPAL[] = {
0b11111100, 0b11111100, 0b11111100, 0b11001100, 0b11001100, 0b11001100, 0b11001100, 0b11001100,
0b11001100, 0b11001100, 0b11001100, 0b11001100, 0b11111100, 0b11111100, 0b11111100, /* 0 */
0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100,
0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, /* 1 */
0b11111100, 0b11111100, 0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b11111100, 0b11111100,
0b11111100, 0b11000000, 0b11000000, 0b11000000, 0b11111100, 0b11111100, 0b11111100, /* 2 */
0b11111100, 0b11111100, 0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b11111100, 0b11111100,
0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b11111100, 0b11111100, 0b11111100, /* 3 */
0b11001100, 0b11001100, 0b11001100, 0b11001100, 0b11001100, 0b11001100, 0b11111100, 0b11111100,
0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, /* 4 */
0b11111100, 0b11111100, 0b11111100, 0b11000000, 0b11000000, 0b11000000, 0b11111100, 0b11111100,
0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b11111100, 0b11111100, 0b11111100, /* 5 */
0b11111100, 0b11111100, 0b11111100, 0b11000000, 0b11000000, 0b11000000, 0b11111100, 0b11111100,
0b11111100, 0b11001100, 0b11001100, 0b11001100, 0b11111100, 0b11111100, 0b11111100, /* 6 */
0b11111100, 0b11111100, 0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100,
0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, /* 7 */
0b11111100, 0b11111100, 0b11111100, 0b11001100, 0b11001100, 0b11001100, 0b11111100, 0b11111100,
0b11111100, 0b11001100, 0b11001100, 0b11001100, 0b11111100, 0b11111100, 0b11111100, /* 8 */
0b11111100, 0b11111100, 0b11111100, 0b11001100, 0b11001100, 0b11001100, 0b11111100, 0b11111100,
0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b11111100, 0b11111100, 0b11111100, /* 9 */
};
PROGMEM const unsigned char sevenSegNTSC[] = {
0b11111100, 0b11111100, 0b11001100, 0b11001100, 0b11001100, 0b11001100,
0b11001100, 0b11001100, 0b11111100, 0b11111100, /* 0 */
0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100, 0b00001100,
0b00001100, 0b00001100, 0b00001100, 0b00001100, /* 1 */
0b11111100, 0b11111100, 0b00001100, 0b00001100, 0b11111100, 0b11111100,
0b11000000, 0b11000000, 0b11111100, 0b11111100, /* 2 */
0b11111100, 0b11111100, 0b00001100, 0b00001100, 0b11111100, 0b11111100,
0b00001100, 0b00001100, 0b11111100, 0b11111100, /* 3 */
0b11001100, 0b11001100, 0b11001100, 0b11001100, 0b11111100, 0b11111100,
0b00001100, 0b00001100, 0b00001100, 0b00001100, /* 4 */
0b11111100, 0b11111100, 0b11000000, 0b11000000, 0b11111100, 0b11111100,
0b00001100, 0b00001100, 0b11111100, 0b11111100, /* 5 */
0b11111100, 0b11111100, 0b11000000, 0b11000000, 0b11111100, 0b11111100,
0b11001100, 0b11001100, 0b11111100, 0b11111100, /* 6 */
0b11111100, 0b11111100, 0b00001100, 0b00001100, 0b00001100, 0b00001100,
0b00001100, 0b00001100, 0b00001100, 0b00001100, /* 7 */
0b11111100, 0b11111100, 0b11001100, 0b11001100, 0b11111100, 0b11111100,
0b11001100, 0b11001100, 0b11111100, 0b11111100, /* 8 */
0b11111100, 0b11111100, 0b11001100, 0b11001100, 0b11111100, 0b11111100,
0b00001100, 0b00001100, 0b11111100, 0b11111100, /* 9 */
};
//NK Variables for modification to using digital up/down paddles
const int NKframeTrig = 2; // Check button every #+1 frames.
const int NKupDownStep = 75; // Granularity of up/down movement
bool NKuseDigitalPaddles; // True if NKpaddlemodePin us low during init
int NKframeCount; // Helps count frames for up/down button sampling (affects speed of movemet)
int NKdigitalLeft; // Left paddle digital position (equivalent to ADC 0-1023 paddle ranges)
int NKdigitalRight; // Right paddle digital position
const int NKleftUp = 0; // D0/PD0 Pins to sense for up/down commands
const int NKleftDown = 1; // D1/PD1
const int NKrightUp = 2; // D2/PD2
const int NKrightDown = 3; // D3/PD3
const int NKpaddlemodePin = 16; // A2/PC2 Hi = Analogue paddles, Lo = Digital up/down switches
//
int leftPaddle;
int rightPaddle;
const char LEFT_POTENTIOMETER=4;
const char RIGHT_POTENTIOMETER=5;
const char GAME_SEL=17;
//Use the following instead of a single game select button if preferred
//const int TENNIS_SEL=17;
//const int SOCCER_SEL=16;
//const int SQUASH_SEL=15;
//const int SOLO_SEL=14;
const char RESET_SW=5;
const char PADDLE_SIZE_SW=6;
const char SPEED_SW=7;
const char ANGLES_SW=8;
const char VERSION_SEL=12;
const char PAL_NTSC=4;
char ballVisible=1;
char attractMode=1;
char squashActivePlayer=1;
char ballSpeed=1;
char bigAngles=1;
char scoreLeft=0;
char scoreRight=0;
char ballX=0;
char ballY=0;
int virtualBallY=0;
char ballXInc=0;
char ballYInc=0;
char paddleHeight=14;
char chipVersion=1;
char fieldWidth=79;
char bat1X=2;
char bat2X=18;
char leftDig1X=21;
char leftDig2X=29;
char centreX=40;
char rightDig1X=43;
char rightDig2X=51;
char bat3X=58;
char bat4X=60;
char bat5X=76;
char edgeWidth=1;
char ballWidth=1;
char fieldHeight=115;
char soccerEdgeHeight=25;
char gameNumber=1; //1=Tennis, 2=Football, 3=Squash, 4=Solo
char downCount=0;
char palOrNtscMode=1; //1=PAL, 0=NTSC
uint8_t * screen;
// 5 lines of double-resolution to hold the ball
uint8_t * doubleResScreen;
void setup()
{
// NK Init the digital paddle pins
pinMode(NKleftUp, INPUT_PULLUP);
pinMode(NKleftDown, INPUT_PULLUP);
pinMode(NKrightUp, INPUT_PULLUP);
pinMode(NKrightDown, INPUT_PULLUP);
pinMode(NKpaddlemodePin, INPUT_PULLUP); // Hi = Analogue Pots, Lo = Digital switches
pinMode(LEFT_POTENTIOMETER, INPUT);
pinMode(RIGHT_POTENTIOMETER, INPUT);
pinMode(GAME_SEL, INPUT_PULLUP);
//Use the following instead of a single game select button if preferred
// pinMode(TENNIS_SEL, INPUT_PULLUP);
// pinMode(SOCCER_SEL, INPUT_PULLUP);
// pinMode(SQUASH_SEL, INPUT_PULLUP);
// pinMode(SOLO_SEL, INPUT_PULLUP);
pinMode(RESET_SW, INPUT_PULLUP);
pinMode(SPEED_SW, INPUT_PULLUP);
pinMode(ANGLES_SW, INPUT_PULLUP);
pinMode(PADDLE_SIZE_SW, INPUT_PULLUP);
pinMode(VERSION_SEL, INPUT_PULLUP);
pinMode(PAL_NTSC, INPUT_PULLUP);
doubleResScreen = (unsigned char*)malloc(10 * 5 * sizeof(unsigned char));
setPAL_NTSC();
reset();
}
void setPaddletype()
{
// NK Which paddle type are we using?
if (digitalRead(NKpaddlemodePin)==0)
{
NKuseDigitalPaddles = true; // Up/down buttons
// Reset digital paddles values
NKframeCount = 0; // Helps count frames for up/down button sampling (affects speed of movemet)
NKdigitalLeft = 512; // Digital paddles start in middle
NKdigitalRight = 512;
leftPaddle = 512;
rightPaddle = 512;
}
else
{
NKuseDigitalPaddles = false; // Analogue paddles
}
}
void setPAL_NTSC()
{
if (digitalRead(PAL_NTSC)==1) // 1=PAL, 0=NTSC
{
fieldHeight=115;
soccerEdgeHeight=25;
palOrNtscMode=1;
beginVideo(80,116);
//Position to the same location as for the AY3-3-8500
forceOutStart(47);
forceLineStart(50);
}
else
{
fieldHeight=96;
soccerEdgeHeight=21;
palOrNtscMode=0;
beginVideo(80,97);
//Position to the same location as for the AY3-3-8500-1
forceOutStart(49);
forceLineStart(45);
}
}
void setGame()
{
// Ensure the selection button is pressed for several frames before accepting
// to debounce it.
if (digitalRead(GAME_SEL)==1)
{
downCount=0;
}
else if (downCount<10)
{
downCount=downCount+1;
}
if (downCount==4)
{
// Select next game
gameNumber=gameNumber+1;
if (gameNumber>4)
{
gameNumber=1;
}
}
//Use the following instead of a single game select button if preferred
/*
if (digitalRead(TENNIS_SEL)==0)
{
gameNumber=1;
}
if (digitalRead(SOCCER_SEL)==0)
{
gameNumber=2;
}
if (digitalRead(SQUASH_SEL)==0)
{
gameNumber=3;
}
if (digitalRead(SOLO_SEL)==0)
{
gameNumber=4;
}
*/
}
void reset()
{
scoreLeft=0;
scoreRight=0;
setPaddletype(); //NK Analogue or digital paddles?
// Position ball off field so that it appears at the correct entry point
// a short while after reset
ballX=0;
virtualBallY=-18;
// Reset ball speed/angle
ballXInc=ballSpeed;
ballYInc=ballSpeed;
ballVisible=1;
attractMode=0;
squashActivePlayer=1;
}
void drawBoundary()
{
// Top and bottom dotted boundary
int i1;
for (i1=0;i1<fieldWidth-8;i1=i1+8)
{
bitmap(i1,0,edge,0,8,1);
bitmap(i1,fieldHeight-1,edge,0,8,1);
}
for (int i2=i1-8;i2<fieldWidth;i2=i2+2)
{
setPixel(i2,0,1);
setPixel(i2,fieldHeight-1,1);
}
//Set bytes directly as it is much quicker than "setPixel"
// Goals for Soccer
if (gameNumber==2)
{
if (chipVersion==2)
{
for (int i=0;i<(soccerEdgeHeight*10);i=i+10)
{
screen[i] |= 0b11000000;
screen[i+9] |=0b11000000;
}
for (int i=((fieldHeight-soccerEdgeHeight)*10);i<(fieldHeight*10);i=i+10)
{
screen[i] |= 0b11000000;
screen[i+9] |=0b11000000;
}
}
else
{
for (int i=0;i<(soccerEdgeHeight*10);i=i+10)
{
screen[i] |= 0b10000000;
screen[i+9] |=0b00000010;
}
for (int i=((fieldHeight-soccerEdgeHeight)*10);i<(fieldHeight*10);i=i+10)
{
screen[i] |= 0b10000000;
screen[i+9] |=0b00000010;
}
}
}
//Left wall for Squash or Solo
if (gameNumber==3 || gameNumber==4)
{
if (chipVersion==2)
{
for (int i=0;i<(fieldHeight*10);i=i+10)
{
screen[i] |= 0b11000000;
}
}
else
{
for (int i=0;i<(fieldHeight*10);i=i+10)
{
screen[i] |= 0b10000000;
}
}
}
//Centre line for Tennis or Soccer
if (gameNumber==1 || gameNumber==2)
{
if (palOrNtscMode==1) // PAL - Dotted centre
{
if (chipVersion==2)
{
int byteNum=24;
for (int i=4;i<fieldHeight-2;i=i+4)
{
screen[byteNum]=0b00001000;
screen[byteNum+10]=0b00001000;
byteNum=byteNum+40;
}
}
else
{
int byteNum=25;
for (int i=4;i<fieldHeight-2;i=i+4)
{
screen[byteNum]=0b10000000;
screen[byteNum+10]=0b10000000;
byteNum=byteNum+40;
}
}
}
else // NTSC - solid centre
{
if (chipVersion==2)
{
int byteNum=14;
for (int i=2;i<fieldHeight;i++)
{
screen[byteNum]=0b00001000;
byteNum=byteNum+10;
}
}
else
{
int byteNum=15;
for (int i=2;i<fieldHeight;i++)
{
screen[byteNum]=0b10000000;
byteNum=byteNum+10;
}
}
}
}
}
void drawScores()
{
// Only draw first digit if score is >9
if (scoreLeft>9)
{
drawDigit(leftDig1X,3,1);
drawDigit(leftDig2X,3,scoreLeft-10);
}
else
{
drawDigit(leftDig2X,3,scoreLeft);
}
if (gameNumber!=4) // not solo
{
// Only draw first digit if score is >9
if (scoreRight>9)
{
drawDigit(rightDig1X,3,1);
drawDigit(rightDig2X,3,scoreRight-10);
}
else
{
drawDigit(rightDig2X,3,scoreRight);
}
}
}
void drawDigit(int x, int y, int v)
{
if (palOrNtscMode==1)
{
bitmap(x,y,sevenSegPAL,v*15,8,15);
}
else
{
bitmap(x,y,sevenSegNTSC,v*10,8,10);
}
}
// Draw the ball on a virtual screen area 80 pixels across by 5 pixels high.
// This area is then displayed (double vertical resolution) instead of the normal background
// for those 5 lines.
static void inline setBallPixel(uint8_t x, uint8_t y)
{
if (x<0 || x>79 || y<0 || y>4)
{
return;
}
doubleResScreen[(x/8) + (y*10)] |= 0x80 >> (x&7);
}
void drawBall()
{
// Copy the screen to the high res screen
for (int x=0;x<10;x++)
{
// Put the appropriate 3 lines that are being overlayed into
// the double-res buffer
if ((virtualBallY % 2)==0)
{
doubleResScreen[x]=screen[x+(10*(ballY))];
doubleResScreen[x+10]=screen[x+(10*(ballY))];
doubleResScreen[x+20]=screen[x+(10*(ballY+1))];
doubleResScreen[x+30]=screen[x+(10*(ballY+1))];
doubleResScreen[x+40]=screen[x+(10*(ballY+2))];
}
else
{
doubleResScreen[x]=screen[x+(10*(ballY))];
doubleResScreen[x+10]=screen[x+(10*(ballY+1))];
doubleResScreen[x+20]=screen[x+(10*(ballY+1))];
doubleResScreen[x+30]=screen[x+(10*(ballY+2))];
doubleResScreen[x+40]=screen[x+(10*(ballY+2))];
}
}
// Mask off the sides to produce a dotted boundary
// (same as done in the display render routine)
if (chipVersion==2)
{
if ((virtualBallY % 2)==0)
{
for (int i=10;i<=30;i=i+20)
{
if (gameNumber>1) doubleResScreen[i] &= 0b00111111;
if (gameNumber==2) doubleResScreen[i+9] =0;
}
}
else
{
for (int i=0;i<=40;i=i+20)
{
if (gameNumber>1) doubleResScreen[i] &= 0b00111111;
if (gameNumber==2) doubleResScreen[i+9] =0;
}
}
}
else
{
if ((virtualBallY % 2)==0)
{
for (int i=10;i<=30;i=i+20)
{
if (gameNumber>1) doubleResScreen[i] &= 0b01111111;
if (gameNumber==2) doubleResScreen[i+9] &=0b11111101;
}
}
else
{
for (int i=0;i<=40;i=i+20)
{
if (gameNumber>1) doubleResScreen[i] &= 0b01111111;
if (gameNumber==2) doubleResScreen[i+9] &=0b11111101;
}
}
}
// Draw the ball on the double-resolution screen
// This will then replace a strip across the screen at the ball
// position, ensuring a smooth ball travel
setBallPixel(ballX,0);
setBallPixel(ballX,1);
setBallPixel(ballX,2);
// Newer version of the AY-3-8500 had a wider and taller ball
if (chipVersion==2)
{
setBallPixel(ballX,3);
setBallPixel(ballX,4);
setBallPixel(ballX+1,0);
setBallPixel(ballX+1,1);
setBallPixel(ballX+1,2);
setBallPixel(ballX+1,3);
setBallPixel(ballX+1,4);
}
}
void drawPaddle(int paddleNum, int val)
{
int paddleX=0;
if (paddleNum==1) // Left player left paddle
{
paddleX=bat1X;
}
if (paddleNum==5) // Right player right paddle
{
paddleX=bat5X;
}
if (paddleNum==4) // Left player mid/squash paddle
{
paddleX=bat4X;
}
if (paddleNum==2) // Right player mid paddle
{
paddleX=bat2X;
}
if (paddleNum==3) // Right player squash paddle
{
paddleX=bat3X;
}
int y1=val;
int y2=val+paddleHeight;
if (y1<1)
{
y1=1;
}
if (y2>fieldHeight-2)
{
y2=fieldHeight-2;
}
drawLine(paddleX,y1,paddleX,y2,1);
// Intercept ball
if (ballVisible==1 && attractMode==0)
{
if ((ballXInc<0 && (ballX==paddleX || ballX == (paddleX+1)))
|| (ballXInc>0 && (ballX==(paddleX-ballWidth) || ballX == (paddleX-ballWidth+1))))
{
if (ballY>=val-1 && ballY<=val+paddleHeight)
{
beep(1000,32);
if (gameNumber==3) //Squash
{
if (ballXInc<0 || (squashActivePlayer==1 && paddleNum!=4) || (squashActivePlayer==2 && paddleNum!=3))
{
// Either the bat is not active or it is passing through the left bat
// so don't intercept on this game
return;
}
if (ballXInc>0 && (squashActivePlayer==1 && paddleNum==4))
{
ballXInc=-ballXInc;
squashActivePlayer=2;
}
if (ballXInc>0 && (squashActivePlayer==2 && paddleNum==3))
{
ballXInc=-ballXInc;
squashActivePlayer=1;
}
}
else if (gameNumber==4) //Solo
{
if (ballXInc>0)
{
ballXInc=-ballXInc;
squashActivePlayer=2;
}
}
else
{
if (ballXInc<0 && (paddleNum==1 || paddleNum==4))
{
ballXInc=-ballXInc;
}
if (ballXInc>0 && (paddleNum==2 || paddleNum==3 || paddleNum==5))
{
ballXInc=-ballXInc;
}
}
if (bigAngles==1 && ballY<val+paddleHeight/4)
{
if (ballSpeed==1)
{
ballYInc=-3;
}
else
{
ballYInc=-5;
}
}
else if (ballY<val+paddleHeight/2)
{
ballYInc=-ballSpeed;
}
else if (bigAngles==0 || ballY<val+paddleHeight*3/4)
{
ballYInc=ballSpeed;
}
else
{
if (ballSpeed==1)
{
ballYInc=3;
}
else
{
ballYInc=5;
}
}
}
}
}
}
void configureSelectedChipVersion()
{
// The two versions of the AY-3-8500 have different widths, so set the positions
// depending on the version selected.
if (digitalRead(VERSION_SEL)==1)
{
chipVersion=2;
fieldWidth=74;
bat1X=3;
bat2X=19;
bat3X=52;
bat4X=54;
bat5X=70;
leftDig1X=19;
leftDig2X=27;
centreX=36;
rightDig1X=37;
rightDig2X=45;
edgeWidth=2;
ballWidth=2;
}
else
{
chipVersion=1;
fieldWidth=79;
bat1X=2;
bat2X=18;
bat3X=58;
bat4X=60;
bat5X=76;
leftDig1X=21;
leftDig2X=29;
centreX=40;
rightDig1X=43;
rightDig2X=51;
edgeWidth=1;
ballWidth=1;
}
}
void setGameOptionsFromSwitches()
{
if (digitalRead(SPEED_SW)==1)
{
// Switch to higher speed if not already set
if (ballSpeed==1)
{
ballXInc=ballXInc*2;
if (ballYInc==-1 || ballYInc==1)
{
ballYInc=ballYInc*2; // from 1 line to 2 lines per step
}
else if (ballYInc<-1)
{
ballYInc=-5; // from 3 lines to 5 lines per step
}
else if (ballYInc>1)
{
ballYInc=5; // from 3 lines to 5 lines per step
}
ballSpeed=2;
}
}
else
{
// Switch to lower speed if not already set
if (ballSpeed==2)
{
ballXInc=ballXInc/2;
if (ballYInc==-2 || ballYInc==2)
{
ballYInc=ballYInc/2; // from 2 lines down to 1 line per step
}
else if (ballYInc<-1)
{
ballYInc=-3; // from 5 lines down to 3 lines per step
}
else if (ballYInc>1)
{
ballYInc=3; // from 5 lines down to 3 lines per step
}
ballSpeed=1;
}
}
if (digitalRead(ANGLES_SW)==1)
{
bigAngles=1;
}
else
{
bigAngles=0;
}
if (digitalRead(PADDLE_SIZE_SW)==1)
{
paddleHeight=14;
}
else
{
paddleHeight=7;
}
}
void drawPaddles()
{
// Read digital up/down
if (NKuseDigitalPaddles == true )
{
NKframeCount++; // We're here again
if (NKframeCount > NKframeTrig) // Time to read buttons this time round
{
NKframeCount = 0;
if (digitalRead(NKleftUp) == 0)
{
NKdigitalLeft = (NKdigitalLeft + NKupDownStep);
if (NKdigitalLeft > 1023) { NKdigitalLeft = 1023; }
}
if (digitalRead(NKleftDown) == 0)
{
NKdigitalLeft = (NKdigitalLeft - NKupDownStep);
if (NKdigitalLeft < 0) { NKdigitalLeft = 0; }
}
if (digitalRead(NKrightUp) == 0)
{
NKdigitalRight = (NKdigitalRight + NKupDownStep);
if (NKdigitalRight > 1023) { NKdigitalRight = 1023; }
}
if (digitalRead(NKrightDown) == 0)
{
NKdigitalRight = (NKdigitalRight - NKupDownStep);
if (NKdigitalRight < 0) { NKdigitalRight = 0; }
}
} // End frame count determines time to read pins
leftPaddle = NKdigitalLeft;
rightPaddle = NKdigitalRight;
} // End reading digital pad pins
else // Analogue pins
{
leftPaddle=analogRead(LEFT_POTENTIOMETER);
rightPaddle=analogRead(RIGHT_POTENTIOMETER);
}
leftPaddle=map(leftPaddle,0,1023,2-paddleHeight,fieldHeight-2);
rightPaddle=map(rightPaddle,0,1023,2-paddleHeight,fieldHeight-2);
if (gameNumber==1 || gameNumber==2) // Tennis or Football
{
drawPaddle(1,leftPaddle);
drawPaddle(5,rightPaddle);
}
if (gameNumber==2) // Football
{
drawPaddle(4,leftPaddle);
drawPaddle(2,rightPaddle);
}
if (gameNumber==3) // Squash
{
drawPaddle(4,leftPaddle);
drawPaddle(3,rightPaddle);
}
if (gameNumber==4) // Solo
{
drawPaddle(3,rightPaddle);
}
}
void loop()
{
// Synchronise, so that this runs once per frame
delay_frame(1);
configureSelectedChipVersion();
clearScreen();
setGame();
drawBoundary();
drawScores();
setGameOptionsFromSwitches();
drawPaddles();
// If reset button is pressed then don't proceed any further
if (digitalRead(RESET_SW)==0)
{
reset();
return;
}
// Update the ball position
ballX=ballX+ballXInc;
virtualBallY=virtualBallY+ballYInc;
ballY=virtualBallY/2;
if (virtualBallY<0)
{
ballY=(virtualBallY-1)/2;
}
// Check if a boundary has been hit
if (gameNumber==2 && ballVisible==1) // Football - left/right partial wall
{
if (ballY<soccerEdgeHeight+2 || ballY>fieldHeight-soccerEdgeHeight-1)
{
if (ballXInc<0 && ballX<=edgeWidth && ballX>=0)
{
ballXInc=-ballXInc;
beep(500,32);
}
if (ballXInc>0 && ballX >= fieldWidth-ballWidth-edgeWidth && ballX < fieldWidth)
{
ballXInc=-ballXInc;
beep(500,32);
}
}
}
if (gameNumber>=3 && ballVisible==1) // Squash or Solo - left full wall
{
if (ballXInc<0 && ballX<=edgeWidth)
{
ballXInc=-ballXInc;
beep(500,32);
}
}
// Check if the ball has exited the left or right side of the field, and update the score as required
if (ballXInc>0 && ballX >= fieldWidth)
{
ballXInc=-ballXInc;
if (ballVisible==0)
{
ballVisible=1;
}
else
{
beep(2000,32);
if (attractMode==0)
{
if (gameNumber==3)
{
if (squashActivePlayer==1)
{
scoreRight=scoreRight+1;
}
else
{
scoreLeft=scoreLeft+1;
}
}
else
{
scoreLeft=scoreLeft+1;
}
if (scoreLeft>=15 || scoreRight>=15)
{
attractMode=1;
}
}
ballX=fieldWidth*0.7;
ballVisible=0;
}
}
if (ballXInc<0 && ballX <= -ballWidth)
{
ballXInc=-ballXInc;
if (ballVisible==0)
{
ballVisible=1;
}
else
{
beep(2000,32);
if (attractMode==0)
{
scoreRight=scoreRight+1;
if (scoreRight>=15)
{
attractMode=1;
}
}
ballX=fieldWidth*0.3;
ballVisible=0;
}
}
// Bounce the ball off the top or bottom edge if needed
if ((ballYInc<0 && ballY<2) || (ballYInc>0 && ballY > fieldHeight-4))
{
ballYInc=-ballYInc;
beep(500,32);