forked from nennes/ArduinoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoGame.ino
140 lines (108 loc) · 3.46 KB
/
ArduinoGame.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
// Arduino LCD library
#include <TFT.h>
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
// game variables
#define PADDLE_WIDTH 30
#define PADDLE_HEIGHT 5
#define BALL_SIZE 5
#define DEBUG_PORT 7
#define PADDLE_STEP 2
#define BALL_SPEED 5
#define DELAY_MS 5
TFT TFTscreen = TFT(cs, dc, rst);
// variables for the position of the ball and paddle
int paddleX = 0;
int paddleY = 0;
int newPaddleX = 0;
int newPaddleY = 0;
int oldPaddleX, oldPaddleY;
int ballDirectionX = 1;
int ballDirectionY = 1;
int ballX, ballY, oldBallX, oldBallY;
// save the width and height of the screen
int tftWidth = TFTscreen.width();
int tftHeight = TFTscreen.height();
void setup() {
// initialize the display
TFTscreen.begin();
Serial.begin(9600);
// black background
TFTscreen.background(0, 0, 0);
// Initial paddle's location to the center of the screen
paddleX = (tftWidth/2) - PADDLE_WIDTH/2;
paddleY = (tftHeight/2) - PADDLE_HEIGHT/2;
}
void loop() {
// #update the current position
newPaddleX = paddleX + map(analogRead(A0), 0, 1023, -PADDLE_STEP, PADDLE_STEP);
if((newPaddleX >= - PADDLE_WIDTH/2) && (newPaddleX <= tftWidth - PADDLE_WIDTH/2)){
paddleX = newPaddleX;
}
newPaddleY = paddleY + map(analogRead(A1), 0, 1023, -PADDLE_STEP, PADDLE_STEP);
if((newPaddleY >= - PADDLE_HEIGHT/2) && (newPaddleY <= tftHeight - PADDLE_HEIGHT/2)){
paddleY = newPaddleY;
}
if (digitalRead(DEBUG_PORT) == LOW){
Serial.println(paddleX);
Serial.println(paddleY);
}
// set the fill color to black and erase the previous
// position of the paddle if different from present
TFTscreen.fill(0, 0, 0);
if (oldPaddleX != paddleX || oldPaddleY != paddleY) {
TFTscreen.rect(oldPaddleX, oldPaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
}
// draw the paddle on screen, save the current position
// as the previous.
TFTscreen.fill(255, 255, 255);
TFTscreen.rect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
oldPaddleX = paddleX;
oldPaddleY = paddleY;
// update the ball's position and draw it on screen
if (millis() % BALL_SPEED < 2) {
moveBall();
}
delay(DELAY_MS);
}
// this function determines the ball's position on screen
void moveBall() {
// if the ball goes offscreen, reverse the direction:
if (ballX > TFTscreen.width() || ballX < 0) {
ballDirectionX = -ballDirectionX;
}
if (ballY > TFTscreen.height() || ballY < 0) {
ballDirectionY = -ballDirectionY;
}
// check if the ball and the paddle occupy the same space on screen
if (inPaddle(ballX, ballY, paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT)) {
ballDirectionX = -ballDirectionX;
ballDirectionY = -ballDirectionY;
}
// update the ball's position
ballX += ballDirectionX;
ballY += ballDirectionY;
// erase the ball's previous position
TFTscreen.fill(0, 0, 0);
if (oldBallX != ballX || oldBallY != ballY) {
TFTscreen.rect(oldBallX, oldBallY, BALL_SIZE, BALL_SIZE);
}
// draw the ball's current position
TFTscreen.fill(255, 255, 255);
TFTscreen.rect(ballX, ballY, BALL_SIZE, BALL_SIZE);
oldBallX = ballX;
oldBallY = ballY;
}
// this function checks the position of the ball
// to see if it intersects with the paddle
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
boolean result = false;
if ((x >= (rectX - BALL_SIZE) && x <= (rectX + rectWidth)) &&
(y >= (rectY - BALL_SIZE) && y <= (rectY + rectHeight))) {
result = true;
}
return result;
}