-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideoPong.pde
177 lines (149 loc) · 4.43 KB
/
VideoPong.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This is version 0.16 of VideoPong, made by VJ pixel (http://memelab.com.br) and Tiago Pimentel.
*/
// Kinect
import org.openkinect.freenect.*;
import org.openkinect.processing.*;
Kinect kinect;
// Depth image
PImage depthImg;
// Which pixels do we care about?
int minDepth = 100;
int maxDepth = 700;
// What is the kinect's angle
float angle;
// audio
import ddf.minim.*;
AudioSample rSound, lSound;
Minim minim;
// Pong
PFont scoreFont;
PFont playerFont;
int xDirection = 1;
int yDirection = 1;
int circleDia = 10; // Diameter of the circle/square
float circleRad = circleDia/2; // Radius of the circle/square
float xBallPos, yBallPos; // Variables for the position of the ball and the paddles
int paddleWidth = 10;
int paddleHeight = 60;
int lPlayerScore = 0;
int rPlayerScore = 0;
float origSpeed = 6;
float speed = origSpeed; // Speed of the ball. This increments as the game runs, to make it move faster
float speedInc = origSpeed/10;
float ySpeed = 0; // Start the ball completely flat. Position of impact on the paddle will determine reflection angle.
int paused = 1; // Enable pausing of the game. Start paused
int paddleJump = 5; // How fast the paddles move every frame
int ry1, ly1;
char player = ' ';
int end = 5;
void setup() {
size(640, 480);
//fullScreen(1);
minim = new Minim(this);
rSound = minim.loadSample("rSound.mp3");
lSound = minim.loadSample("lSound.mp3");
kinect = new Kinect(this);
kinect.initDepth();
angle = kinect.getTilt();
// Blank image
depthImg = new PImage(kinect.width, kinect.height);
smooth();
noStroke();
scoreFont = loadFont("AndaleMono-24.vlw"); // Score font
playerFont = loadFont("AndaleMono-10.vlw"); // Player font
frameRate(30);
xBallPos = width/2; // Start the ball in the center
yBallPos = height/2;
ry1 = height/2;
ly1 = height/2;
}
void draw() {
input(); // camera input
// Define the boundaries
if ((xBallPos > width-circleRad-20) && ((yBallPos >= ry1-paddleHeight/2) && (yBallPos <= ry1+paddleHeight/2))) { // if it's is touching right paddle
xDirection = -1; // Make the ball move from right to left
ySpeed = (ry1 + yBallPos) / 75; // Make position of impact on paddle determine deflection angle. Not perfect.
speed += speedInc;
ping(); // Sound
}
if ((xBallPos < circleRad+20) && ((yBallPos >= ly1-paddleHeight/2) && (yBallPos <= ly1+paddleHeight/2))) { // if it's is touching left paddle
xDirection = 1; // Make the ball move from left to right
ySpeed = (ly1 + yBallPos) / 75; // Make position of impact on paddle determine deflection angle. Not perfect.
speed += speedInc;
pong(); // Sound
}
if ((yBallPos > height-circleRad) || (yBallPos < circleRad)) {
yDirection = -yDirection;
}
if (xBallPos > width) { // If the ball goes off the screen to the right
lPlayerScore++;
speed = origSpeed;
xBallPos = width/2;
ySpeed = random(-1., 1.);
if (lPlayerScore == end) { // left player wins
player = 'l';
paused = 1;
}
}
if (xBallPos < 0) { // If the ball goes off the screen to the left
rPlayerScore++;
speed = origSpeed;
xBallPos = width/2;
ySpeed = random(-1., 1.);
if (rPlayerScore == end) { // right player wins
player = 'r';
paused = 1;
}
}
output(); // output in the screen (text, paddles, ball)
} // End of draw loop
void mousePressed() {
if (paused == 1) {
paused = 0;
redraw();
} else if (paused == 0) {
paused = 1;
redraw();
}
}
// Adjust the angle and the depth threshold min and max
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
angle++;
} else if (keyCode == DOWN) {
angle--;
}
angle = constrain(angle, 0, 30);
kinect.setTilt(angle);
} else if (key == 'a') {
minDepth = constrain(minDepth+10, 0, maxDepth);
} else if (key == 's') {
minDepth = constrain(minDepth-10, 0, maxDepth);
} else if (key == 'z') {
maxDepth = constrain(maxDepth+10, minDepth, 2047);
} else if (key =='x') {
maxDepth = constrain(maxDepth-10, minDepth, 2047);
} else if ((key == '.') || (key == ' ')) {
if (paused == 1) {
paused = 0;
redraw();
} else if (paused == 0) {
paused = 1;
redraw();
}
}
}
void ping() {
rSound.trigger();
}
void pong() {
lSound.trigger();
}
void stop() {
// always close Minim audio classes when you are done with them
rSound.close();
lSound.close();
minim.stop();
super.stop();
}