-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardu-tennis.ino
176 lines (145 loc) · 3.19 KB
/
ardu-tennis.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
#include <stdarg.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#define BLPIN 8
#define POTLPIN 2
#define POTPIN 0
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
// missing sprinf
char* p(char *fmt, ... ) {
char buf[128];
va_list args;
va_start (args, fmt );
vsnprintf(buf, 128, fmt, args);
va_end (args);
return buf;
}
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
void setup() {
// set backlight
pinMode(BLPIN, OUTPUT);
digitalWrite(BLPIN, HIGH);
// set pot led
pinMode(POTLPIN, OUTPUT);
// turn on display
display.begin();
// set contrast
display.setContrast(56);
resetGame();
}
int level = 0;
int hits = 0;
int gameSpeed = 100;
int headerH = 8;
unsigned long previousMillis = 0;
int pot;
// ball
int ballD = 2;
int ballX = 0;
int ballY = 0;
bool ballXD = false;
bool ballYD = false;
// paddle
int paddleX = 0;
int paddleW;
int paddleH = 3;
int paddleWMin = 10;
void resetGame() {
display.clearDisplay();
drawText(0, 0, "Get ready ...");
delay(1500);
level = 1;
hits = 0;
ballX = display.width() / 2;
ballY = display.height() / 2;
ballXD = false;
ballYD = false;
paddleW = 20;
digitalWrite(POTLPIN, HIGH);
}
void hit() {
hits++;
if (hits % 5 == 0) {
if (paddleW > paddleWMin) {
paddleW -= 2;
}
level++;
}
}
void gameOver() {
display.clearDisplay();
drawPaddle();
digitalWrite(POTLPIN, LOW);
drawText(0, 0, "Game Over :(");
delay(3000);
}
void drawHeader() {
drawText(58, 0, "lvl");
drawText(display.width() - 7, 0, p("%d", level));
drawText(0, 0, p("%d", hits));
//display.fillRect(0, headerH, display.width(), 1, BLACK);
}
void drawBall() {
display.fillCircle(ballX, ballY, ballD, BLACK);
}
void drawPaddle() {
display.fillRoundRect(paddleX, display.height() - paddleH, paddleW, paddleH, 1, BLACK);
}
void drawText(int x, int y, const char* text) {
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(x, y);
display.println(text);
display.display();
}
void updatePaddle() {
pot = analogRead(POTPIN);
paddleX = map(pot, 0, 1023, 0, display.width() - paddleW);
paddleX = (paddleX - (display.width() - paddleW)) * (-1);
}
void updateBall() {
int delta = 1;
ballX += ballXD ? (-1) * delta : delta;
ballY += ballYD ? (-1) * delta : delta;
}
void nextFrame() {
// sides (horizontal)
if (ballX < ballD || ballX >= display.width() - ballD) {
ballXD = !ballXD;
}
// top
if (ballY < ballD + headerH) {
ballYD = !ballYD;
} else {
// bottom - collision with paddle
if (ballY == display.height() - paddleH - ballD) {
//if (ballX == constrain(ballX, paddleX, paddleX + paddleW)) {
if (ballX >= paddleX && ballX <= paddleX + paddleW) {
ballYD = !ballYD;
hit();
}
} else {
if (ballY > display.height() + ballD) {
gameOver();
resetGame();
}
}
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= gameSpeed) {
nextFrame();
previousMillis = currentMillis;
}
display.clearDisplay();
updatePaddle();
updateBall();
drawBall();
drawPaddle();
drawHeader();
display.display();
delay(25);
}