We are about to switch to a new forum software. Until then we have removed the registration on this forum.
int playerscore=0; int aiscore=0; float ballspeed = 3; float aipaddleSpeed=5; Paddle paddle; Paddle aipaddle; Ball ball; boolean keypress = false; boolean endgame = false; PImage background; PImage headball;
void setup () { size(600, 400); smooth(); aipaddle = new Paddle (20, 200); paddle = new Paddle (580, 200); ball = new Ball(); }
void draw() { //back ground background=loadImage("bg1");
//show score fill(0,0,255); textSize(60); text(playerscore,width/2+23,50); fill(255,0,0); text(aiscore,width/2-47,50);
//displays paddle.display(); aipaddle.display(); ball.display(); if(endgame){ textSize(25); fill(255); text("Press Enter Key", width/2+width/4-80, height/2+135); text("To Start Again", width/2+width/4-80, height/2+160); textSize(50); text("End\nof\nGame",width/2-width/4+200,height/2-80); whowins(); } else{ if(keypress==true){ ball.move(ballspeed); paddle.moveY(mouseY); aipaddle.aiAi(ball); aipaddle.intersectRight(ball);
paddle.intersectLeft(ball);
ball.checkGoal();
}
else{
textSize(20);
fill(255);
if(aiscore>=lastlevel || playerscore>=lastlevel){
endgame=true;
}
else{
text("Press Enter Key", width/2+width/4-80, height/2+25);
text("To Start", width/2+width/4-40, height/2+50);
}
}
} }
void whowins(){ if (aiscore >= lastlevel || playerscore >= lastlevel) { textSize(30);
if (aiscore > playerscore) {
fill(0, 255, 0, 180);
text("AI Wins", width/4-80, 50);
fill(255, 0, 0, 180);
text("You Lose", width/2+80, 50);
}
else {
fill(255, 0, 0, 180);
text("AI Lose", width/4-80, 50);
fill(0, 255, 0, 180);
text("You Wins", width/2+80, 50);
}
} }
//start & pause void keyPressed() { ballspeed = 5; if ( key == ENTER) { if (endgame) { endgame = false; aiscore = 0; playerscore = 0; } else { keypress = !keypress; } } }
class Ball { int x = width/2; int y = height/2; int r = 7; //radius float speed; int directionX = 1; int directionY = 1; Ball() { }
void display() { loadImage("headball.png");
}
void move(float tspeed) { speed = tspeed; x += speed * directionX; y += speed * directionY; //boundries if ( y < r) { y = r; directionY = -directionY; } else if ( y > height-r) { y = height-r; directionY = -directionY; } }
void checkGoal() { if ( x > width+r) { x = width/2; y = height/2; keypress = false; aiscore = aiscore+1; directionX = -directionX; directionY = -directionY; println("Score for AI!"); } else if (x < -r) { x = width/2; y = height/2; keypress = false; playerscore = playerscore+1; directionX = -directionX; directionY = -directionY; println("Score for Player"); } }
class Paddle{ int x; int y; int w= 20; int h= 80; Paddle(int tx, int ty){ x = tx; y = ty; }
void display(){ rectMode(CENTER); loadImage("head.png"); }
void moveY(int ty){ y= ty; if(y>height-h/2){ y= height-h/2; } else if (y<0+h/2){ y=h/2; } }
void aiAi(Ball b) { if (b.x <= width/2) { if (y < p.y-p.r2) { y +=aipaddleSpeed; } else if (y > b.y +b.r2) { y -= aipaddleSpeed; } } else { if (y < height/2) { y+=aipaddleSpeed; ; } if ( y > height/2) { y-=aipaddleSpeed; } } }
void intersectLeft(ball b) { if (b.x > x-w/2 && (b.y > y - h/2 && b.y < y + h/2)) { b.directionX = -p.directionX; ballspeed+=0.3; println(ballspeed); } }
void intersectRight(ball b) { if (b.x < x+w/2 && (b.y > y - h/2 && b.y < y + h/2)) { b.directionX = -p.directionX; ballspeed+=0.3; println(ballspeed); } } }
//why,,, I already insert images also in data folder.
Answers
Edit post, highlight code, press Ctrl-o to format code.
Please don't double post questions.
In what way doesn't it work? Don't just post screenfuls of code and say it doesn't work.