-1

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.

Bert Hanz
  • 417
  • 1
  • 7
  • 16
  • *"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`."* An `Image` can be drawn to a `BufferedImage`.. – Andrew Thompson Jun 08 '18 at 09:59
  • I tried changing it to BufferedImage, but I can't use the `draw` method in my `render` method to put it on screen. – Bert Hanz Jun 08 '18 at 12:39
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 08 '18 at 12:59
  • @AndrewThompson I updated the code. When I try using the `draw` method, it asks me to cast it to `(Renderable)`, but when I run the program, it immediately closes the screen. I'm using Slick2D for this game. – Bert Hanz Jun 08 '18 at 13:09
  • *"I updated the code."* I advised you to post an MCVE / SSCCE. That is not. It depends on 3rd party APIs and has no code to put it on-screen. – Andrew Thompson Jun 08 '18 at 13:11

1 Answers1

0

Based on the line

maze = new Image("res/Game/Maze.png");

in your code, the maze is stored in a file. Therefore you can create a BufferedImage object from it. You should change that line to

try {
    maze = ImageIO.read(new File("res/Game/Maze.png"));
} catch (IOException e) {
}

make sure you define maze to be a BufferedImage!

BufferedImage maze;

and since BufferedImage is a subclass of Image, you can use the it in the same way you were using it before. You don't really need to redefine maze, but if you do then you can use one of the the getRGB() commands you mentioned.

Pillager225
  • 439
  • 1
  • 3
  • 15
  • So I changed my code to what you posted, but now I can't use the `draw` method to put it on screen. It asked to cast it to `(Renderable)`, but when I run the program, it immediately closes the screen. I'm using Slick2D for this game. – Bert Hanz Jun 08 '18 at 12:42
  • Oh, you did not mention you were using `org.newdawn.slick.Image` class. I assumed you were using `java.awt.Image`. You should add that bit of information to your question as it makes a big difference. – Pillager225 Jun 08 '18 at 16:38
  • After reading the documentation for `org.newdawn.slick.Image`, it has a function whose signature is `Color getColor(int x, int y)`. It does exactly what you want it to do. I recommend you [RTFM](http://slick.ninjacave.com/javadoc/org/newdawn/slick/Image.html). – Pillager225 Jun 08 '18 at 16:41
  • So I added the line `System.out.println(maze.getColor(5, 35));` to my `init()` method. This is what I see in the console: `Color (0.0,0.0,0.0,0.0)` `Color (0.0,0.0,0.0,0.0)`. What do these values mean? – Bert Hanz Jun 08 '18 at 17:45
  • As I mentioned before, I recommend you [RTFM](https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html) – Pillager225 Jun 08 '18 at 21:47