Looking for help on my current Java Project. It is to create a pacman game I hav
ID: 3808961 • Letter: L
Question
Looking for help on my current Java Project.
It is to create a pacman game I have most of the code done I just need help on adding the dots for the pacman to eat and then the ghosts.
Here is the program requirments
You are going to use the maze from Lab 4 and recreate that maze. Then, instead of blank spaces, you are to put dots in there. Then, you should have a single player that can move through the maze collecting the dots. Feel free to use the collision detection from the first three labs.
You should have 3 lives. As you collect dots, the score should continually update on the screen.
Finally, you should have a couple of “monsters” that follow you around the screen autonomously. Remember, they are unable to move through walls. However, if they hit you lose a life and the level is over after all the dots are collected.
So if anyone can help with the dots and ghosts that would be appericated.
Code follows
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Maze extends JFrame implements KeyListener {
private static final String[] FILE = { "maze1.txt", "maze2.txt" };
private static final int mazeWidth = 50;
private static final int mazeHeight = 50;
private static final int LEFT = -1;
private static final int RIGHT = 1;
private static final int UP = -1;
private static final int DOWN = 1;
private int[][] maze;
private JLabel[][] mazeLabel;
private int row;
private int col;
private int entryX = -1;
private int entryY = -1;
private int exitX = -1;
private int exitY = -1;
private int currX = -1;
private int currY = -1;
private ImageIcon playerImg;
private boolean hasWon;
public Maze() {
super("Maze");
// Reads the maze from input file
startMaze();
if (this.maze.length > 0) {
// Finds entry and exit
findStartEnd();
if ((this.entryX != -1) && (this.entryY != -1) && (this.exitX != -1) && (this.exitY != -1)) {
// Draws maze
drawMaze();
// Sets current position
this.currX = this.entryX;
this.currY = this.entryY;
// Place the player in the maze
setPlayer();
} else
// Prints if error reading the input from maze ie no 0's on border
System.out.println("No Entry/Exit point(s) found.");
} else {
System.out.println("No maze found.");
}
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addKeyListener(this);
requestFocus();
this.hasWon = false;
}
private void startMaze() {
// Selects a random file for the maze, this fullfils requirement#3
int n = (int) (Math.random() * 10) % 2;
// Use scanner file to read it the maze.txt file. I also had to look for help with the try exception as i was having trouble getting the file read in this was what was best suggested so
// so this part isn't my code and i know we will learn about the try later but just wanted to give a heads up.
Scanner file = null;
try {
file = new Scanner(new File(FILE[n]));
// This puts the file into the array, once again not my code here here had help with this part. this fullfills requiremnt 2 i think?
String[] lines = new String[0];
while (file.hasNextLine()) {
int len = lines.length;
lines = Arrays.copyOf(lines, len + 1);
lines[len] = file.nextLine().replaceAll("\s+", "");
}
if (lines.length > 0) {
this.row = lines.length;
this.col = lines[0].length();
this.maze = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
this.maze[i][j] = (lines[i].charAt(j) == '0') ? 0 : 1;
}
}
} catch (FileNotFoundException e) {
System.out.println("No file found: " + FILE[n]);
System.exit(0);
} finally {
if (file != null)
file.close();
}
}
private void drawMaze() {
// This draws the maze from the txt file.
setLayout(null);
getContentPane().setPreferredSize(new Dimension((col * mazeWidth), (row * mazeHeight)));
pack();
ImageIcon image = new ImageIcon("brick.jpg");
// This resizes the image to the Maze, had help here as well with the Scale defualt I looked this up online.
image = new ImageIcon(image.getImage().getScaledInstance(mazeWidth, mazeHeight, Image.SCALE_DEFAULT));
this.mazeLabel = new JLabel[row][col];
int y = 0;
for (int i = 0; i < row; i++) {
int x = 0;
for (int j = 0; j < col; j++) {
this.mazeLabel[i][j] = new JLabel();
this.mazeLabel[i][j].setBounds(x, y, mazeWidth, mazeHeight);
this.mazeLabel[i][j].setOpaque(true);
if (this.maze[i][j] == 1)
this.mazeLabel[i][j].setIcon(image);
else
this.mazeLabel[i][j].setBackground(Color.WHITE);
// Adds Jlabel/Maze into the main panel
add(this.mazeLabel[i][j]);
x += mazeWidth;
}
y += mazeHeight;
}
}
//This is the method/logic that finds the start of the maze and exit of the maze. Had some help with this part as well as i was having trouble ending the game
private void findStartEnd() {
for (int i = 0; i < row; i++) {
if (this.maze[i][0] == 0) {
this.entryX = i;
this.entryY = 0;
break;
}
}
for (int i = 0; i < col; i++) {
if (this.maze[0][i] == 0) {
if ((this.entryX == -1) && (this.entryY == -1)) {
this.entryX = 0;
this.entryY = i;
break;
} else if ((this.exitX != -1) && (this.exitY != -1)) {
this.exitX = 0;
this.exitY = i;
break;
}
}
}
if (((this.entryX == -1) && (this.entryY == -1)) || ((this.exitX == -1) && (this.exitY == -1))) {
for (int i = 0; i < row; i++) {
if (this.maze[i][col - 1] == 0)
if ((this.entryX == -1) && (this.entryY == -1)) {
this.entryX = i;
this.entryY = col - 1;
break;
} else if ((this.exitX == -1) && (this.exitY == -1)) {
this.exitX = i;
this.exitY = col - 1;
break;
}
}
for (int i = 0; i < col; i++) {
if (this.maze[row - 1][i] == 0) {
if ((this.entryX == -1) && (this.entryY == -1)) {
this.entryX = row - 1;
this.entryY = i;
break;
} else if ((this.exitX == -1) && (this.exitY == -1)) {
this.exitX = row - 1;
this.exitY = i;
break;
}
}
}
}
}
//Puts the player in the maze
private void setPlayer() {
playerImg = new ImageIcon("2000px-Pacman.svg.png");
playerImg = new ImageIcon(playerImg.getImage().getScaledInstance(mazeWidth, mazeHeight, Image.SCALE_DEFAULT));
this.mazeLabel[currX][currY].setIcon(playerImg);
}
// checks the current loction of player
private void setNewLocation(int newX, int newY) {
this.mazeLabel[currX][currY].setIcon(null);
this.mazeLabel[currX][currY].setBackground(Color.WHITE);
currX = newX;
currY = newY;
this.mazeLabel[currX][currY].setIcon(playerImg);
}
// This checks to see if a horizontal move is valid and if there is a open space
private void checkHorizontal(int dir) {
if (dir == LEFT) {
if ((currY > 0) && (this.maze[currX][currY - 1] == 0)) {
setNewLocation(currX, currY - 1);
}
} else if (dir == RIGHT) {
if ((currY < (col - 1)) && (this.maze[currX][currY + 1] == 0)) {
setNewLocation(currX, currY + 1);
}
}
}
// This checks to see if a vertical move is valid and if there is a open space
private void checkVertical(int dir) {
if (dir == UP) {
if ((currX > 0) && (this.maze[currX - 1][currY] == 0)) {
setNewLocation(currX - 1, currY);
}
} else if (dir == DOWN) {
if ((currX < (row - 1)) && (this.maze[currX + 1][currY] == 0)) {
setNewLocation(currX + 1, currY);
}
}
}
// Player movement i use awsd for movement
public void keyPressed(KeyEvent ke) {
if (!hasWon) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A:
checkHorizontal(LEFT);
break;
case KeyEvent.VK_W:
checkVertical(UP);
break;
case KeyEvent.VK_D:
checkHorizontal(RIGHT);
break;
case KeyEvent.VK_S:
checkVertical(DOWN);
}
// Check if the player exits the maze
if ((currX == exitX) && (currY == exitY)) {
this.hasWon = true;
JOptionPane.showMessageDialog(this, "You found the exit");
}
repaint();
}
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public static void main(String[] args) {
Maze maze = new Maze();
maze.setVisible(true);
}
}
2000px-Pacman.svg.png
brick.jpg
Explanation / Answer
The code which you are using is having some errors by which it is not executing flawlessly. Please, use this code mentioned below which is giving you the deisred output and is working.
//Source Code for Pacman Game
package com.pcman;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Brd extends JPanel implements ActionListener {
private Dimension d;
private final Font smallFont = new Font("Helvetica", Font.BOLD, 14);
private Image ii;
private final Color dotColor = new Color(192, 192, 0);
private Color mazeColor;
private boolean inGame = false;
private boolean dying = false;
private final int BLOCK_SIZE = 24;
private final int N_BLOCKS = 15;
private final int SCREEN_SIZE = N_BLOCKS * BLOCK_SIZE;
private final int PAC_ANIM_DELAY = 2;
private final int PACMAN_ANIM_COUNT = 4;
private final int MAX_GHOSTS = 12;
private final int PACMAN_SPEED = 6;
private int pacAnimCount = PAC_ANIM_DELAY;
private int pacAnimDir = 1;
private int pacmanAnimPos = 0;
private int N_GHOSTS = 6;
private int pacsLeft, score;
private int[] dx, dy;
private int[] ghost_x, ghost_y, ghost_dx, ghost_dy, ghostSpeed;
private Image ghost;
private Image pacman1, pacman2up, pacman2left, pacman2right, pacman2down;
private Image pacman3up, pacman3down, pacman3left, pacman3right;
private Image pacman4up, pacman4down, pacman4left, pacman4right;
private int pacman_x, pacman_y, pacmand_x, pacmand_y;
private int req_dx, req_dy, view_dx, view_dy;
private final short levelData[] = {
19, 26, 26, 26, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22,
21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
21, 0, 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20,
21, 0, 0, 0, 17, 16, 16, 24, 16, 16, 16, 16, 16, 16, 20,
17, 18, 18, 18, 16, 16, 20, 0, 17, 16, 16, 16, 16, 16, 20,
17, 16, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 16, 24, 20,
25, 16, 16, 16, 24, 24, 28, 0, 25, 24, 24, 16, 20, 0, 21,
1, 17, 16, 20, 0, 0, 0, 0, 0, 0, 0, 17, 20, 0, 21,
1, 17, 16, 16, 18, 18, 22, 0, 19, 18, 18, 16, 20, 0, 21,
1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21,
1, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16, 20, 0, 21,
1, 17, 16, 16, 16, 16, 16, 18, 16, 16, 16, 16, 20, 0, 21,
1, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 21,
1, 25, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 18, 20,
9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 25, 24, 24, 24, 28
};
private final int validSpeeds[] = {1, 2, 3, 4, 6, 8};
private final int maxSpeed = 6;
private int currentSpeed = 3;
private short[] screenData;
private Timer timer;
public Brd() {
loadImages();
initVariables();
initBrd();
}
private void initBrd() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.black);
setDoubleBuffered(true);
}
private void initVariables() {
screenData = new short[N_BLOCKS * N_BLOCKS];
mazeColor = new Color(5, 100, 5);
d = new Dimension(400, 400);
ghost_x = new int[MAX_GHOSTS];
ghost_dx = new int[MAX_GHOSTS];
ghost_y = new int[MAX_GHOSTS];
ghost_dy = new int[MAX_GHOSTS];
ghostSpeed = new int[MAX_GHOSTS];
dx = new int[4];
dy = new int[4];
timer = new Timer(40, this);
timer.start();
}
@Override
public void addNotify() {
super.addNotify();
initGame();
}
private void doAnim() {
pacAnimCount--;
if (pacAnimCount <= 0) {
pacAnimCount = PAC_ANIM_DELAY;
pacmanAnimPos = pacmanAnimPos + pacAnimDir;
if (pacmanAnimPos == (PACMAN_ANIM_COUNT - 1) || pacmanAnimPos == 0) {
pacAnimDir = -pacAnimDir;
}
}
}
private void playGame(Graphics2D g2d) {
if (dying) {
death();
} else {
movePacman();
drawPacman(g2d);
moveGhosts(g2d);
checkMaze();
}
}
private void showIntroScreen(Graphics2D g2d) {
g2d.setColor(new Color(0, 32, 48));
g2d.fillRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50);
g2d.setColor(Color.white);
g2d.drawRect(50, SCREEN_SIZE / 2 - 30, SCREEN_SIZE - 100, 50);
String s = "Press s to start.";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g2d.setColor(Color.white);
g2d.setFont(small);
g2d.drawString(s, (SCREEN_SIZE - metr.stringWidth(s)) / 2, SCREEN_SIZE / 2);
}
private void drawScore(Graphics2D g) {
int i;
String s;
g.setFont(smallFont);
g.setColor(new Color(96, 128, 255));
s = "Score: " + score;
g.drawString(s, SCREEN_SIZE / 2 + 96, SCREEN_SIZE + 16);
for (i = 0; i < pacsLeft; i++) {
g.drawImage(pacman3left, i * 28 + 8, SCREEN_SIZE + 1, this);
}
}
private void checkMaze() {
short i = 0;
boolean finished = true;
while (i < N_BLOCKS * N_BLOCKS && finished) {
if ((screenData[i] & 48) != 0) {
finished = false;
}
i++;
}
if (finished) {
score += 50;
if (N_GHOSTS < MAX_GHOSTS) {
N_GHOSTS++;
}
if (currentSpeed < maxSpeed) {
currentSpeed++;
}
initLevel();
}
}
private void death() {
pacsLeft--;
if (pacsLeft == 0) {
inGame = false;
}
continueLevel();
}
private void moveGhosts(Graphics2D g2d) {
short i;
int pos;
int count;
for (i = 0; i < N_GHOSTS; i++) {
if (ghost_x[i] % BLOCK_SIZE == 0 && ghost_y[i] % BLOCK_SIZE == 0) {
pos = ghost_x[i] / BLOCK_SIZE + N_BLOCKS * (int) (ghost_y[i] / BLOCK_SIZE);
count = 0;
if ((screenData[pos] & 1) == 0 && ghost_dx[i] != 1) {
dx[count] = -1;
dy[count] = 0;
count++;
}
if ((screenData[pos] & 2) == 0 && ghost_dy[i] != 1) {
dx[count] = 0;
dy[count] = -1;
count++;
}
if ((screenData[pos] & 4) == 0 && ghost_dx[i] != -1) {
dx[count] = 1;
dy[count] = 0;
count++;
}
if ((screenData[pos] & 8) == 0 && ghost_dy[i] != -1) {
dx[count] = 0;
dy[count] = 1;
count++;
}
if (count == 0) {
if ((screenData[pos] & 15) == 15) {
ghost_dx[i] = 0;
ghost_dy[i] = 0;
} else {
ghost_dx[i] = -ghost_dx[i];
ghost_dy[i] = -ghost_dy[i];
}
} else {
count = (int) (Math.random() * count);
if (count > 3) {
count = 3;
}
ghost_dx[i] = dx[count];
ghost_dy[i] = dy[count];
}
}
ghost_x[i] = ghost_x[i] + (ghost_dx[i] * ghostSpeed[i]);
ghost_y[i] = ghost_y[i] + (ghost_dy[i] * ghostSpeed[i]);
drawGhost(g2d, ghost_x[i] + 1, ghost_y[i] + 1);
if (pacman_x > (ghost_x[i] - 12) && pacman_x < (ghost_x[i] + 12)
&& pacman_y > (ghost_y[i] - 12) && pacman_y < (ghost_y[i] + 12)
&& inGame) {
dying = true;
}
}
}
private void drawGhost(Graphics2D g2d, int x, int y) {
g2d.drawImage(ghost, x, y, this);
}
private void movePacman() {
int pos;
short ch;
if (req_dx == -pacmand_x && req_dy == -pacmand_y) {
pacmand_x = req_dx;
pacmand_y = req_dy;
view_dx = pacmand_x;
view_dy = pacmand_y;
}
if (pacman_x % BLOCK_SIZE == 0 && pacman_y % BLOCK_SIZE == 0) {
pos = pacman_x / BLOCK_SIZE + N_BLOCKS * (int) (pacman_y / BLOCK_SIZE);
ch = screenData[pos];
if ((ch & 16) != 0) {
screenData[pos] = (short) (ch & 15);
score++;
}
if (req_dx != 0 || req_dy != 0) {
if (!((req_dx == -1 && req_dy == 0 && (ch & 1) != 0)
|| (req_dx == 1 && req_dy == 0 && (ch & 4) != 0)
|| (req_dx == 0 && req_dy == -1 && (ch & 2) != 0)
|| (req_dx == 0 && req_dy == 1 && (ch & 8) != 0))) {
pacmand_x = req_dx;
pacmand_y = req_dy;
view_dx = pacmand_x;
view_dy = pacmand_y;
}
}
// Check for standstill
if ((pacmand_x == -1 && pacmand_y == 0 && (ch & 1) != 0)
|| (pacmand_x == 1 && pacmand_y == 0 && (ch & 4) != 0)
|| (pacmand_x == 0 && pacmand_y == -1 && (ch & 2) != 0)
|| (pacmand_x == 0 && pacmand_y == 1 && (ch & 8) != 0)) {
pacmand_x = 0;
pacmand_y = 0;
}
}
pacman_x = pacman_x + PACMAN_SPEED * pacmand_x;
pacman_y = pacman_y + PACMAN_SPEED * pacmand_y;
}
private void drawPacman(Graphics2D g2d) {
if (view_dx == -1) {
drawPacnanLeft(g2d);
} else if (view_dx == 1) {
drawPacmanRight(g2d);
} else if (view_dy == -1) {
drawPacmanUp(g2d);
} else {
drawPacmanDown(g2d);
}
}
private void drawPacmanUp(Graphics2D g2d) {
switch (pacmanAnimPos) {
case 1:
g2d.drawImage(pacman2up, pacman_x + 1, pacman_y + 1, this);
break;
case 2:
g2d.drawImage(pacman3up, pacman_x + 1, pacman_y + 1, this);
break;
case 3:
g2d.drawImage(pacman4up, pacman_x + 1, pacman_y + 1, this);
break;
default:
g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
break;
}
}
private void drawPacmanDown(Graphics2D g2d) {
switch (pacmanAnimPos) {
case 1:
g2d.drawImage(pacman2down, pacman_x + 1, pacman_y + 1, this);
break;
case 2:
g2d.drawImage(pacman3down, pacman_x + 1, pacman_y + 1, this);
break;
case 3:
g2d.drawImage(pacman4down, pacman_x + 1, pacman_y + 1, this);
break;
default:
g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
break;
}
}
private void drawPacnanLeft(Graphics2D g2d) {
switch (pacmanAnimPos) {
case 1:
g2d.drawImage(pacman2left, pacman_x + 1, pacman_y + 1, this);
break;
case 2:
g2d.drawImage(pacman3left, pacman_x + 1, pacman_y + 1, this);
break;
case 3:
g2d.drawImage(pacman4left, pacman_x + 1, pacman_y + 1, this);
break;
default:
g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
break;
}
}
private void drawPacmanRight(Graphics2D g2d) {
switch (pacmanAnimPos) {
case 1:
g2d.drawImage(pacman2right, pacman_x + 1, pacman_y + 1, this);
break;
case 2:
g2d.drawImage(pacman3right, pacman_x + 1, pacman_y + 1, this);
break;
case 3:
g2d.drawImage(pacman4right, pacman_x + 1, pacman_y + 1, this);
break;
default:
g2d.drawImage(pacman1, pacman_x + 1, pacman_y + 1, this);
break;
}
}
private void drawMaze(Graphics2D g2d) {
short i = 0;
int x, y;
for (y = 0; y < SCREEN_SIZE; y += BLOCK_SIZE) {
for (x = 0; x < SCREEN_SIZE; x += BLOCK_SIZE) {
g2d.setColor(mazeColor);
g2d.setStroke(new BasicStroke(2));
if ((screenData[i] & 1) != 0) {
g2d.drawLine(x, y, x, y + BLOCK_SIZE - 1);
}
if ((screenData[i] & 2) != 0) {
g2d.drawLine(x, y, x + BLOCK_SIZE - 1, y);
}
if ((screenData[i] & 4) != 0) {
g2d.drawLine(x + BLOCK_SIZE - 1, y, x + BLOCK_SIZE - 1,
y + BLOCK_SIZE - 1);
}
if ((screenData[i] & 8) != 0) {
g2d.drawLine(x, y + BLOCK_SIZE - 1, x + BLOCK_SIZE - 1,
y + BLOCK_SIZE - 1);
}
if ((screenData[i] & 16) != 0) {
g2d.setColor(dotColor);
g2d.fillRect(x + 11, y + 11, 2, 2);
}
i++;
}
}
}
private void initGame() {
pacsLeft = 3;
score = 0;
initLevel();
N_GHOSTS = 6;
currentSpeed = 3;
}
private void initLevel() {
int i;
for (i = 0; i < N_BLOCKS * N_BLOCKS; i++) {
screenData[i] = levelData[i];
}
continueLevel();
}
private void continueLevel() {
short i;
int dx = 1;
int random;
for (i = 0; i < N_GHOSTS; i++) {
ghost_y[i] = 4 * BLOCK_SIZE;
ghost_x[i] = 4 * BLOCK_SIZE;
ghost_dy[i] = 0;
ghost_dx[i] = dx;
dx = -dx;
random = (int) (Math.random() * (currentSpeed + 1));
if (random > currentSpeed) {
random = currentSpeed;
}
ghostSpeed[i] = validSpeeds[random];
}
pacman_x = 7 * BLOCK_SIZE;
pacman_y = 11 * BLOCK_SIZE;
pacmand_x = 0;
pacmand_y = 0;
req_dx = 0;
req_dy = 0;
view_dx = -1;
view_dy = 0;
dying = false;
}
private void loadImages() {
ghost = new ImageIcon("images/ghost.png").getImage();
pacman1 = new ImageIcon("images/pacman.png").getImage();
pacman2up = new ImageIcon("images/up1.png").getImage();
pacman3up = new ImageIcon("images/up2.png").getImage();
pacman4up = new ImageIcon("images/up3.png").getImage();
pacman2down = new ImageIcon("images/down1.png").getImage();
pacman3down = new ImageIcon("images/down2.png").getImage();
pacman4down = new ImageIcon("images/down3.png").getImage();
pacman2left = new ImageIcon("images/left1.png").getImage();
pacman3left = new ImageIcon("images/left2.png").getImage();
pacman4left = new ImageIcon("images/left3.png").getImage();
pacman2right = new ImageIcon("images/right1.png").getImage();
pacman3right = new ImageIcon("images/right2.png").getImage();
pacman4right = new ImageIcon("images/right3.png").getImage();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.fillRect(0, 0, d.width, d.height);
drawMaze(g2d);
drawScore(g2d);
doAnim();
if (inGame) {
playGame(g2d);
} else {
showIntroScreen(g2d);
}
g2d.drawImage(ii, 5, 5, this);
Toolkit.getDefaultToolkit().sync();
g2d.dispose();
}
class TAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (inGame) {
if (key == KeyEvent.VK_LEFT) {
req_dx = -1;
req_dy = 0;
} else if (key == KeyEvent.VK_RIGHT) {
req_dx = 1;
req_dy = 0;
} else if (key == KeyEvent.VK_UP) {
req_dx = 0;
req_dy = -1;
} else if (key == KeyEvent.VK_DOWN) {
req_dx = 0;
req_dy = 1;
} else if (key == KeyEvent.VK_ESCAPE && timer.isRunning()) {
inGame = false;
} else if (key == KeyEvent.VK_PAUSE) {
if (timer.isRunning()) {
timer.stop();
} else {
timer.start();
}
}
} else {
if (key == 's' || key == 'S') {
inGame = true;
initGame();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == Event.LEFT || key == Event.RIGHT
|| key == Event.UP || key == Event.DOWN) {
req_dx = 0;
req_dy = 0;
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.