-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_PINg_POnG_revisefor_cchanges.pde
162 lines (149 loc) · 4.07 KB
/
new_PINg_POnG_revisefor_cchanges.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
import processing.serial.*;
Serial port;
String value;
int s1,s2,temp;
String player_1_name,player_2_name;
int ball_x, ball_y, radius, speedX, speedY;
int player_1_Y_pos,player_2_Y_pos;
int paddle_Height, paddle_Width;
int paddleXL, paddleXR;
int scoreL=0;
int scoreR=0;
int winScore=9;
color colorL1=color(200,255,10);
color colorL2=color(100,40,10);
color colorL3=color(50,255,255);
color colorR1=color(100,40,10);
color colorR2=color(50,255,255);
color colorR3=color(200,255,10);
////////////////////////////////////////////////////////
void setup(){
fullScreen();
port = new Serial(this, "COM24", 9600);
port.bufferUntil('\n');
textSize(30);
textAlign(CENTER,CENTER);
rectMode(CENTER);
paddleXL=20;
paddleXR=width-20;
paddle_Height=170;
paddle_Width=20;
ball_x=width/2;
ball_y=height/2;
radius=50;
speedX=6;
speedY=8;
}
//////////////////////////////////////////////////////
void draw(){
background(100,200,50);
port.write(scoreL);
port.write(scoreR);
movingBAll();
bounceBack();
movingPaddle();
paddleContact();
score();
gameOver();
}
void movingBAll(){
fill(255,0,0);
circle(ball_x,ball_y,radius);
ball_x=ball_x+speedX;
ball_y=ball_y+speedY;
}
///////////////////////////////////////////////////////
void bounceBack(){
if(ball_x > width-radius/3){
reStart();
speedX = -speedX;
scoreL=scoreL+1;}
else if(ball_x<0+radius/3){
scoreR=scoreR+1;
reStart();}
else if(ball_y>height-radius/3){
speedY=-speedY;}
else if(ball_y<0+radius/3){
speedY=-speedY;}
}
/////////////////////////////////////////////////////
void movingPaddle(){
if(s1==1){
fill(colorL1);}
else if(s1==2){
fill(colorL2);}
else if(s1==3){
fill(colorL3);}
rect(paddleXL,player_1_Y_pos,paddle_Width,paddle_Height);
if(s2==1){
fill(colorR1);}
else if(s2==2){
fill(colorR2);}
else if(s2==3){
fill(colorR3);}
rect(paddleXR,player_2_Y_pos,paddle_Width,paddle_Height);
}
/////////////////////////////////////////////////////////////
void paddleContact(){
if(ball_x-radius/2<paddleXL+paddle_Width/2 && ball_y-radius/2 < player_1_Y_pos+paddle_Height/2 && ball_y+radius/2 > player_1_Y_pos-paddle_Height/2){
if(speedX < 0){
speedX=speedX-2; speedX = -speedX;}
}
if(ball_x+radius/2 > paddleXR - paddle_Width/2 && ball_y+radius/2 > player_2_Y_pos - paddle_Height/2 && ball_y-radius/2 < player_2_Y_pos + paddle_Height/2){
if(speedX > 0){
speedX=speedX+2; speedX=-speedX;}
}
}
/////////////////////////////////////////////////////////////////
void score(){
fill(0);
text(player_1_name,120,25); text(player_2_name,width-120,25); //text("Coded by : Sachin Barupal",width-180,height-30);
fill(250,0,0);
text(scoreL,120,50); text(scoreR,width-120,50);
text("PING PONG GAME",width/2,height/2+50); //text("by Team 8 : Tech Aura",width/2+26,height/2+80);
}
//////////////////////////////////////////////////////////////////
void gameOver(){
if(scoreL == winScore){
gameOverPage(player_1_name);}
if(scoreR == winScore){
gameOverPage(player_2_name);}
}
////////////////////////////////////////////////////////////////////
void gameOverPage(String msg){
speedX=0;
speedY=0;
text("GAME OVER",width/2,height/3-40);
text("CLICK TO RESTART",width/2,height/3-80);
text(msg+" WINS!!!",width/2,height/3);
if(mousePressed){
scoreR=0;
scoreL=0;
speedX=6;
speedY=8;}
}
/////////////////////////////////////////////////////////////////
void reStart(){
ball_x=width/2;
ball_y=height/2;
radius=50;
speedX=6;
speedY=8;
}
//////////////////////////////////////////////////////////////////
void serialEvent( Serial port)
{
value = port.readStringUntil('\n');
if (value != null)
{
String[] values = split(value, ",");
player_1_name = values[0];
player_2_name = values[1];
player_1_Y_pos = int(values[2]);
player_2_Y_pos = int(values[3]);
s1 = int(values[4]) ;
s2 = int(values[5]) ;
temp = int(values[6]);
}
}
/////////////////////////////////////////////////////////////////