Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animation #6

Merged
merged 3 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/audio/dying.wav
Binary file not shown.
Binary file added assets/audio/gameover.wav
Binary file not shown.
Binary file added assets/mspacman/dying.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/pacman/dying.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 12 additions & 1 deletion src/edu/ucsb/cs56/projects/games/pacman/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ enum GameType {
private Timer timer;
private Audio beginningAudio;
private Audio gameoverAudio;
private Audio dyingAudio;
private GhostHouse ghostHouse;
private DevToolGui devTools = null;

Expand Down Expand Up @@ -103,6 +104,11 @@ public Board() {
} catch (Exception e) {
e.printStackTrace();
}
try {
this.dyingAudio = new Audio(getClass().getResourceAsStream("assets/audio/dying.wav"));
} catch (Exception e) {
e.printStackTrace();
}
}

/**
Expand All @@ -128,7 +134,12 @@ public void playGame(Graphics2D g2d) {
for (PacPlayer player : pacmen) {
if (pacman.getAnimateTimer() > 0)
pacman.decrementAnimateTimer();
pacman.draw(g2d, this);
pacman.dying(g2d, this);
try {
this.dyingAudio.play();
} catch (Exception e) {
e.printStackTrace();
}
if (pacman.getAnimateTimer() <= 0)
gameOver();
}
Expand Down
20 changes: 11 additions & 9 deletions src/edu/ucsb/cs56/projects/games/pacman/Ghost.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Ghost extends Character {
public static final int TYPE_ORANGE = 3;
public static final int GHOST1 = 1;
public static final int GHOST2 = 2;
public int blinky_x, blinky_y;

public static int defaultSpeed = 2;

Expand Down Expand Up @@ -348,6 +349,8 @@ public void moveAI(Grid grid, Character[] c)
double distance = 0;
if (type == TYPE_RED) {
distance = Math.sqrt(Math.pow(this.x - p.x, 2.0) + Math.pow(this.y - p.y, 2.0));
blinky_x = this.x;
blinky_y = this.y;
} else if (type == TYPE_PINK) {
int aheadX = p.x;
int aheadY = p.y;
Expand Down Expand Up @@ -403,7 +406,6 @@ else if (type == TYPE_BLUE) {
int aheadX = p.x;
int aheadY = p.y;
//get red ghosts location
//blinky = grid.get(TYPE_RED);
PacPlayer pacman = (PacPlayer) p;
if (pacman.direction == Direction.LEFT)
aheadX = p.x - 2;
Expand All @@ -414,19 +416,19 @@ else if (pacman.direction == Direction.RIGHT)
else
aheadY = p.y + 2;

if (aheadX > 18)
aheadX = 18;
if (aheadX > 16)
aheadX = 16;
else if (aheadX < 0)
aheadX = 0;
if (aheadY > 18)
aheadY = 18;
if (aheadY > 16)
aheadY = 16;
else if (aheadY < 0)
aheadY = 0;
//int vectorX = (aheadX - blinky.x)*2;
//int vectorY = (aheadY - blinky.y)*2;
int vectorX = (aheadX - blinky_x)*2;
int vectorY = (aheadY - blinky_y)*2;

// distance = Math.sqrt(Math.pow(this.x - vectorX, 2.0) + Math.pow(this.y - vectorY, 2.0));
distance = Math.sqrt(Math.pow(this.x - aheadX, 2.0) + Math.pow(this.y - aheadY, 2.0));
distance = Math.sqrt(Math.pow(this.x - vectorX, 2.0) + Math.pow(this.y - vectorY, 2.0));
//distance = Math.sqrt(Math.pow(this.x - aheadX, 2.0) + Math.pow(this.y - aheadY, 2.0));
}

if(p.alive && distance < 150.0) { // && Math.random() < 0.6)
Expand Down
33 changes: 21 additions & 12 deletions src/edu/ucsb/cs56/projects/games/pacman/PacPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.lang.Math;
import java.awt.geom.AffineTransform;
import java.awt.Graphics2D;



/**
Expand Down Expand Up @@ -46,6 +49,9 @@ public class PacPlayer extends Character {
private Audio[] pacmanAudio;
private String assetAudioPath;
private Grid grid;
private Image dying_1;
private Image dying_2;


/**
* Constructor for PacPlayer class
Expand Down Expand Up @@ -200,23 +206,23 @@ else if (direction == Direction.DOWN)
else
g2d.drawImage(pacmanRight[pacmananimpos], x + 4, y + 4, canvas);

}
}

public void dying(Graphics2D g2d, JComponent canvas) {
if (animateTimer %5 > 3){
g2d.rotate(Math.toRadians(90));
//draw shape/image (will be rotated)
return;
if(animateTimer > 0){
if(animateTimer % 3 == 1)
g2d.drawImage(dying_1, x + 4, y + 4, canvas);
else
g2d.drawImage(dying_2, x + 4, y + 4, canvas);
}
}


/**
* Moves character's current position while detecting for collision
* within the board
*
* @param grid The Grid to be used for collision detection
*/
/**
* Moves character's current position while detecting for collision
* within the board
*
* @param grid The Grid to be used for collision detection
*/
@Override
public void moveAI(Grid grid, Character[] c) {
}
Expand Down Expand Up @@ -343,6 +349,9 @@ public void loadImages()
pacmanRight[1] = ImageIO.read(getClass().getResource(assetImagePath + "right1.png"));
pacmanRight[2] = ImageIO.read(getClass().getResource(assetImagePath + "right2.png"));
pacmanRight[3] = ImageIO.read(getClass().getResource(assetImagePath + "right3.png"));
dying_1 = ImageIO.read(getClass().getResource(assetImagePath + "dying.png"));
dying_2 = ImageIO.read(getClass().getResource(assetImagePath + "right1.png"));

} catch (IOException e) {
e.printStackTrace();
}
Expand Down