Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BoxallBenjaminA2Q5
- //By Ben Boxall COMP 1010 04-A
- //Due Friday, 19 October 2019
- //Pong Game
- //Setting final variables first
- final int BALL_DIAMETER = 20; //the Diameter of the ball (does not move in this program)
- final int PADDLE_HEIGHT = 100; // the height in pixels of both paddles.
- final int PADDLE_WIDTH = 20; //the width in pixels of both paddles.
- final int PADDLE_SPEED = 10; //speed in which the paddles move in pixels per frame
- final int MAX_BALL_SPEED = 12; //speed will not exceed this value
- final int MIN_BALL_SPEED = 3; //speed will not subceed this value
- //Setting global variables for the score, as well as the coordinants of the paddles.
- int keyScore, mouseScore;
- int lPaddleX, lPaddleY, rPaddleX, rPaddleY;
- //graphical variables
- int centreX, centreY;
- //These represent the position of the ball
- float ballX, ballY; //physical position of balls
- float ballVX, ballVY; //Pixels per frame that the ball moves.
- float angle; //between 0 and TWO_PI, the direction that the ball will move.
- float ballSpeed; //current speed of ball
- //while gameOver == false, the game will be paused until SPACE (keycode == 32) is pressed.
- boolean gameOver = false;
- int lastPoint; //1 and -1 respective for left and right. Toggles based on point.
- //gameModes
- boolean randomCollisions; //gamemode for random ball bouncing.
- boolean ballSpeedIncreases; //gamemode for ball speeding up on collisions
- void setup(){
- size(720, 480); //No size was specified, I chose this size because it is an excelent game size.
- frameRate(60); //As shown in the Ball Bouncing program example
- parameters(); //lets the user change some parameters
- keyScore = mouseScore = 0; //start of game sets both scores to 0.
- resetBall(); //This resets the ball to the centre
- checkAngles(); //gets ballVX and ballVY to initiate game.
- resetPaddles(); //This resets the paddles to the centre at the beginning and when a point is scored
- setGraphicalVariables();
- }
- void draw(){ //loops infinitely unless otherwise controlled
- if (!gameOver){ //
- background(100,155,200); //lovely blue.
- drawGame(); //calls visual functions
- checkIfScored(); //checks if the ball is out of bounds, then resets it and changes the score.
- }
- else {
- reset(); //waits for a reset key to be pressed (post game) to reset the game while displaying the score.
- }
- }
- void drawGame(){
- //eventually this function will include commands to call the movement functions
- drawBall();
- drawPaddles();
- moveBall();
- checkColissions();
- movePaddles();
- drawScore();
- }
- //~~~~Functions to Draw ~~~~\\
- void drawPaddles(){
- //passes the variables to PaddleDrawer for each side as to save space with code.
- paddleDrawer(lPaddleY, lPaddleX);
- paddleDrawer(rPaddleY, rPaddleX);
- }
- void paddleDrawer(int paddleY, int paddleX){
- //Thsi function is passed variables so code can be reused;
- rect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
- }
- void drawScore(){
- //the following code was copied and pasted directly as shown at /SPECIFIC INSTRUCTION/ of the assignment
- textSize(20);
- String toPrint = "Keyboard: " + keyScore;
- text(toPrint, width/4-textWidth(toPrint)/2, 50);
- toPrint = "Mouse: " + mouseScore;
- text(toPrint, width*3/4-textWidth(toPrint)/2, 50);
- }
- void drawBall(){
- //This function draws the ball
- //I thought these colors were pretty cool
- fill(255);
- strokeWeight(4);
- ellipse(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);
- }
- void moveBall(){
- ballX += ballVX;
- ballY += ballVY;
- }
- void setGraphicalVariables(){
- centreX = width/2;
- centreY = height/2;
- }
- //~~~~Functions to Calculate~~~~\\
- void movePaddles(){
- moveLeftPaddle();
- moveRightPaddle();
- }
- //Moving Paddles could not be condensed further due to nature of controls being different inputs.
- void moveLeftPaddle(){ //Left paddle controlled by UP and DOWN (inversed)
- if (keyPressed){
- if (keyCode == UP && lPaddleY > 0){
- lPaddleY -= PADDLE_SPEED; //moves paddle up (negative y)
- }
- else if (keyCode == DOWN && lPaddleY+PADDLE_HEIGHT <height){
- lPaddleY += PADDLE_SPEED; //moves paddle down (positive y)
- }
- }
- }
- void moveRightPaddle(){
- if (mouseY-PADDLE_HEIGHT/2 <= 0){
- rPaddleY = 0;
- }
- else if (mouseY+PADDLE_HEIGHT/2 >=height){
- rPaddleY = height-PADDLE_HEIGHT; //moves paddle down (positive y)
- }
- else{
- rPaddleY = mouseY-PADDLE_HEIGHT/2; //moves paddle up (negative y)
- }
- }
- //Gets random angle in a circle, but not one that is too crazy up or down.
- void getAngle(){
- if (lastPoint == -1){ //if right paddle scored
- angle = random(3*PI/4, 5*PI/4);
- }
- else if (lastPoint == 1){ //if left paddle scored
- angle = random(-QUARTER_PI/4, QUARTER_PI/4);
- }
- else{
- boolean angleChecker = true; //creates a loop that only returns legal angles.
- while(angleChecker){ //creates loop to get legal angle >|<
- angle = random(0,2*PI);
- println(angle);
- if (abs(sin(angle)) < sin(QUARTER_PI)){ //makes sure the angle is not super high or low.
- angleChecker = false; //breaks loop when angle is legal.
- }
- }
- }
- }
- void checkColissions(){
- //Y axis colissions (top and bottom)
- if (ballY <= 0+BALL_DIAMETER/2 || ballY >= height-BALL_DIAMETER/2){
- ballVY = -ballVY; //changes ball Y velocity upon contact with floor and ceiling
- }
- //collisions for paddles
- //left paddle
- if (ballY >= lPaddleY && ballY < lPaddleY+PADDLE_HEIGHT){ //if ball is within paddle height
- if (ballX < lPaddleX+PADDLE_WIDTH+BALL_DIAMETER/2 && ballX > lPaddleX+BALL_DIAMETER/2){//and within paddle width
- if (randomCollisions){
- angle = random(-PI/3, PI/3);
- }
- else{
- //collide based on where the ball is on paddle. Uses percent of paddle for angle range.
- angle = (2*THIRD_PI*((ballY-lPaddleY)/PADDLE_HEIGHT))-THIRD_PI;
- }
- checkAngles(1); //1 = multiply cosX by 1 for x (left going right)
- }
- }
- if (ballY > rPaddleY && ballY < rPaddleY+PADDLE_HEIGHT){
- if (ballX > rPaddleX-BALL_DIAMETER/2 && ballX < rPaddleX){
- if (randomCollisions){
- angle = random(-PI/3, PI/3);
- }
- else{
- angle = (2*THIRD_PI*((ballY-rPaddleY)/PADDLE_HEIGHT))-THIRD_PI;
- }
- checkAngles(-1);//-1 = multiply cosX by -1 for x (right going left)
- }
- }
- }
- void checkAngles(){//non-collision (new ball)
- ballVX = ballSpeed * cos(angle);
- ballVY = ballSpeed * sin(angle);
- }
- void checkAngles(int paddle){ //collision
- if (ballSpeedIncreases){
- ballSpeed *= 1.125; //raises ball speed by an eighth.
- }
- ballVX = ballSpeed*cos(angle)*paddle; //if right paddle, COS is made negative
- ballVY = ballSpeed*sin(angle);
- }
- void checkIfScored(){
- if (ballX < 0){ //OPTION: MOUSE SCORES
- mouseScore ++;
- lastPoint = 1; //value to decide which way to spawn the ball
- resetBall();
- }
- else if (ballX > width){ //OPTION: KEY SCORES
- keyScore ++;
- lastPoint = -1; //value to decide which way to spawn the ball
- resetBall();
- }
- //stops game once scores reach 11.
- if (mouseScore == 11){
- endGame("Mouse");
- }
- if (keyScore == 11){
- endGame("Key");
- }
- }
- //~~~~Functions to Debut~~~~\\
- void keyPressed(){ //spawns new ball when space is pressed.
- if (keyCode == 32){
- resetBall();
- }
- }
- //~~~~Functions to Reset~~~~\\
- void resetBall(){
- //sets initial Ball position to the centre of canvas
- ballSpeed = MIN_BALL_SPEED; //resets the ball speed
- ballX = centreX; //dead centre
- ballY = centreY; //dead centre
- getAngle(); //calculates angle of new ball.
- checkAngles();
- }
- void resetPaddles(){
- //resets both paddles to exactly the centre
- rPaddleY = (height-PADDLE_HEIGHT)/2;
- lPaddleY = (height-PADDLE_HEIGHT)/2;
- lPaddleX = PADDLE_WIDTH;
- rPaddleX = width-(PADDLE_WIDTH*2);
- }
- void endGame(String winner){ //winner display
- gameOver = true; //changes boolean to stop game loop in draw()
- textSize(32);
- text(winner + " Wins!", (centreX-textWidth(winner)), centreY); //String
- }
- void reset(){
- println(keyCode);
- if (keyCode == 32 && gameOver){
- resetBall();
- keyScore = 0;
- mouseScore = 0;
- gameOver = false;
- }
- }
- void parameters(){
- //random colisions:
- randomCollisions = false;
- ballSpeedIncreases = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement