Working on a Pac-man game for my grade 12 computer science class.
I have game.java
file the runs the code through the main method, while I have a Play.java
file that runs the actual game.
This is what's inside the Play.java
file:
package javagame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
//extends BasicGameState creates a basic computer screen
public class Play extends BasicGameState {
float pacmanPosX = 218;
float pacmanPosY = 400;
BufferedImage maze;
private SpriteSheet[] pacmanSprite = new SpriteSheet[4];
private Animation[] pacmanAnimation = new Animation[4];
private Animation pacman;
private int score = 0;
public Play(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
try {
maze = ImageIO.read(new File("res/Game/Maze.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pacmanSprite[0] = new SpriteSheet("res/Characters/Left-Direction.png", 19, 20);
pacmanAnimation[0] = new Animation(pacmanSprite[0], 150);
pacmanSprite[1] = new SpriteSheet("res/Characters/Right-Direction.png", 19, 20);
pacmanAnimation[1] = new Animation(pacmanSprite[1], 150);
pacmanSprite[2] = new SpriteSheet("res/Characters/Up-Direction.png", 19, 20);
pacmanAnimation[2] = new Animation(pacmanSprite[2], 150);
pacmanSprite[3] = new SpriteSheet("res/Characters/Down-Direction.png", 19, 20);
pacmanAnimation[3] = new Animation(pacmanSprite[3], 150);
pacman = pacmanAnimation[0];
}
// This is the method that draws stuff on the screen
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawString("HIGH SCORE: ", 0, 0);
g.drawString("SCORE: ", 356, 0);
((Renderable) maze).draw(5, 35);
pacman.draw(pacmanPosX, pacmanPosY);
}
// Updating the images on the screen
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
pacman.update(delta);
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_UP)) {
pacman = pacmanAnimation[2];
pacmanPosY -= delta * .1f;
} else if(input.isKeyDown(Input.KEY_DOWN)) {
pacman = pacmanAnimation[3];
pacmanPosY += delta * .1f;
} else if(input.isKeyDown(Input.KEY_LEFT)) {
pacman = pacmanAnimation[0];
pacmanPosX -= delta * .1f;
} else if(input.isKeyDown(Input.KEY_RIGHT)) {
pacman = pacmanAnimation[1];
pacmanPosX += delta * .1f;
}
}
public int getID() {
return 1;
}
}
This is my plan to make sure Pac-man does not overlap the maze. What I plan on doing is to figure out the future position of the pixel Pac-man will overlap and what color that pixel will be. If the future color of the pixel is not black, then I won't allow Pac-man to continue moving in that direction.
How do I figure out the color of the future pixel? Some of you might suggest using BufferedImage
as it has the getRGB()
method, but that won't work because maze
is an image object, not a file object so I can't use BufferedImage
.