diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 0000000..5008ddf
Binary files /dev/null and b/.DS_Store differ
diff --git a/instructions.txt b/instructions.txt
new file mode 100644
index 0000000..5511620
--- /dev/null
+++ b/instructions.txt
@@ -0,0 +1 @@
+
Difficulty:
The Screen size increases with the difficulty that is chosen.
How To Play:
Player 1 uses W to move the paddle up,
S to move their paddle down, and A to grab the ball.
Player 2 uses the Up and Down arrows to move their paddle up and
down and the left arrow to grab the ball.
When ball is being held and the player releases it while moving their paddle up or down,
ball will be released with the same speed that the paddle is moving
Hit the space bar to move the ball initially.
Press P to pause the game.
Press M to return to the Main Menu.
You lose a life if you miss the ball.
When you lose a life, the total hits will be added to your opponents score.
The winner is the player with remaining lives.
Chaos Mode:
very small ball on large screen moving at faster than normal speed
Custom Mode:
Allows players to select both level and how many balls they would like
to be in play at one time at the level selected.
Information on Ball to Paddle Speed:
When ball is released, it has the same y-velocity as your paddle.
The ball increases in speed after each hit of paddle.
diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java
index 58b443d..30b0eb2 100644
--- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java
+++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java
@@ -123,7 +123,7 @@ public void holdBallToPaddle(KeyEvent evt, double distance)
setAttached(true);
setSpeedX(getXVelocity());
- if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)(distance+50) < (DifficultyLevel.getPaddleHeight()) ) ) {
+ if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)(distance+55) < (DifficultyLevel.getPaddleHeight()) ) ) {
stopBall();
paddle = false;
}
@@ -153,7 +153,7 @@ public void releaseBallFromPaddle(KeyEvent evt, double distance)
{
//attached = false;
setAttached(false);
- if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)(distance+50) < DifficultyLevel.getPaddleHeight() ) ) {
+ if(evt.getKeyCode() == KeyEvent.VK_A && ( (int)(distance+55) < DifficultyLevel.getPaddleHeight() ) ) {
setYVelocity(p1.getYVelocity());
if(getSpeedX() < 0)
setXVelocity(getSpeedX() * -1);
@@ -175,7 +175,7 @@ public void releaseBallFromPaddle(KeyEvent evt, double distance)
* @param distance ArrayList of distances from balls
*/
public void keyPressed(KeyEvent evt, ArrayList distance){
- if ( (evt.getKeyCode() == KeyEvent.VK_A) && ( distance.get(0) < DifficultyLevel.getPaddleHeight()) && (isAttached() == false)) {
+ if ( (evt.getKeyCode() == KeyEvent.VK_A) && ( (int)(distance.get(0)+55) < DifficultyLevel.getPaddleHeight()) && (isAttached() == false)) {
holdBallToPaddle(evt, distance.get(0));
}
if(paddle == false && isAttached() == true){
@@ -186,10 +186,12 @@ public void keyPressed(KeyEvent evt, ArrayList distance){
if( evt.getKeyCode() == KeyEvent.VK_S && distance.get(2) > 5){
setYVelocity(p1.getYVelocity());
}
- }
+ }else{
+ setYVelocity(0);
+ }
}
- if (evt.getKeyCode() == KeyEvent.VK_LEFT && distance.get(1)< DifficultyLevel.getPaddleHeight() && isAttached() == false) {
+ if (evt.getKeyCode() == KeyEvent.VK_LEFT && (int)(distance.get(1)+40)< DifficultyLevel.getPaddleHeight() && isAttached() == false) {
holdBallToPaddle(evt, distance.get(1));
}
if(paddle == true && isAttached() == true){
@@ -200,7 +202,9 @@ public void keyPressed(KeyEvent evt, ArrayList distance){
if( evt.getKeyCode() == KeyEvent.VK_DOWN && distance.get(3) > 5 ){
setYVelocity(p2.getYVelocity());
}
- }
+ }else{
+ setYVelocity(0);
+ }
}
}
diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java
index cf7f91c..4fdda1b 100644
--- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java
+++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java
@@ -138,6 +138,8 @@ public void draw2(Graphics g)
/** decrementBalls() decrements ballCount when a player loses a life
*/
public void decrementBalls(){ ballCount--; }
+
+ public void incrementBalls(){ ballCount++; }
/** incrementPoints adds the number of hits to the user's score
* @param numOfPoints number of points to add to score
diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Pong.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Pong.java
index 09269e5..7f7c724 100644
--- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Pong.java
+++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Pong.java
@@ -3,6 +3,7 @@
import edu.ucsb.cs56.projects.games.pong.sound.SoundEffect;
import edu.ucsb.cs56.projects.games.pong.menu.PlayTextComponent;
import edu.ucsb.cs56.projects.games.pong.menu.ColorPrompt;
+import java.awt.*;
/** edu.ucsb.cs56.projects.games.pong.gameplay.Pong is the class that will facilitate
* the game of Pong being run
@@ -34,6 +35,7 @@ public class Pong implements Runnable {
/**The balls*/
Ball b[];
+ static Rectangle star;
/**Number of balls*/
int ballNum;
@@ -58,6 +60,10 @@ public Pong() {
p2.setColor(ColorPrompt.getColorB());
b = new Ball[ballNum];
+ star = new Rectangle ((((Screen.w-DifficultyLevel.getWidth()) /2) + DifficultyLevel.getHeight()/4) ,
+ ((Screen.h-DifficultyLevel.getHeight()) /2 - (DifficultyLevel.getHeight()/2)),
+ DifficultyLevel.getHeight()/2,
+ DifficultyLevel.getHeight()/2);
for(int i = 0; i < ballNum; i++) {
boolean dir = false;
@@ -169,8 +175,9 @@ public void moveGame() { // every iterations of thread the ball calls this
}
// checks if it hit a paddle
- paddleCollision();
+ //paddleCollision();
wallCollision();
+ paddleCollision();
}
}
@@ -185,42 +192,63 @@ public void paddleCollision() { // speed starts out at 1
// and adds the increments the number of hits
for(int i = 0; i < ballNum; i++){
+ //check if touch star
+ if( star.intersects( (b[i].rectangle)) ){
+ if(b[i].getXVelocity() >0){
+ p1.incrementBalls();
+ b[i].setXVelocity( -1 * ( b[i].getXVelocity() + moreSpeed ) );
+ }
+ else if(b[i].getXVelocity() < 0){
+ p2.incrementBalls();
+ b[i].setXVelocity( -1 * ( b[i].getXVelocity() - moreSpeed ) );
+ }
+ star = new Rectangle(0,0,1,1);
+ }
// checks if it hits p1
- if( ( b[i].rectangle ).intersects( p1.rectangle ) ){
- playPaddleCollisionAudio();
- b[i].setXVelocity( -1 * ( b[i].getXVelocity() - moreSpeed ) );
+ if(( ( b[i].rectangle ).intersects( p1.rectangle ))&&((b[i].getXCoordinate()+b[i].getWidth()/2)<=p2.getXCoordinate()) )
+ {
+ playPaddleCollisionAudio();
+ b[i].setXVelocity( -1 * ( b[i].getXVelocity() - moreSpeed ) );
+
if((b[i].getYVelocity() >0) && p1.getYVelocity() >0){
b[i].setYVelocity(b[i].getYVelocity()+2);
}
if((b[i].getYVelocity() <0) && p1.getYVelocity() <0){
- b[i].setYVelocity(b[i].getYVelocity()-2);
+ b[i].setYVelocity(b[i].getYVelocity()-1);
}
if((b[i].getYVelocity() >0) && p1.getYVelocity() <0){
- b[i].setYVelocity(b[i].getYVelocity()-2);
+ b[i].setYVelocity(b[i].getYVelocity()-1);
}
if((b[i].getYVelocity() <0) && p1.getYVelocity() >0){
b[i].setYVelocity(b[i].getYVelocity()+2);
}
+ if(b[i].getYVelocity() ==0){
+ b[i].setYVelocity(p1.getYVelocity());
+ }
incrementHits();
}
// checks if it hits p2
- else if( ( b[i].rectangle ).intersects( p2.rectangle ) ){
+ else if( ( b[i].rectangle ).intersects( p2.rectangle ) )
+ {
playPaddleCollisionAudio();
b[i].setXVelocity( -1 * ( b[i].getXVelocity() + moreSpeed ) );
+
if((b[i].getYVelocity() >0) && p1.getYVelocity() >0){
b[i].setYVelocity(b[i].getYVelocity()+2);
}
if((b[i].getYVelocity() <0) && p1.getYVelocity() <0){
- b[i].setYVelocity(b[i].getYVelocity()-2);
+ b[i].setYVelocity(b[i].getYVelocity()-1);
}
if((b[i].getYVelocity() >0) && p1.getYVelocity() <0){
- b[i].setYVelocity(b[i].getYVelocity()-2);
+ b[i].setYVelocity(b[i].getYVelocity()-1);
}
if((b[i].getYVelocity() <0) && p1.getYVelocity() >0){
b[i].setYVelocity(b[i].getYVelocity()+2);
}
-
+ if(b[i].getYVelocity()==0){
+ b[i].setYVelocity(p2.getYVelocity());
+ }
incrementHits();
}
}
@@ -238,26 +266,57 @@ protected void playPaddleCollisionAudio() {
/** wallCollision() detects whether the ball hits a wall*/
public void wallCollision() {
+ if(DifficultyLevel.getDifficulty()==170){
+ for(int i = 0; i < ballNum; i++){
+ // check if p1 misses
+ if( b[i].getXCoordinate() <= ( 0 ) ) {
+ p1.playerMissed( b[i], getHits(), p2 );
+ b[i].isGoingRight = true;
+ hitsReset();
+ b[i].resetBall(i);
+ }
+ // check if p2 misses
+ else if( b[i].getXCoordinate() >= ( Screen.w - 20 ) ) {
+ p2.playerMissed( b[i], getHits(), p1 );
+ b[i].isGoingRight = false;
+ hitsReset();
+ b[i].resetBall(i);
+ }
+ }
+ }else{
+ double coef=0;
+ if(DifficultyLevel.getDifficulty()==80){
+ coef=0.45;
+ }
+ if(DifficultyLevel.getDifficulty()==100){
+ coef=0.37;
+ }
+ if(DifficultyLevel.getDifficulty()==120){
+ coef=0.31;
+ }
// if p1 misses / hits the wall behind it
// then increment balls lost, sets the ball
// to the middle and gives points to other player
for(int i = 0; i < ballNum; i++){
// check if p1 misses
- if( b[i].getXCoordinate() <= ( 0 ) ) {
+ if( b[i].getXCoordinate() <= ( b[i].getWidth()/2-(DifficultyLevel.getOrigballsize()*DifficultyLevel.getScreenFactor()*coef) ) )
+ {
p1.playerMissed( b[i], getHits(), p2 );
b[i].isGoingRight = true;
hitsReset();
b[i].resetBall(i);
}
// check if p2 misses
- else if( b[i].getXCoordinate() >= ( Screen.w - 20 ) ) {
+
+ else if( b[i].getXCoordinate() >= ( p2.getXCoordinate()-b[i].getWidth()/2-(DifficultyLevel.getOrigballsize()*DifficultyLevel.getScreenFactor()*coef) ) )
+ {
p2.playerMissed( b[i], getHits(), p1 );
b[i].isGoingRight = false;
hitsReset();
b[i].resetBall(i);
}
}
-
+ }
// If the ball hits the top or bottom of the screen,
// then the Y velocity is reversed to stay on screen
for(int i = 0; i < ballNum; i++){
@@ -282,7 +341,7 @@ public void run(){
try{
while( gameIsGoing ){
moveGame();
- Thread.sleep( 15 );
+ Thread.sleep(15 );
}
}catch(Exception e){}
diff --git a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java
index e863114..33a4fb9 100644
--- a/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java
+++ b/src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java
@@ -120,12 +120,21 @@ public void draw(Graphics g){
}
//If all the balls have stopped then pause the game
- if(Pong.isPaused) {
+ if(game.checkBallStopped()) {
g.drawString( "Game Paused", Screen.w/2 - 100, Screen.h/2 - 100 );
g.drawString( "Press M to return to Main Menu", Screen.w/2 - 220, Screen.h/2 + 100 );
}
game.getPlayer1().draw(g);
game.getPlayer2().draw(g);
+
+ //draw star
+ g.setColor( Color.RED );
+ int star_x = (int) game.star.getX();
+ int star_y = (int) game.star.getY();
+ int star_width = (int) game.star.getWidth();
+ int star_height = (int) game.star.getHeight();
+ g.fillRect(star_x,star_y,star_width,star_height);
+
jf.repaint();
}
diff --git a/src/edu/ucsb/cs56/projects/games/pong/menu/DifficultyLevelPrompt.java b/src/edu/ucsb/cs56/projects/games/pong/menu/DifficultyLevelPrompt.java
index 941b4f3..921e17d 100644
--- a/src/edu/ucsb/cs56/projects/games/pong/menu/DifficultyLevelPrompt.java
+++ b/src/edu/ucsb/cs56/projects/games/pong/menu/DifficultyLevelPrompt.java
@@ -27,7 +27,8 @@ public class DifficultyLevelPrompt extends JPanel {
put("Hard",130);
put("Extreme",140);
put("Chaos",170);
- put("Custom Mode",90);
+ put("Custom Mode",90);
+ put("AI Mode",120);
}};
/**Map that returns difficulties
diff --git a/src/edu/ucsb/cs56/projects/games/pong/menu/instructions/Instructions.java b/src/edu/ucsb/cs56/projects/games/pong/menu/instructions/Instructions.java
index 20a1167..a5f36c3 100644
--- a/src/edu/ucsb/cs56/projects/games/pong/menu/instructions/Instructions.java
+++ b/src/edu/ucsb/cs56/projects/games/pong/menu/instructions/Instructions.java
@@ -1,5 +1,6 @@
package edu.ucsb.cs56.projects.games.pong.menu.instructions;
+import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@@ -59,7 +60,39 @@ public void setToVisible()
*/
public String displayInstructions()
{
- return ("
Difficulty:
The Screen size increases with the difficulty that is chosen.
How To Play:
Player 1 uses W to move the paddle up,
S to move their paddle down, and A to grab the ball.
Player 2 uses the Up and Down arrows to move their paddle up and
down and the left arrow to grab the ball.
When ball is being held and the player releases it while moving their paddle up or down,
ball will be released with the same speed that the paddle is moving
Hit the space bar to move the ball initially.
Press P to pause the game.
Press M to return to the Main Menu.
You lose a life if you miss the ball.
When you lose a life, the total hits will be added to your opponents score.
The winner is the player with remaining lives.
Chaos Mode:
very small ball on large screen moving at faster than normal speed
Custom Mode:
Allows players to select both level and how many balls they would like
to be in play at one time at the level selected.
Information on Ball to Paddle Speed:
When ball is released, it has the same y-velocity as your paddle.
The ball increases in speed after each hit of paddle.
");
+ String fileName = "instructions.txt";
+
+ // This will reference one line at a time
+ String line = null;
+
+ try {
+ // FileReader reads text files in the default encoding.
+ FileReader fileReader =
+ new FileReader(fileName);
+
+ // Always wrap FileReader in BufferedReader.
+ BufferedReader bufferedReader =
+ new BufferedReader(fileReader);
+
+ line = bufferedReader.readLine();
+
+ // Always close files.
+ bufferedReader.close();
+ }
+ catch(FileNotFoundException ex) {
+ System.out.println(
+ "Unable to open file '" +
+ fileName + "'");
+ }
+ catch(IOException ex) {
+ System.out.println(
+ "Error reading file '"
+ + fileName + "'");
+ // Or we could just do this:
+ // ex.printStackTrace();
+ }
+ return line;
+
}
/** The action when the button "Back To Main Menu" is pressed. It just makes the frame not visible, so it appears to be closed and does not need to create a new instance of Instructions for every time the user clicks how to play */
diff --git a/src/edu/ucsb/cs56/projects/games/pong/menu/instructions/instructions.txt b/src/edu/ucsb/cs56/projects/games/pong/menu/instructions/instructions.txt
new file mode 100644
index 0000000..5511620
--- /dev/null
+++ b/src/edu/ucsb/cs56/projects/games/pong/menu/instructions/instructions.txt
@@ -0,0 +1 @@
+
Difficulty:
The Screen size increases with the difficulty that is chosen.
How To Play:
Player 1 uses W to move the paddle up,
S to move their paddle down, and A to grab the ball.
Player 2 uses the Up and Down arrows to move their paddle up and
down and the left arrow to grab the ball.
When ball is being held and the player releases it while moving their paddle up or down,
ball will be released with the same speed that the paddle is moving
Hit the space bar to move the ball initially.
Press P to pause the game.
Press M to return to the Main Menu.
You lose a life if you miss the ball.
When you lose a life, the total hits will be added to your opponents score.
The winner is the player with remaining lives.
Chaos Mode:
very small ball on large screen moving at faster than normal speed
Custom Mode:
Allows players to select both level and how many balls they would like
to be in play at one time at the level selected.
Information on Ball to Paddle Speed:
When ball is released, it has the same y-velocity as your paddle.
The ball increases in speed after each hit of paddle.