Results 1 to 15 of 15
Thread: Making PacMan game, need help!
- 05-21-2011, 02:15 AM #1
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Making PacMan game, need help!
I am attempting to create pacman from scratch in java. I am using a GUI to do so. My program feels like a mess however, and I still need to work on some major components.
I am making this thread because I will most likely be having problems during the process of making the game and I can use this to periodically ask questions:o.
My current problem: getting the keylistener to work. I am running the keylistener methods in PacRunner. I have the key bindings set to the arrow keys, so the arrow key corresponds to the appropriate pacman move. As of now nothing happens.
Here's my code:
Grid Class
PacRunnerJava Code:import javax.swing.*; import java.awt.*; import java.util.ArrayList; public class Grid{ private static ArrayList<Ghost> enemies; private static Pacman Player; private static ArrayList<Dot> food; private static Container pane; private static int index; private JFrame game; public Grid() { enemies = new ArrayList<Ghost>(); game = new JFrame(); game.setFocusable(true); game.setResizable(false); game.setTitle("Pac-Man 2.0"); game.setPreferredSize(new Dimension(1000, 450)); game.setBackground(Color.BLACK); food = new ArrayList<Dot>(); Player = new Pacman(); int row = 10, col = 25; index = 0; game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane = game.getContentPane(); pane.setBackground(Color.white); pane.setLayout(new GridLayout(row, col, 0, 0)); for(int i = 0; i < row; i++) { for(int a = 0; a < col; a++) { if(getGrid()[i][a] == 0){ //Adding Dot Images to Grid Dot d = new Dot(); d.setLocation(new Location(i, a)); food.add(d); pane.add(new JLabel(d.getImage())); } else //Adding Boundery Images to Grid pane.add(new JLabel(new ImageIcon("Wall.gif"))); } } game.pack(); } private static int[][] getGrid(){ //Creating Layout of grid int[][] gr = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,1,1,1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,1,1,1,0,1,1,0,0,0,0,0,0,1,0,1,1,1,1,0,0,1,0,1}, {1,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,1}, {1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,1,1,1,1,0,1,0,1}, {1,0,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1}, {1,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}; return gr; } public ArrayList<Ghost> getActors(){ //Returns array of ghosts return enemies; } public Pacman getPac() { //Returns pacman return Player; } public static void move(int direction) { //Move method for pacman Location nw = new Location(getLocationInDirection(direction, Player.getLocation())); Location old = new Location(Player.getLocation()); if(isValid(nw)) { replace(old); movePac(nw); Player.setLocation(nw); } } public static void move(int direction, int g) { //Move method for Ghosts Location nw = new Location(getLocationInDirection(direction, enemies.get(g).getLocation())); Location old = new Location(enemies.get(g).getLocation()); if(isValid(nw)) { int num = (old.getRow() * 25) + old.getCol(); boolean found = false; for(int i = 0; i < food.size(); i++){ if(food.get(i).getLocation().isEqual(old)){ pane.remove(num); pane.add(new JLabel(new ImageIcon("dot.gif")), num); found = true; } } if(!found) replace(old); moveTo(nw, g); enemies.get(g).setLocation(nw); } } private static boolean isValid(Location loc) { //Checks if location is a wall int r = loc.getRow(), c = loc.getCol(); if(getGrid()[r][c] == 0) return true; return false; } public static void movePac(Location x) { //moves pacman to location for(int i = 0; i < food.size(); i++) if(food.get(i).getLocation().isEqual(x)) food.remove(i); if(isValid(x)) { int num = (x.getRow() * 25) + x.getCol(); pane.remove(num); pane.add(new JLabel(Player.getImage()), num); Player.setLocation(x); } } public static void moveTo(Location x, int value) { //Moves ghost to location if(isValid(x)) { int num = (x.getRow() * 25) + x.getCol(); pane.remove(num); pane.add(new JLabel(enemies.get(value).getImage()), num); enemies.get(value).setLocation(x); } } public static void addGhost(Location x) { //Adds Ghost into game if(isValid(x)){ Ghost g = new Ghost(); int num = (x.getRow() * 25) + x.getCol(); pane.remove(num); pane.add(new JLabel(g.getImage()), num); g.setLocation(x); g.setIndex(index); enemies.add(g); index++; } } public static void replace(Location loc) { //Replaces spot on grid with floor piece int num = (loc.getRow() * 25) + loc.getCol(); pane.remove(num); pane.add(new JLabel(new ImageIcon("Floor.gif")), num); } public void show() { //Displays Grid game.setVisible(true); } public static Location getLocationInDirection(int d, Location b) { if(d == Location.RIGHT) return new Location(b.getRow(), b.getCol() + 1); else if(d == Location.LEFT) return new Location(b.getRow(), b.getCol() - 1); else if(d == Location.UP) return new Location(b.getRow() - 1, b.getCol()); else if(d == Location.DOWN) return new Location(b.getRow() + 1, b.getCol()); else return null; } }
PlayersJava Code:import java.awt.event.*; import java.awt.event.KeyEvent; public class PacRunner implements KeyListener { private static Grid gr; public static void main (String[] args) { gr = new Grid(); gr.addGhost(new Location(1,1)); gr.movePac(new Location(4,1)); gr.show(); } public void keyPressed(KeyEvent e) { if(e.getKeyCode() == 38) gr.move(Location.UP); else if(e.getKeyCode() == 40) gr.move(Location.DOWN); else if(e.getKeyCode() == 37) gr.move(Location.LEFT); else if(e.getKeyCode() == 39) gr.move(Location.RIGHT); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } }
PacmanJava Code:import javax.swing.*; import java.awt.*; public class Players{ private Location loc; public Players(){ } public Location getLocation(){ return loc; } public void setLocation(Location k) { loc.set(k.getRow(), k.getCol()); } }
LocationJava Code:import javax.swing.*; import java.awt.*; public class Pacman extends Players { private Location loc; private int life; public Pacman() { life = 0; loc = new Location(-1,-1); } public ImageIcon getImage() { return new ImageIcon("pac.png"); } public Location getLocation(){ return loc; } public void setLocation(Location k) { loc.set(k.getRow(), k.getCol()); } }
GhostJava Code:public class Location{ private int row, col; public static final int RIGHT = 0, LEFT = 180, UP = 90, DOWN = 270; public Location(int r, int c){ row = r; col = c; } public Location (Location loc) { row = loc.getRow(); col = loc.getCol(); } public boolean isEqual(Location c) { if(row == c.getRow() && col == c.getCol()) return true; return false; } public int getRow(){ return row; } public int getCol(){ return col; } public void set(int i, int x){ row = i; col = x; } }
DotJava Code:import javax.swing.*; import java.awt.*; public class Ghost extends Players { private Location loc; public int index; public Ghost() { loc = new Location(-1,-1); } public void setLocation(Location k) { loc.set(k.getRow(), k.getCol()); } public void setIndex(int i) { index = i; } public Location getLocation() { return loc; } public ImageIcon getImage() { return new ImageIcon("Ghost.png"); } }
I could also use tips on improving my already existing code. Thanks!Java Code:import javax.swing.*; import java.awt.*; public class Dot extends Players { private Location loc; public Dot(){ } public void setLocation(Location k) { loc = new Location(k); } public Location getLocation() { return loc; } public ImageIcon getImage() { return new ImageIcon("dot.gif"); } }
- 05-21-2011, 03:42 AM #2
You haven't set the KeyListener for the frame. See the addKeyListener method. There is no reason for the JVM to call the keyPressed method.
You need some work on redoing the layout after removing an image. See the validate method.
- 05-21-2011, 03:49 AM #3
I have two things to say.
First off I would recommend putting this instead.
Second the original PacMan did not have all the ghosts follow the player. Instead only the red one follows the player. The blue ghost tries to get to a point that's exactly thirty-two pixels in front of the player and the green ghost tries to go to a similar spot as the blue. The yellow ghost just bounces around randomly and never tries to go to a set point like the rest.Java Code:if(e,getKeyCode() == KeyEvent.VK_LEFT)
<Daniel>P.</Berry>
- 05-21-2011, 04:00 AM #4
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Wow thanks it's working now! ;)
- 05-24-2011, 10:28 PM #5
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
New problem: How can I make a timer so it will make a variable true until the timer goes on for a period of time, then the variable would be set false. I have tried the method I used for delaying the ghosts movement but it just pauses the entire game.
This is for making the ghosts eatable and blue, like in pacman.
Thanks
- 05-24-2011, 10:36 PM #6
Can you make a small simple program to demonstrate your problem using the Timer class.
Not sure how using a Timer would pause the entire game.
a guess would be: Is your problem with the scope of the boolean?
- 05-24-2011, 10:41 PM #7
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Well, I just said timer. I am really using this weird Thread.sleep() typed deal. I have no clue how to use a timer and google hasn't helped much.
:D
- 05-24-2011, 10:42 PM #8
Look in the Java tutorial for examples:
The Really Big Index
- 05-24-2011, 10:45 PM #9
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Thanks for the quick response.
So would this wait 7 seconds and then execute the code following it in the method?Java Code://Method called when supermode is set to true timer = new Timer(7000, this); timer.start(); supermode = false;
- 05-24-2011, 10:49 PM #10
No, the last statement would be executed immediately.
Go back and read the tutorial again. The timer calls an ActionListener when it expires.
- 06-02-2011, 10:16 AM #11
I never really liked using timers. You can also create your own Thread that runs parallel to the main thread, kinda like multi-tasking. But Thread.sleep(1000) is what i use since thats pretty much what the timer is doing really.
- 06-02-2011, 01:48 PM #12
For some, very limited, definitions of 'pretty much'.
A Swing Timer encapsulates more functionality, primarily in that its actionPerformed is invoked on the EDT, and that all Swing Timers use a common background Thread for their timing. Then there's the facility to setRepeats and setCoalesce -- the latter is particularly significant.
db
- 06-03-2011, 12:47 AM #13
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
Does anyone know a simple way to add sound effects. All the tutorials I have looked up are so complicated and don't seem to work for me. Isn't there a simple playsound(wav file) class?
- 06-03-2011, 08:02 AM #14
See the API for the static method in Applet that creates an AudioClip.
db
- 06-03-2011, 03:24 PM #15
Sounds
Heres a simple code that will play two types of sound files. .wav and .au
Java Code:try { String fileLocation = ""; File bounceFile = new File(fileLocation); URL bounceURL = bounceFile.toURI().toURL(); AudioClip clip = Applet.newAudioClip(bounceURL); clip.play(); }catch(Exception e) {}
Plug this into your code and place the file path in the fileLocation variable. Whenever the code runs it will play the audio file. Remember only .au and .wav can be used.
clip.play() can be replaced by clip.loop(). This will loop the sound file over and over until the clip.stop() method is run.<Daniel>P.</Berry>
Similar Threads
-
Making a game restart? :/
By AndroidAppNewbie in forum New To JavaReplies: 2Last Post: 02-19-2011, 10:11 AM -
Making a puzzle game, need help!
By Atriamax in forum Advanced JavaReplies: 1Last Post: 12-30-2010, 12:55 AM -
Game making help
By Mrkantaloupe in forum New To JavaReplies: 2Last Post: 07-13-2010, 10:15 PM -
Huge problem. PacMan game.
By Bullfrog in forum New To JavaReplies: 3Last Post: 06-01-2010, 02:39 PM -
need help with pacman game!!
By Newbie_Javaer in forum New To JavaReplies: 2Last Post: 10-22-2009, 02:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks