Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //pong game was created by joel comeau 7873238 for assingment 2 of comp 1010
- int x; //x coordinate
- int y; //y coordinate
- int w; //width
- int h; //height
- int speedX; //speed variables
- int speedY;
- int paddleXL;//padle variables
- int paddleYL;
- int paddleWidth;
- int paddleHeight;
- int paddleSpeed;
- int paddleXR;
- int paddleYR;
- boolean downL;
- boolean downR;
- boolean upL;
- boolean upR;
- //color for the paddles
- int paddleColorR = color (0,40,255);
- int paddleColorL = color (0,40,255);
- //score
- int scoreL = 0;
- int scoreR = 0;
- //final win score
- int winScore = 10;
- //Sets the game and window size up, along with the background and other important things
- //happens only once; at the beginning of the game
- void setup() {
- //this sets the window size
- size(650,500);
- //the x,y,w,h get the ball to begin at the center of the game
- x = width/2;
- y = height/2;
- w = 35;
- h = 35;
- //this allows the ball to bounce
- speedX = 2;
- speedY = 2;
- //this creates a text
- textSize(30);
- textAlign(CENTER, CENTER); //this centers the height and width of the text
- rectMode(CENTER); //this allows both paddles to be centered
- //This sets the paddles to a certain place on the window, and makes them visible
- paddleXL = 20;
- paddleYL = height/2;
- paddleXR = width-40;
- paddleYR = height/2;
- paddleWidth = 15;
- paddleHeight = 100;
- paddleSpeed = 5;
- }
- //Draws the canvas for the game
- void draw() {
- background(0,22,139);
- drawCircle();
- moveCircle();
- bounceOff();
- drawPaddles();
- movePaddle();
- restrictPaddle();
- contactPaddle();
- drawScore();
- gameOver();
- }
- void drawScore() {
- textSize(20);
- fill (255);
- String toPrint = "PLAYER 2: "+scoreR;text(toPrint, width/4-textWidth(toPrint)/2, 50);
- toPrint = "PLAYER 1: "+scoreL;
- text(toPrint, width*3/4-textWidth(toPrint)/2, 50);
- }
- //creates paddles
- void drawPaddles() {
- //left paddle
- fill(paddleColorL);
- rect(paddleXL, paddleYL, paddleWidth, paddleHeight);
- //right paddle
- fill(paddleColorR);
- rect(paddleXR, paddleYR, paddleWidth, paddleHeight);
- }
- //Moves the paddles
- void movePaddle() {
- if (downL) {
- paddleYL = paddleYL + paddleSpeed;
- }
- if (upL) {
- paddleYL = paddleYL - paddleSpeed;
- }
- if (downR) {
- paddleYR = paddleYR + paddleSpeed;
- }
- if (upR) {
- paddleYR = paddleYR - paddleSpeed;
- }
- }
- //detects when a paddle has hit the wall
- void restrictPaddle() {
- if (paddleYL - paddleHeight/2 < 0) {
- paddleYL = paddleYL + paddleSpeed;
- }
- if (paddleYL + paddleHeight/2 > height) {
- paddleYL = paddleYL - paddleSpeed;
- }
- if (paddleYR - paddleHeight/2 < 0) {
- paddleYR = paddleYR + paddleSpeed;
- }
- if (paddleYR + paddleHeight/2 > height) {
- paddleYR = paddleYR - paddleSpeed;
- }
- }
- //This detects left and right collision; respectively
- void contactPaddle() {
- if (x - w/2 < paddleXL + paddleWidth/2 && y - h/2 < paddleYL + paddleHeight/2 && y + h/2 > paddleYL - paddleHeight/2 ) {
- if (speedX < 0) {
- speedX = -speedX*1;
- }
- }
- else if (x + w/2 > paddleXR - paddleWidth/2 && y - h/2 < paddleYR + paddleHeight/2 && y + h/2 > paddleYR - paddleHeight/2 ) {
- if (speedX > 0) {
- speedX = -speedX*1;
- }
- }
- }
- //creates circle
- void drawCircle() {
- fill(255);
- ellipse(x, y, w, h);
- }
- //this allows the circle to bounce
- void moveCircle() {
- x = x + speedX*2;
- y = y + speedY*2;
- }
- //This detects if the ball has bounced or not
- void bounceOff() {
- if ( x > width - w/2) {
- setup();
- speedX = -speedX;
- scoreL = scoreL + 1;
- } else if ( x < 0 + w/2) {
- setup();
- scoreR = scoreR + 1;
- }
- if ( y > height - h/2) {
- speedY = -speedY;
- } else if ( y < 0 + h/2) {
- speedY = -speedY;
- }
- }
- //This determines who wins
- void gameOver() {
- if(scoreL == winScore) {
- gameOverPage("PLAYER 1 wins!", paddleColorL);
- }
- if(scoreR == winScore) {
- gameOverPage("PLAYER 2 wins!", paddleColorR);
- }
- }
- //This stops the ball from bouncing around, gets the user to know that the game is over
- void gameOverPage(String text, color c) {
- speedX = 0;
- speedY = 0;
- fill(255);
- text("Game over", width/2, height/3 - 40);
- text("Click to play again", width/2, height/3 + 40);
- fill(c);
- text(text, width/2, height/3);
- //this gets the user to click on the screen to play again
- if(mousePressed) {
- //this sets the score to zero
- scoreR = 0;
- scoreL = 0;
- //this gets the speed to a 1
- speedX = 1;
- speedY = 1;
- }
- }
- //this gets the user to press a key and allow the paddle(s) to move
- //the second set assumes if caps lock is on for s and w
- void keyPressed() {
- if (key == 'w' || key == 'W') {
- upL = true;
- }
- if (key == 's' || key == 'S') {
- downL = true;
- }
- if (keyCode == UP) {
- upR = true;
- }
- if (keyCode == DOWN) {
- downR = true;
- }
- }
- //this creates a false key interaction
- void keyReleased() {
- if (key == 'w' || key == 'W') {
- upL = false;
- }
- if (key == 's' || key == 'S') {
- downL = false;
- }
- if (keyCode == UP) {
- upR = false;
- }
- if (keyCode == DOWN) {
- downR = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement