Slick Forums
http://slick.ninjacave.com/forum/

How do I add collision to an image/sprite in Slick2D?
http://slick.ninjacave.com/forum/viewtopic.php?f=3&t=8458
Page 1 of 1

Author:  singlespace56 [ Thu Jun 07, 2018 3:23 am ]
Post subject:  How do I add collision to an image/sprite in Slick2D?

I'm working on a Pac-man game for my Grade 12 computer science class.

This is the code that I so far have:

Player.java:

Code:
package javagame;

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;

    Image maze;

    //private SpriteSheet pacMovingLeft;
    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 {
        maze = new Image("res/Game/Maze.png");

        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);

        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;
    }
}


I have a Menu.java file that takes you to the Play state, but that code is irrelevant to my question.

How do I add collision to the maze? I remember in Grade 11 I worked on a game using Phaser and I was able to methods/functions such as physics and collideWorldBounds to my images. This allowed objects to bounce off images/sprites that had these methods. I need to add some physics to the maze so Pac-man can't go through the maze.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/