Making a computer player in Pong that can lose
in
Programming Questions
•
2 years ago
Hello all!
I have a slight problem. I have made a pong game that involves a human player and a computer player. The computer player is programmed by the paddle tracking the balls exact y position. But beacuse of this, the ball cannot get past the computers paddle at all!
Does anyone know of a way to make the computer's paddle a bit slower than the ball but still track the position of the ball so that a human has a chance of scoring?
here is the code for the game:
- //Variables for ball
- float ball_x; //balls horizontal position
- float ball_y; //balls vertical position
- float ball_hori_speed = 2; //ball speed in x axis
- float ball_size = 15; // ball size according to it's radius
- float ball_vert_speed = 1; // ball speed in y axis
- //Paddle Variables
- int pad_width = 10; //paddle's Width
- int pad_height = 30; //paddle's height
- //score variable
- int comp_score; //Computer's score
- int hu_score; //Human's score
- //General variables
- color c = color(105,255,98); //Colour corresponding the colour selector
- PFont font; //fonts variable
- void setup(){
- size(400,400); //Window size
- noStroke(); //no outline for game ball
- smooth(); //where this is declared shapes have smooth edges
- font = loadFont("LucidaConsole-26.vlw"); //where "font" is declared. use this font type and size
- textFont(font); //Use the "font" varaible
- ball_y = height/2; //ball's vertical position
- ball_x = width/2; //ball's horizonal position
- }
- void draw(){
- background(0);
- rectMode(RADIUS);
- ellipseMode(RADIUS);
- //text
- fill(c);
- textSize(20);
- text("Human",3,22);
- text("Computer",300,22);
- text(hu_score, 150, 22);
- text(comp_score,250,22);
- stroke(c);
- line(0,27,400,27);
- //center line
- for(int i = 27; i < 400; i += 30){
- line(200,i,200,i + 10);
- }
- ball_x += ball_hori_speed; //adds speed to ball hori position
- ball_y += ball_vert_speed; //adds speed to ball vert pos
- if(ball_x > width+ball_size) { //if hori pos is greater than screen width+ball size
- ball_x = -width/2 - ball_size; //ball hori pos is minus half width minus ball size
- ball_y = random(27, height);
- }
- // Constrain paddle to screen
- float cpad_y = constrain(ball_y, 59 , height); //tracks ball y position
- float pad_y = constrain(mouseY, 59 , height);
- // Test to see if the ball is touching the paddle
- float py = width-15 - pad_width - ball_size;
- if(ball_x == py
- && ball_y > cpad_y - pad_height - ball_size
- && ball_y < cpad_y + pad_height + ball_size) {
- ball_hori_speed *= -1;
- if(mouseY != pmouseY) {
- ball_vert_speed = (mouseY-pmouseY)/2.0;
- if(ball_vert_speed > 5) { ball_vert_speed = 5; }
- if(ball_vert_speed < -5) { ball_vert_speed = -5; }
- }
- }
- float py1 = 15 + pad_width + ball_size;
- if(ball_x == py1
- && ball_y > pad_y - pad_height - ball_size
- && ball_y < pad_y + pad_height + ball_size) {
- ball_hori_speed *= -1;
- if(mouseY != pmouseY) {
- ball_vert_speed = (mouseY-pmouseY)/2.0;
- if(ball_vert_speed > 5) { ball_vert_speed = 5; }
- if(ball_vert_speed < -5) { ball_vert_speed = -5; }
- }
- }
- //Scoring system and ball position reset
- if(ball_x < -width/4){
- comp_score = comp_score +1; ball_x = 200;
- }
- if(comp_score >= 5){
- ball_vert_speed = 0; ball_hori_speed = 0;
- text("Game Over\nComputer Wins!",210,175);
- }
- if((comp_score == 5) && (mousePressed)){
- exit();
- }
- //Human score
- if(ball_x > width){
- hu_score = hu_score +1; ball_x = 200;
- }
- if(hu_score >= 15){
- ball_vert_speed = 0; ball_hori_speed = 0;
- text("Game Over\nyou win",60,175);
- }
- // If the ball is touching top or bottom edge, reverse direction
- if(ball_y > height-ball_size) {
- ball_vert_speed = ball_vert_speed * -1;
- }
- if(ball_y < ball_size+27) {
- ball_vert_speed = ball_vert_speed * -1;
- }
- // Draw ball
- fill(c);
- noStroke();
- ellipse(ball_x, ball_y, ball_size, ball_size);
- // Draw the paddles
- fill(c);
- rect(width-15, cpad_y, pad_width, pad_height);
- rect(15,pad_y,pad_width,pad_height);
- }
Any help would be truely awesome!
Thanks
Ian787
1