Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int PadSX_Y, PadDX_Y, hWd, directionX, p1_Score, p2_Score;
- float Player_Movement, Player2_Movement, Player_Speed, Ball_Speed, BallX, BallY, directionY, difficulty;
- void setup(){
- fullScreen(1);
- background(0);
- stroke(255);
- rectMode(CENTER);
- ellipseMode(CENTER);
- textSize(80);
- PadSX_Y = height/2;
- PadDX_Y = PadSX_Y;
- hWd = width/2;
- Player_Movement=0;
- Player2_Movement=0;
- Player_Speed = 5;
- Ball_Speed = 5;
- BallX = width/2;
- BallY = height/2;
- directionY=0.25;
- directionX=-1;
- difficulty=0.05;
- p1_Score=0;
- p2_Score=0;
- }
- void drawField(){
- //Creazione dei Muri
- strokeWeight(10);
- text(p1_Score, width*0.4, height*0.3);
- text(p2_Score, width*0.6-40, height*0.3);
- line(width*0.1, height*0.2, width*0.9, height*0.2);//Muro Nord
- line(width*0.1, height*0.2, width*0.1, height*0.8);//Muro Ovest
- line(width*0.1, height*0.8, width*0.9, height*0.8);//Muro Sud
- line(width*0.9, height*0.8, width*0.9, height*0.2);//Muro Est
- //Linea tratteggiata del centro campo
- strokeWeight(2);
- stroke(130);
- line(hWd, height*0.23, hWd, height*0.29);
- line(hWd, height*0.35, hWd, height*0.41);
- line(hWd, height*0.47, hWd, height*0.53);
- line(hWd, height*0.59, hWd, height*0.65);
- line(hWd, height*0.71, hWd, height*0.77);
- stroke(255);
- }
- void draw(){
- background(0);
- drawField(); //Disegno Campo
- //Disegno Padella Sinistra
- rect(width*0.125, PadSX_Y, 15, height*0.12);
- //Disegno Padella Destra
- rect(width*0.875, PadDX_Y, 15, height*0.12);
- //Disegno Palla
- rect(BallX, BallY, 30, 30);
- //Movimento Giocatore 1
- if(!keyPressed){
- Player2_Movement=0;
- }
- if( !(PadSX_Y>height*0.8-height*0.06 && Player2_Movement >0) && !(PadSX_Y<height*0.2+height*0.06 && Player2_Movement<0)){ //Questo if รจ scritto ammerda, sistemare
- PadSX_Y += Player2_Movement;
- }
- //Movimento Giocatore 2
- if(!keyPressed){
- Player_Movement=0;
- }
- if(!(PadDX_Y>height*0.8-height*0.06 && Player_Movement >0) && !(PadDX_Y<height*0.2+height*0.06 && Player_Movement<0)){ //Pure questo
- PadDX_Y += Player_Movement;
- }
- //Movimento Palla
- BallX += directionX * Ball_Speed;
- BallY += directionY*Ball_Speed;
- //Controllo Collisioni
- padCollision();
- wallCollision();
- if(BallX<width*0.125-(15/2)){
- roundReset("p1");
- }else{
- if(BallX>width*0.875+(15/2)){
- roundReset("p2");
- }
- }
- }
- void roundReset(String winner){
- if(winner=="p1"){
- p2_Score++;
- BallX=width/2;
- BallY=height/2;
- directionX=1;
- }else{
- p1_Score++;
- BallX=width/2;
- BallY=height/2;
- directionX=-1;
- }
- directionY=0.25;
- }
- void padCollision(){
- if( (BallX <width*0.125+30 && PadSX_Y+height*0.06>BallY && PadSX_Y-height*0.06<BallY ) ){
- float newDirection = map(BallY, PadSX_Y-height*0.06, PadSX_Y+height*0.06, -2, 2);
- directionY = newDirection;
- Ball_Speed+=difficulty;
- directionX*=-1;
- }
- }
- void wallCollision(){
- if( BallY<height*0.2+15 || BallY>height*0.8-15){
- directionY*=-1;
- }
- }
- void keyPressed(){
- if(keyPressed){
- if(key == CODED){
- if(keyCode == DOWN){
- Player_Movement = 0;
- Player_Movement = 1*Player_Speed;
- }else{
- if(keyCode == UP){
- Player_Movement = 0;
- Player_Movement = -1*Player_Speed;
- }
- }
- }
- if(key == 's'){
- Player2_Movement = 0;
- Player2_Movement = 1*Player_Speed;
- println("Check S");
- }else{
- if(key== 'w'){
- Player2_Movement=0;
- Player2_Movement = -1*Player_Speed;
- println("Check W");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement