Commit 4e3cc1f5 authored by james.green2's avatar james.green2

final push

parents
import processing.core.PApplet;
import java.util.Random;
PFont f;
//Declaring the variables for the first players paddle
float Player1xpos = 320;
float Player1ypos = 45;
float Player1xspeed = 1*3;
int Player1Points = 0;
//Declaring the variables for the second players paddle
float Player2xpos = 320;
float Player2ypos = 440;
float Player2xspeed = 1*3;
int Player2Points = 0;
//Display method draws the paddle in each new position depending
// on whether the paddle has moved left or right in previous methods
void display(float xpos, float ypos, float xpos2, float ypos2){
rectMode(CENTER);
//fill(255);
rect(xpos,ypos,95,5);
rect(xpos2,ypos2,95,5);
}
//Changes the xposition of the paddle to make it move to the right
//first a check is carried out on the yposition to work
//out which paddle needs to be moved
void driveR(float xpos, float xspeed, float ypos){
if(ypos == 45){
Player1xpos = xpos + xspeed;
if (xpos > width){
Player1xpos = 0;
}
}
if(ypos == 440){
Player2xpos = xpos + xspeed;
if (xpos < 0){
Player2xpos = 200;
}
}
}
void driveL(float xpos, float xspeed, float ypos){
if (ypos == 45){
Player1xpos = xpos - xspeed;
if (xpos < 0){
Player1xpos = 200;
}
}
if(ypos == 440){
Player2xpos = xpos - xspeed;
if (xpos < 0){
Player2xpos = 200;
}
}
}
// 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 void settings() {
// Set our window size
size(width, height);
}
public void setup() {
// Create a random initial position and speed for the ball
f = createFont("Arial",16,true); // STEP 2 Create Font
Random r = new Random();
ballX = r.nextInt(width);
ballY = r.nextInt(height);
ballSpeedX = (r.nextInt(width / 150) + 1);
ballSpeedY = (r.nextInt(height / 150) + 1);
ellipseMode(CENTER);
}
public void keyPressed(){
//listener if statement that runs every time a key is pressed
// the various key's are how the player controls the paddles
if (keyPressed){
if (key == '0') {
//Move the paddle right
driveR(Player1xpos, Player1xspeed, Player1ypos);
}
if (key == '9'){
//Move the paddle left
driveL(Player1xpos, Player1xspeed, Player1ypos);
}
if (keyPressed){
if (key == '1') {
//Move the paddle Left
driveL(Player2xpos, Player2xspeed, Player2ypos);
}
if (key == '2'){
//Move the paddle right
driveR(Player2xpos, Player2xspeed, Player2ypos);
}
}
}
}
public void draw() {
// Clear the background of the window
background(255, 255, 255);
textFont(f,16); // STEP 3 Specify font to be used
fill(0); // STEP 4 Specify font color
text("Player1: "+Player1Points,10,100);
text("Player2: "+Player2Points,10,140);
// Draw the ball
ellipse(ballX, ballY, 32, 32);
// Move the ball
ballX += ballSpeedX;
ballY += ballSpeedY;
//Checks the ball is still within the screen
//If not it reverses the balls direction and
//'bounces' the ball off the screen edge
if (ballX > (width-16) || ballX < 0){
ballSpeedX = ballSpeedX - (ballSpeedX * 2);
}
if (ballY > (height-16) || ballY < 0){
ballSpeedY = ballSpeedY - (ballSpeedY * 2);
}
//If the ball is is in the same x and y position as a players
//paddle then it bounces off the paddle
if (ballY <= Player1ypos + 16){
if((ballX > Player1xpos - 58.5 && ballX < Player1xpos +58.5)){
ballSpeedY = ballSpeedY - (ballSpeedY * 2);
}
}
if (ballY >= Player2ypos - 16){
if((ballX > Player2xpos - 58.5 && ballX < Player2xpos +58.5)){
ballSpeedY = ballSpeedY - (ballSpeedY * 2);
}
}
if (ballY < 16){
if((ballX > 0 && ballX < 600)){
setup();
ellipse(ballX, ballY, 32, 32);
Player2Points = Player2Points + 1;
}
}
if (ballY > 464){
if((ballX > 0 && ballX < 640)){
setup();
ellipse(ballX, ballY, 32, 32);
Player1Points = Player1Points + 1;
}
}
//Move the paddles
keyPressed();
//Refresh the board
display(Player1xpos, Player1ypos, Player2xpos, Player2ypos);
}
\ No newline at end of file
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