Slick Forums

Discuss the Slick 2D Library
It is currently Wed Nov 21, 2018 3:33 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Jun 07, 2018 3:23 am 
Offline

Joined: Thu Jun 07, 2018 3:18 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group