forked from coolmandudebro/engg200
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPONG.ino
125 lines (85 loc) · 2.39 KB
/
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
#include <TFT_eSPI.h>
//create the screen variable from the library
TFT_eSPI tft = TFT_eSPI();
// Set up variables for the cursor and counter. Cursor starts in the middle of the screen.
int paddleX = 50;
int paddleY = 0;
int oldPaddleX, oldPaddleY;
int ballDirectionX = 1;
int ballDirectionY = 1;
int ballX, ballY, oldBallX, oldBallY;
int ballSpeed = 10; // lower numbers are faster
int width = 10;
int height = 40;
int player1;
int player2;
// Define colours in 4-digit hex
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Setting the joystick pins here so we can easily change them
#define JOYSTICK_X_PIN A14
#define JOYSTICK_Y_PIN A15
#define JOYSTICK_BUTTON_PIN 23
void setup() {
// put your setup code here, to run once:
// Initalize the screen and set the orientation correctly, then make sure it's clear.
tft.begin();
tft.setRotation(1);
tft.fillScreen(BLACK);
}
void loop() {
// put your main code here, to run repeatedly:
int myWidth = tft.width();
int myHeight = tft.height();
paddleY = paddleY + map(analogRead(A15), 0, 1023, -1, 1);
if (0 >= paddleY)
paddleY = 0;
if (280 <= (paddleY))
paddleY = 280;
if (oldPaddleY != paddleY) {
tft.fillRect(oldPaddleX, oldPaddleY, width, height, BLACK);
}
tft.fillRect(paddleX, paddleY, width, height, WHITE);
oldPaddleX = paddleX;
oldPaddleY = paddleY;
// update the ball's position and draw it on screen
if (millis() % ballSpeed < 2) {
moveBall();
}
}
void moveBall() {
if (ballX > tft.width()) {
player2 += 1;
}
if (ballX < 0) {
player1 += 1;
}
if (ballY > tft.height() || ballY < 0) {
ballDirectionY = -ballDirectionY;
}
if (inPaddle(ballX, ballY, paddleX, paddleY, width, height)) {
ballDirectionX = -ballDirectionX;
}
ballX += ballDirectionX;
ballY += ballDirectionY;
if (oldBallX != ballX || oldBallY != ballY) {
tft.fillCircle(oldBallX, oldBallY, 5, BLACK);
}
tft.fillCircle(ballX, ballY, 5, WHITE);
oldBallX = ballX;
oldBallY = ballY;
}
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
boolean result = false;
if ((x >= rectX && x <= (rectX + rectWidth)) &&
(y >= rectY && y <= (rectY + rectHeight))) {
result = true;
}
return result;
}