Skip to content
Open
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 Pacman/MITpacman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 49 additions & 5 deletions Pacman/src/PacmanGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/

public class PacmanGame extends JPanel implements MouseMotionListener, MouseListener, KeyListener {

int mouse_x;
int mouse_y;
int Pmouse_x;
Expand All @@ -55,14 +55,15 @@ public class PacmanGame extends JPanel implements MouseMotionListener, MouseList
boolean draw_spiral_next_time = false;
static int SQUARE_SIZE = 40;
static int PACMAN_SPEED = 8;
static int ENEMY_SPEED = 8;
static int ENEMY_SPEED = 5;
static int score = 0;
long timeSinceRestart;

Pacman pacman;

Organism enemy1;
Organism enemy2;
Organism enemy3;
// Organism[] enemies;

int pendingKeyCode = -1; // no pending key at the beginning
Expand Down Expand Up @@ -105,6 +106,7 @@ public void restart(){
pacman = new Pacman(7*SQUARE_SIZE, 6*SQUARE_SIZE);
enemy1 = new Enemy(-1, -1, pacman);
enemy2 = new Enemy(-1, -1, pacman);
enemy3 = new Enemy(-1, -1, pacman);
score = 0;
pendingKeyCode = -1;
timeSinceRestart = System.currentTimeMillis();
Expand All @@ -116,7 +118,7 @@ public void restart(){
}
}

public static void loadBoard() {
public static void loadBoard(String level) {
InputStream fis;
BufferedReader br;
String line;
Expand All @@ -132,7 +134,7 @@ public static void loadBoard() {

try
{
fis = new FileInputStream("level.txt");
fis = new FileInputStream(level);
br = new BufferedReader(new InputStreamReader(fis));

int num_rows = -1;
Expand Down Expand Up @@ -167,7 +169,19 @@ else if (num_cols < 0) {

public static void main(String[] args) {

loadBoard();
String level = "level.txt";
if(args.length != 0) {
level = args[0];
System.out.print("Using specified level: ");
System.out.println(level);
}
else {

System.out.print("Using default level: ");
System.out.println(level);
}

loadBoard(level);

JFrame frame = new JFrame();
frame.setSize(original_board[0].length * SQUARE_SIZE, original_board.length * SQUARE_SIZE+20);
Expand Down Expand Up @@ -293,7 +307,32 @@ public void paintComponent(Graphics oldg)
(pacman.y==enemy2.y && Math.abs(pacman.x-enemy2.x)<SQUARE_SIZE)){
restart();
}

}

if (System.currentTimeMillis() - timeSinceRestart > 10000) {
if (enemy3.x < 0) {
System.out.println("Spawning enemy 3 -- time is " + System.currentTimeMillis());
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == Things.ENEMY_SPAWNING_PLACE) {
enemy3.x = j * SQUARE_SIZE;
enemy3.y = i * SQUARE_SIZE;
enemy3.dir = Organism.Direction.UP;
}
}
}
}
else {
enemy3.move(board, ENEMY_SPEED);
}
if ((pacman.x==enemy3.x && Math.abs(pacman.y-enemy3.y)<SQUARE_SIZE) ||
(pacman.y==enemy3.y && Math.abs(pacman.x-enemy3.x)<SQUARE_SIZE)){
restart();
}

}

}
}

Expand Down Expand Up @@ -334,6 +373,11 @@ else if (board[i][j]== Things.ENEMY_SPAWNING_GATE){
g.setColor(new Color(100, 200, 100));
g.fillRect(enemy2.x, enemy2.y, SQUARE_SIZE, SQUARE_SIZE);
}

if (enemy3.x >= 0) {
g.setColor(new Color(200, 200, 100));
g.fillRect(enemy3.x, enemy3.y, SQUARE_SIZE, SQUARE_SIZE);
}

// Draw the pause button
g.setColor(new Color(255, 0, 0));
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions Pacman/src/level2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
15
35
###################################
#..####....##.#........#..........#
#..#CC#...#CC.#CCCCCCC.#CCCCCCC...#
#..#C#C.#.C#C.#...C....#...C......#
#..#C##C#C..C.#..#C....#...C......#
#..#C##.C###C.=..#C....#...C......#
#..#C#......C.#...C....#...C......#
#..#C#......C.#...C....#...C......#
#..#C#......C.#CCCCCCC.....C......#
#.............#........#=#........#
#...#=#.......#.......#---#.......#
#..#---#......#......#######......#
###################################
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# Pacman
Author: Henry Friedlander

Single player Pacman game with GUI. Java program using Swing.

![alt text](https://raw.githubusercontent.com/peteflorence/Pacman/master/Pacman/MITpacman.png "MIT level :)")

### Quickstart with Eclipse

Run with Eclipse project

### Quickstart without Eclipse

```
cd Pacman/src
for i in *.java; do javac *.java; done
java PacmanGame

```

To run a different level, for example:


```
java PacmanGame level2.txt
```