forked from mhenstell/acw
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Pong.pde
158 lines (133 loc) · 2.99 KB
/
Pong.pde
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
class Pong extends Routine {
float ballX;
float ballY;
float ballAngle;
float ballSpeed;
int paddleY;
int paddleX;
int paddleSize;
long flashUntilFrame;
long ballFlashUntilFrame;
boolean deathLeft;
int lives;
void setup(PApplet parent) {
ballY = 0;
ballX = 0;
ballAngle = radians(45);
deathLeft = false;
ballSpeed = 0.125;
lives = 3;
paddleX = Config.WIDTH/2;
paddleY = 0;
paddleSize = Config.HEIGHT/8;
ballFlashUntilFrame = frameCount + (long)frameRate * 3;
}
void draw() {
if (frameCount < flashUntilFrame) {
draw.background(millis() % 255, (millis() % 255)/2, 0);
}
else if (lives > 0) {
drawGameplay();
}
else {
newMode();
}
}
void drawGameplay() {
draw.background(0);
if (frameCount > ballFlashUntilFrame) {
moveBall();
}
movePaddle();
drawBall();
drawPaddle();
}
void drawBall() {
if (frameCount < ballFlashUntilFrame) {
draw.stroke(millis() % 255, (millis() % 255)/2, 0);
}
else {
draw.stroke(255);
}
draw.point(ballX, ballY);
}
void drawPaddle() {
for (int i=0; i<paddleSize; i++) {
draw.stroke(255, 64, 64);
draw.point(paddleX, paddleY+i);
}
}
void movePaddle() {
if ((keyPressed && keyCode == UP) || controller.buttonUp) {
paddleY--;
}
else if ((keyPressed && keyCode == DOWN) || controller.buttonDown) {
paddleY++;
}
if (paddleY > Config.HEIGHT-paddleSize) {
paddleY = Config.HEIGHT-paddleSize;
}
else if (paddleY < 0) {
paddleY = 0;
}
}
void moveBall() {
float xVec = ballSpeed * cos(ballAngle);
float yVec = ballSpeed * sin(ballAngle);
if (abs(xVec) > 1) {
println("Ball is moving too fast. "+xVec);
}
ballX += xVec;
ballY += yVec;
if (ballY >= Config.HEIGHT) {
ballY = Config.HEIGHT-1;
bounceY();
}
else if (ballY < 0) {
ballY = 0;
bounceY();
}
if (ballX >= Config.WIDTH) {
ballX = ballX - Config.WIDTH;
if (!deathLeft) {
playerDied();
}
else {
deathLeft = false;
}
}
else if (ballX < 0) {
ballX = Config.WIDTH + ballX;
if (deathLeft) {
playerDied();
}
else {
deathLeft = true;
}
}
if (int(ballX) == paddleX && ballY >= paddleY && ballY <= paddleY+paddleSize) {
bounceX((paddleY+paddleSize-ballY-paddleSize/2)/(paddleSize/2.0));
if (deathLeft) {
ballX++;
}
else {
ballX--;
}
}
}
void playerDied() {
ballY = paddleY;
ballX = 2;
ballAngle = radians(45);
deathLeft = false;
flashUntilFrame = frameCount + (long)frameRate*3;
ballFlashUntilFrame = flashUntilFrame + (long)frameRate*3;
lives--;
}
void bounceX(float spin) {
ballAngle = radians(180 - degrees(ballAngle) - spin*10);
}
void bounceY() {
ballAngle = radians(360 - degrees(ballAngle) + (random(40)-20));
}
}