Skip to content

Commit

Permalink
Merge pull request #104 from iamSamuelFu/proj02
Browse files Browse the repository at this point in the history
Proj02
  • Loading branch information
brianslee authored Mar 19, 2018
2 parents 915389e + 75972df commit 09f2b91
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 23 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions instructions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><p><br>Difficulty:<br>The Screen size increases with the difficulty that is chosen.<br><br>How To Play:<br><br>Player 1 uses W to move the paddle up,<br> S to move their paddle down, and A to grab the ball.<br>Player 2 uses the Up and Down arrows to move their paddle up and<br> down and the left arrow to grab the ball.<br><br>When ball is being held and the player releases it while moving their paddle up or down,<br>ball will be released with the same speed that the paddle is moving<br><br>Hit the space bar to move the ball initially.<br><br>Press P to pause the game.<br>Press M to return to the Main Menu.<br>You lose a life if you miss the ball.<br>When you lose a life, the total hits will be added to your opponents score.<br>The winner is the player with remaining lives.<br><br>Chaos Mode:<br> very small ball on large screen moving at faster than normal speed<br><br>Custom Mode:<br>Allows players to select both level and how many balls they would like<br>to be in play at one time at the level selected.<br><br>Information on Ball to Paddle Speed:<br>When ball is released, it has the same y-velocity as your paddle.<br>The ball increases in speed after each hit of paddle.</p></html>
16 changes: 10 additions & 6 deletions src/edu/ucsb/cs56/projects/games/pong/gameplay/Ball.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -175,7 +175,7 @@ public void releaseBallFromPaddle(KeyEvent evt, double distance)
* @param distance ArrayList of distances from balls
*/
public void keyPressed(KeyEvent evt, ArrayList<Double> 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){
Expand All @@ -186,10 +186,12 @@ public void keyPressed(KeyEvent evt, ArrayList<Double> 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){
Expand All @@ -200,7 +202,9 @@ public void keyPressed(KeyEvent evt, ArrayList<Double> distance){
if( evt.getKeyCode() == KeyEvent.VK_DOWN && distance.get(3) > 5 ){
setYVelocity(p2.getYVelocity());
}
}
}else{
setYVelocity(0);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/edu/ucsb/cs56/projects/games/pong/gameplay/Paddle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 73 additions & 14 deletions src/edu/ucsb/cs56/projects/games/pong/gameplay/Pong.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,6 +35,7 @@ public class Pong implements Runnable {

/**The balls*/
Ball b[];
static Rectangle star;
/**Number of balls*/
int ballNum;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}
Expand All @@ -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++){
Expand All @@ -282,7 +341,7 @@ public void run(){
try{
while( gameIsGoing ){
moveGame();
Thread.sleep( 15 );
Thread.sleep(15 );
}
}catch(Exception e){}

Expand Down
11 changes: 10 additions & 1 deletion src/edu/ucsb/cs56/projects/games/pong/gameplay/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -59,7 +60,39 @@ public void setToVisible()
*/
public String displayInstructions()
{
return ("<html><p><br>Difficulty:<br>The Screen size increases with the difficulty that is chosen.<br><br>How To Play:<br><br>Player 1 uses W to move the paddle up,<br> S to move their paddle down, and A to grab the ball.<br>Player 2 uses the Up and Down arrows to move their paddle up and<br> down and the left arrow to grab the ball.<br><br>When ball is being held and the player releases it while moving their paddle up or down,<br>ball will be released with the same speed that the paddle is moving<br><br>Hit the space bar to move the ball initially.<br><br>Press P to pause the game.<br>Press M to return to the Main Menu.<br>You lose a life if you miss the ball.<br>When you lose a life, the total hits will be added to your opponents score.<br>The winner is the player with remaining lives.<br><br>Chaos Mode:<br> very small ball on large screen moving at faster than normal speed<br><br>Custom Mode:<br>Allows players to select both level and how many balls they would like<br>to be in play at one time at the level selected.<br><br>Information on Ball to Paddle Speed:<br>When ball is released, it has the same y-velocity as your paddle.<br>The ball increases in speed after each hit of paddle.</p></html>");
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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><p><br>Difficulty:<br>The Screen size increases with the difficulty that is chosen.<br><br>How To Play:<br><br>Player 1 uses W to move the paddle up,<br> S to move their paddle down, and A to grab the ball.<br>Player 2 uses the Up and Down arrows to move their paddle up and<br> down and the left arrow to grab the ball.<br><br>When ball is being held and the player releases it while moving their paddle up or down,<br>ball will be released with the same speed that the paddle is moving<br><br>Hit the space bar to move the ball initially.<br><br>Press P to pause the game.<br>Press M to return to the Main Menu.<br>You lose a life if you miss the ball.<br>When you lose a life, the total hits will be added to your opponents score.<br>The winner is the player with remaining lives.<br><br>Chaos Mode:<br> very small ball on large screen moving at faster than normal speed<br><br>Custom Mode:<br>Allows players to select both level and how many balls they would like<br>to be in play at one time at the level selected.<br><br>Information on Ball to Paddle Speed:<br>When ball is released, it has the same y-velocity as your paddle.<br>The ball increases in speed after each hit of paddle.</p></html>

0 comments on commit 09f2b91

Please sign in to comment.