Commit 477c3079 authored by Noman Rafique's avatar Noman Rafique

first and final commit of pong.

parent 05e018e4
import processing.core.PApplet;
import java.util.Random;
public class Pong extends PApplet
{
// x position of the ball
private int ballX;
// y position of the ball
private int ballY;
// Ball speed in the x direction
private int ballSpeedX;
// Ball speed in the y direction
private int ballSpeedY;
// Width of our window
final private int width = 640;
// height of our window
final private int height = 480;
public static void main(String[] args) {
// Set up the processing library
PApplet.main("Pong");
}
public void settings() {
// Set our window size
size(width, height);
}
public void setup() {
// Create a random initial position and speed for the ball
Random r = new Random();
ballX = r.nextInt(width);
ballY = r.nextInt(height);
ballSpeedX = r.nextInt(width / 100) + 1;
ballSpeedY = r.nextInt(height / 100) + 1;
}
public void draw() {
// Clear the background of the window
background(255, 255, 255);
// Draw the ball
ellipse(ballX, ballY, 32, 32);
// Move the ball
ballX += ballSpeedX;
ballY += ballSpeedY;
}
}
#!/bin/bash
#PROCESSING=~/Downloads/processing-3.3.5/core/library/core.jar
PROCESSING=/opt/processing/core/library/core.jar
javac -cp $PROCESSING Pong.java
java -cp $PROCESSING:. Pong
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="core.jar"/>
<classpathentry exported="true" kind="lib" path="jsyn-20171016.jar"/>
<classpathentry exported="true" kind="lib" path="sound.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>pong</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
/Pong$Ball.class
/Pong$Computer.class
/Pong$Paddle.class
/Pong$Player.class
/Pong.class
/evafangoria.txt
File added
File added
import java.util.Random;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import processing.sound.*;
public class Pong extends PApplet {
// Constants
private final static int WIDTH = 900; // Width
private final static int HEIGHT = 600; // Height
private final static int DISTANCE = 20; // Distance between paddle and walls
private final static int S_DISTANCE = 10; // Small distance between text and walls
private final static int L_DISTANCE = 40; // Large distance between text and walls
// PFonts for adding new fonts
PFont title;
PFont guidance;
PFont scream;
// PImage for adding Images
PImage gameover;
PImage mainmenu;
PImage background;
PImage img;
PImage circle;
// SoundFile for adding different sound effects
SoundFile gameoversound;
SoundFile startingmusic;
SoundFile paddlehit;
SoundFile missorpoint;
SoundFile wallhit;
// Creating objects
Ball ball;
Player player1;
Player computer;
Player player2;
// Adding modes for the displaying of different screens and different points
int mode = 0; // Mode for initializing the other modes
int mainScreen = 0; // As mainScreen = 0 , same as the mode, so it's our first screen.
int singlePlayerMode = 1; // Mode for Single Player game
int doublePlayerMode = 2; // Mode for Double Player game
int gameoverscreendouble = 4; // Mode for Double Player game over screen
int gameoverscreensingle = 5; // Mode for single Player game over screen
public static void main(String args[]) {
// setup the processing library
PApplet.main("Pong");
}
// this runs once when the program starts to initialize the board
public void settings() {
size(WIDTH, HEIGHT); // sets our windows size
startingmusic = new SoundFile(this, "start.wav"); // starting music as the game runs
startingmusic.play(); // playing the above file
mainmenu = loadImage("mainmenu.jpg"); // loading main menu image
mainmenu.resize(900, 600); // resizing the image so it's correct for the grid size
background = loadImage("spacebackground.jpg"); // Loading background image
background.resize(900, 600); //
gameover = loadImage("game-over.jpg"); // Game over screen image
gameover.resize(900, 600); // resizing the image so it's correct for the grid size
player1 = new Player("Player1", DISTANCE, HEIGHT / 2);
computer = new Player("Computer", WIDTH - DISTANCE, HEIGHT / 2);
player2 = new Player("Player2", WIDTH - DISTANCE, HEIGHT / 2);
ball = new Ball(); // Adding ball object to Ball
img = loadImage("paddles.png"); // loading image for paddle
circle = loadImage("soccer.png"); // loading image for ball
}
public void draw() { // Main draw method where all of the modes and there methods will be used using different if statements
if (mode == mainScreen) { // Our first mode main menu and its methods
mainmenu();
}
if (mode == singlePlayerMode) { // If mode is Single Player mode then this will initialize with it's methods
background(background);
screendrawTextSingle();
ball.draw();
collisionsCheckingpongsingle();
player1.draw();
computer.draw();
keyboardassigningValues();
gameoversingle();
}
if (mode == doublePlayerMode) { // If mode is Double Player mode then this will initialize with it's methods
keyboardassigningValues();
background(background);
collisionsCheckingpongdouble();
screendrawTextDouble();
player1.draw();
player2.draw();
ball.draw();
}
if (mode == gameoverscreensingle) { // If mode is Single Player game over mode then this will initialize with it's methods
gameoversingle();
}
if (mode == gameoverscreendouble) { // If mode is Double Player game over mode then this will initialize with it's methods
gameoverdouble();
}
}
private void mainmenu() { // Draws the main menu screen when the game starts
background(mainmenu); // Adds the background image that we've loaded in the settings
PFont mainmenuFont; // Adding PFonts
mainmenuFont = createFont("evafangoria.ttf", 70); // Using our own fonts
textFont(mainmenuFont);
textAlign(CENTER);
this.fill(255); // Adding white color to fonts
text("Welcome to Pong game", 450, 200); // Displaying text with custom fonts and location
PFont guidance; // Adding PFonts
guidance = createFont("FREEZER.ttf", 50); // Using our own fonts
textFont(guidance);
textAlign(CENTER);
text("Press 1 to start single Player mode", 450, 290); // Displaying text with custom fonts and location
text("Press 2 to start two player mode", 450, 360); // Displaying text with custom fonts and location
textSize(15);
textAlign(CENTER);
text("Controls for two player mode :- Player 1- for up key = 'w', down key = 's', Player 2- for up key = 'UP', Player2 for down key = 'DOWN.'", 450,500);
text("Controls for single player mode :- for up key = 'w' , for down key = 's'", 450,520); // Text guidance on main menu about controls
text("Note :- In both modes ball gets faster as it hits paddles(resetspeed = score++). The player who will score 10 first, will be the winner.", 450,570);
text("ENJOY.", 450,590);
if (keyCode == '1') { // On the main menu if player presses '1' key it will start single player game
mode = singlePlayerMode;
}
if (keyCode == '2') { // On the main menu if player presses '2' key it will start two player game
mode = doublePlayerMode;
}
}
private void screendrawTextSingle() { // drawing text on the screen for single player mode
PFont scream;
scream = createFont("Scream.ttf",24);
textFont(scream);
this.textSize(30);
this.textAlign(CENTER);
this.fill(0, 102, 153, 204);
text("PONG GAME", WIDTH / 2, L_DISTANCE);
// To display player1 info and score on main screen
this.textSize(24);
this.textAlign(LEFT);
this.fill(255, 204, 0);
text("Your Score:- " + player1.getScore(), S_DISTANCE, L_DISTANCE); // setups the text location
// To display player2 info and score on main screen
this.textAlign(RIGHT);
text("Computer Score:- " + player2.getScore(), WIDTH - S_DISTANCE, L_DISTANCE); // setups the text location
// Computer automation
if (ball.ballY < 175) {
computer.moveUp();
}
if (ball.ballY > 175) {
computer.moveDown();
}
// If player 1 score or computer score reaches 10 then this will show the game over screen and will play the game over sound
if (player1.score == 10 || player2.score == 10) {
gameoversound = new SoundFile(this, "gameover.wav");
gameoversound.play();
mode = gameoverscreensingle;
}
}
void screendrawTextDouble() { // Drawing text on the screen for double player mode
//To display "pong game" on the top of the screen when game starts
this.textSize(26);
this.textAlign(CENTER);
this.fill(0, 102, 153, 204);
text("PONG GAME", WIDTH / 2, L_DISTANCE); // Adding text and setting up it's position on the screen
// To display player1 name and score
this.textSize(24);
this.textAlign(LEFT);
this.fill(255, 204, 0);
text(player1.getName() + " Score:- " + player1.getScore(), S_DISTANCE, L_DISTANCE); // Adding text and setting up it's position on the screen
// To display player2 info and score
this.textAlign(RIGHT);
text(player2.getName() + " Score:- " + player2.getScore(), WIDTH - S_DISTANCE, L_DISTANCE); // Adding text and setting up it's position on the screen
// If player 1 score or player 2 score reaches 10 then this will show the game over screen and will play the game over sound
if (player1.score == 10 || player2.score == 10) {
gameoversound = new SoundFile(this, "gameover.wav");
gameoversound.play(); // This will play game over sound, at the end of game
mode = gameoverscreendouble; // Mode will be passed to game over screen double player mode
}
}
// Game over screen for single player mode
private void gameoversingle() {
if (player1.getScore() == 10) { // If player 1 reaches 10 score first then :-
//Stops the ball
ball.xSpeed = 0;
ball.ySpeed = 0;
// Adding background for the game over screen
background(gameover);
// Adding text to the screen
this.textAlign(CENTER);
textSize(40);
text(" Congrats You Won! ", WIDTH / 2, L_DISTANCE); // Setting text location on the screen
text("Result: -", 150, 110);
text(" Your score : " + player1.getScore(), 450, 210); // displays player 1 score
text(computer.getName() + " score : " + player2.getScore(), 450, 150); // displays computer score
text("Press '9' to exit", 450,400);
if (keyCode == '9') { // If user presses '9' key game exits
System.exit(0);
}
}
if (player2.getScore() == 10) { // If computer reaches 10 score first then :-
//Stops the ball
ball.xSpeed = 0;
ball.ySpeed = 0;
// Adding background for the game over screen
background(gameover);
// Adding text to the screen
this.textAlign(CENTER);
text(" Computer Won! ", WIDTH / 2, L_DISTANCE); // Setting text location on the screen
textAlign(CENTER);
textSize(40);
text("Result: -", 150, 110);
text(computer.getName() + "score : " + player2.getScore(), 450, 210); // displays Computer score
text(" Your score : " + player1.getScore(), 450, 150); // displays player 1 score
text("Press '9' to exit", 450,400);
if (keyCode == '9') { // If user presses '9' key game exits
System.exit(0);
}
}
}
// Game over screen for two players mode
private void gameoverdouble() {
if (player1.getScore() == 10) { // If player 1 reaches 10 score first then :-
//Stops the ball
ball.xSpeed = 0;
ball.ySpeed = 0;
// Adding background for the game over screen
background(gameover);
// Adding text to the screen
this.textAlign(CENTER);
textSize(40);
text(player1.getName() + " Won ", WIDTH / 2, L_DISTANCE); // Setting text location on the screen
text("Result: ", 150, 110);
text(player1.getName() + " score is :-" + player1.getScore(), 450, 210); // displays player 1 score
text(player2.getName() + " score is :-" + player2.getScore(), 450, 150); // displays player 2 score
text("Press '9' to exit", 450,400);
if (keyCode == '9') { // If user presses '9' key game exits
System.exit(0);
}
}
if (player2.getScore() == 10) { // If player 1 reaches 10 score first then :-
//Stops the ball
ball.xSpeed = 0;
ball.ySpeed = 0;
// Adding background for the game over screen
background(gameover);
// Adding text to the screen
this.textAlign(CENTER);
text(player2.getName() + " Won ", WIDTH / 2, L_DISTANCE); // Setting text location on the screen
textAlign(CENTER);
textSize(40);
text("Result: -", 150, 110); // Displays text
text(player1.getName() + "score is :-" + player1.getScore(), 450, 210); // displays player 1 score
text(player2.getName() + "score is :-" + player2.getScore(), 450, 150); // displays player 2 score
text("Press '9' to exit", 450,400);
if (keyCode == '9') { // If user presses '9' key game exits
System.exit(0);
}
}
}
// To give keyboard controls to players
void keyboardassigningValues() {
if (keyPressed) {
// Player 2 controls
if (key == CODED) {
if (keyCode == UP) { // If 'UP' key is pressed player2 paddle will moveUp
player2.moveUp(); // Taking moveUp value from the method
}
if (keyCode == DOWN) { // If 'DOWN' key is pressed player2 paddle will moveDown
player2.moveDown(); // Taking moveDown value from the method
}
// Player 1 controls
} else if (key == 'w' || key == 'W') { // If 'w' key is pressed player1 paddle will moveUp
player1.moveUp(); // Taking moveUp value from the method
} else if (key == 's' || key == 'S') { // If 's' key is pressed player2 paddle will moveDown
player1.moveDown(); // Taking moveDown value from the method
}
}
}
// for checking if ball is colliding with the paddle and if it do so then what should be done
void collisionsCheckingpongsingle() {
// To check if ball collided with player1's paddle or player2's paddle
Paddle paddle1 = player1.paddle;
Paddle paddle2 = computer.paddle;
// Checking if ball collided with paddle1
if ((ball.ballX <= paddle1.paddleX + paddle1.width)) {
// To check if ball's Y position is within the paddle range
if ((ball.ballY >= paddle1.paddleY) && (ball.ballY <= paddle1.paddleY + paddle1.length)) {
paddlehit = new SoundFile(this, "ping_pong.wav"); //To play the sound when ball hits the paddle
paddlehit.play(); // Plays sound when ball hits the paddle
// end of paddle position
ball.reverseX(); // Whenever the ball hits the paddle, ball bounces back
}
}
// Checking if ball collided with paddle2 or computer's paddle
if ((ball.ballX >= paddle2.paddleX)) {
// ball collided with the paddle or not
if ((ball.ballY >= paddle2.paddleY) && (ball.ballY <= paddle2.paddleY + paddle2.length)) {
ball.xSpeed++; // Every time ball hits the computer paddle it will increase the speed of the ball
ball.ySpeed = 4;
ball.reverseX(); // If ball collides with the paddle it bounces back
paddlehit = new SoundFile(this, "ping_pong.wav"); // loading the file
paddlehit.play(); // to play sound when the ball hits the paddle
}
}
}
void collisionsCheckingpongdouble() {
// check if ball collided with player1's paddle or player2's paddle
Paddle paddle1 = player1.paddle;
Paddle paddle2 = player2.paddle;
// Checking if ball collided with paddle1
if ((ball.ballX <= paddle1.paddleX + paddle1.width)) {
// To check if ball's Y position is within the paddle range
if ((ball.ballY >= paddle1.paddleY) && (ball.ballY <= paddle1.paddleY + paddle1.length)) {
paddlehit = new SoundFile(this, "ping_pong.wav"); //load the ping_pong.wav sound file
paddlehit.play();
// end of paddle position
ball.reverseX(); // ball bounces back
}
}
// Checking if ball collided with paddle2
if ((ball.ballX >= paddle2.paddleX)) {
// ball collided with the paddle or not
if ((ball.ballY >= paddle2.paddleY) && (ball.ballY <= paddle2.paddleY + paddle2.length)) {
ball.xSpeed++; // ball Speed increases every time it hits the paddle
ball.ySpeed = 4; // ball ySpeed
ball.reverseX(); // ball bounces back
paddlehit = new SoundFile(this, "ping_pong.wav"); // loading the file
paddlehit.play(); // to play sound when the ball hits the paddle
}
}
}
// Ball class
public class Ball {
int ballX; // ball X position
int ballY; // ball Y position
int xSpeed; // ball X Speed
int ySpeed; // ball Y Speed
int size; // ball size
Ball() { // Giving values
ballX = WIDTH / 2;
ballY = HEIGHT / 2;
xSpeed = 4;
ySpeed = 4;
size = 30;
}
void draw() {
ballX += xSpeed; // To move the ball
ballY += ySpeed;
// If the ball went off the bottom of the screen change the directions
if (ballY >= HEIGHT) {
reverseY();
}
// If ball hits the wall play the sound
if (ballY == HEIGHT || ballY == 0) {
wallhit = new SoundFile(Pong.this, "wall_hit.wav");
wallhit.play();
}
// If the ball went off the top of the screen change the directions
if (ballY <= 0) {
reverseY();
}
if (ballX > 900) {
player1.score++; // if player 2 misses the ball it adds score to player 1
missorpoint = new SoundFile(Pong.this, "missorpoint.wav");
missorpoint.play();
Random r = new Random(); // It generates random position for the ball when player misses the ball
ball.ballX = r.nextInt(height);
ball.xSpeed = 4; // Resets the ball Speed to 4
}
if (ballX < 0) {
player2.score++; // if player 1 misses the ball it adds score to player 2
missorpoint = new SoundFile(Pong.this, "missorpoint.wav"); // loading sound
missorpoint.play(); // plays sound whenever player gets a point
Random r = new Random(); // It generates random position for the ball when player misses the ball
ball.ballX = r.nextInt(width);
ball.ySpeed = 4; // Resets the ball Speed to 4
}
image(circle, ballX, ballY, 40, 40); // creates the ball and we added circle which is image of a football which we are using instead of normal ball
}
void reverseY() { // bounces back the ball
ySpeed = -ySpeed;
}
void reverseX() { // bounces back the ball
xSpeed = -xSpeed;
}
int getX() { // To return ballX
return ballX;
}
int getY() { // To return ballY
return ballY;
}
}
public class Computer { // Making class for the single player mode for computer
String name; // Adding name
int score; // Adding Computer score
Paddle pcpaddle; // Adding paddle for the pc
// Default Constructor
Computer() { // Giving values
name = "computer";
score = 0;
pcpaddle = new Paddle();
}
String getName() { // To return Computer name
return name;
}
int getScore() { // To return Computer score
return score;
}
public void draw() {
computer.draw(); // Draws paddle
}
Computer(String name, int xPos, int yPos) { // Passing values
this.name = name;
pcpaddle = new Paddle(xPos, yPos);
}
void moveUp() { // For the moving up of computer paddle
pcpaddle.moveUp(); // Getting the value from moveUp method
}
void moveDown() { // For the moving down of computer paddle
pcpaddle.moveDown(); // Getting the value from moveDown method
}
}
public class Player { // Class for players
String name;
int score;
Paddle paddle;
Player() {
name = "Player";
score = 0;
paddle = new Paddle(); // Generating a new paddle
}
String getName() { // To return Player name
return name;
}
int getScore() { // To return Player score
return score;
}
public void draw() {
// Draws paddle
paddle.draw();
}
Player(String name, int xPos, int yPos) { // Passing values
this.name = name;
paddle = new Paddle(xPos, yPos);
}
void moveUp() { // For the moving up of Player paddles
paddle.moveUp(); // Getting the value from moveUp method
}
void moveDown() { // For the moving down of Player paddles
paddle.moveDown(); // Getting the value from moveDown method
}
}
public class Paddle { // Paddle Class
int paddleX; // Paddle X position
int paddleY; // Paddle Y position
int length; // Paddle length
int width; // paddle width
// Default constructor
Paddle() { // Adding Values
paddleX = 0;
paddleY = 0;
length = 100;
width = 10;
}
Paddle(int paddleX, int paddleY) { // Passing values
this.paddleX = paddleX;
this.paddleY = paddleY;
length = 100;
width = 10;
}
// draws Paddle on the screen
public void draw() {
image(img, paddleX, paddleY, width, length); // we are using our own chosen image with name "img" that we have already loaded in the main class
}
void moveUp() { // Defining moveUp to be used for all paddles
if (paddleY > 0) {
paddleY -= 5;
}
}
// This will not let the paddle go out of the screen
void moveDown() { // Defining moveUp to be used for all paddles
if ((paddleY + length) < HEIGHT) {
paddleY += 5;
}
}
}
}
Eva Fangoria Truetype Font for Windows Version 1.0
2018 Iconian Fonts - Daniel Zadorozny
http://www.iconian.com/
This font comes with the following 22 versions: Regular, Italic, Condensed, Condensed Italic, Expanded, Expanded Italic, 3D, 3D Italic, Outline, Outline Italic, Rotated, Rotated 2, Rotalic, Staggered, Staggered Italic, Staggered Rotalic, Warped, Warped Italic, Warped Rotalic, Semi-Italic, Super-Italic and Leftalic.
This font may be freely distributed and is free for all non-commercial uses. Use of the fonts are at your own risk.
For commercial use of the font please visit http://iconian.com/commercial.html for additional information.
This font is e-mailware; that is, if you like it, please e-mail the author at:
iconian@aol.com
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment