Wednesday, 15 September 2010

Java - movement and wall collision in a 2d array map -



Java - movement and wall collision in a 2d array map -

i creating pac man project, i've ran problem moving around character. implementing wall collision, can move character fine not when seek set in wall collision... doing wrong

when seek move around cant, detects arrow key inputs doesn't move character, because of believe if statement isn't correct, tried setting handle wall collision whilst going up. resulted in seemingly invisible walls or places couldn't move have looked @ awhile , have yet find solution.

current code bundle main; import java.awt.borderlayout; import java.awt.canvas; import java.awt.dimension; import java.awt.graphics; import java.awt.image; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.jframe; public class game extends canvas implements runnable { final static string nl = system.getproperty("line.separator"); public static int tile; public boolean running = false; public string name = "pacman"; public static final int width = 23;//578; public static final int height = 29;//720; public static final int scale = 25; private jframe frame; private image bimage; private graphics db; private input input; public static int playerposx = 0; public static int playerposy = 0; public static int playerheight = 16; public static int playerwidth = 16; public static int speed = 3; static bufferedimage background = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage pacman = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage settingsbackground = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage level1 = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage level2 = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage points = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage point = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage wall = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage blackback = new bufferedimage(width, height, bufferedimage.type_int_rgb);; static bufferedimage pointback = new bufferedimage(width, height, bufferedimage.type_int_rgb);; public static boolean key_down = false; public static boolean key_up = false; public static boolean key_right = false; public static boolean key_left = false; public boolean walldrawn = false; public boolean loaded = false; public static int tc = 0; public int tilex, tiley, ptilex, ptiley; public game() { setminimumsize(new dimension(width * scale, height * scale)); setmaximumsize(new dimension(width * scale, height * scale)); // keeps // // canvas // same // size setpreferredsize(new dimension(width * scale, height * scale)); frame = new jframe(name); // creates frame our game input = new input(); //if(main_menu == true && game == false){ // buttons = new buttons(frame.getcontentpane()); //draws buttons based on code in our graphics/buttons.java //} frame.setlayout(new borderlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); // ends programme on click of exit button in top right corner frame.setlocationbyplatform(true); frame.addkeylistener(new input() ); frame.add(this, borderlayout.center); frame.pack(); // keeps size right frame.setresizable(false); //keep false, if set true whenever resizes our code not function correctly frame.setvisible(true); this.addkeylistener(input); this.createbufferstrategy(2); //i think double buffering, not sure } public static void main(string[] args) { new game().start(); } public void resloader() { seek { background = imageio.read(new file("res\\background.png")); wall = imageio.read(new file("res\\maptile.png")); pacman = imageio.read(new file("res\\pacman.png")); settingsbackground = imageio.read(new file("res\\background.png")); level1 = imageio.read(new file("res\\level1.png")); point = imageio.read(new file("res\\points pacman.png")); blackback = imageio.read(new file("res\\blackback.png")); pointback = imageio.read(new file("res\\points.png")); } grab (ioexception e) { system.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ warning! ~~~~~~~~~~~~~~~~~~~~~~~~~~"); system.out.println("there image unsuccessfully loaded!" + nl + "the game may not work properly, check load images method spelling errors!"); system.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ warning! ~~~~~~~~~~~~~~~~~~~~~~~~~~" + nl + nl); } } public void run() { long lasttime = system.nanotime(); double nspertick = 1000000000 / 60d; long lasttimer = system.currenttimemillis(); double delta = 0; int frames = 0; int ticks = 0; while (running == true) { long = system.nanotime(); delta += (now - lasttime) / nspertick; lasttime = now; boolean render = false; while (delta >= 1) { ticks++; tick(); delta -= 1; render = true; } seek { thread.sleep(3); //keep frames going high } grab (interruptedexception e) { e.printstacktrace(); } if(render == true){ frames++; render(); } if (system.currenttimemillis() - lasttimer >= 1000) { lasttimer +=1000; //system.out.println("frames: " + frames + " ticks: " + ticks); frames = 0; ticks = 0; } } seek { thread.sleep(3); //keep frames going high } grab (interruptedexception e) { e.printstacktrace(); } } public synchronized void start() { resloader(); new thread(this).start(); running = true; } public synchronized void stop() { running = false; } public void render () { graphics g = getgraphics(); if (bimage == null) { bimage = createimage (this.getsize().width, this.getsize().height); db = bimage.getgraphics (); } db.setcolor (getbackground ()); db.fillrect (0, 0, this.getsize().width, this.getsize().height); paint (db); g.drawimage (bimage, 0, 0, this); } public void paint (graphics g) { (int x = 0; x < main.map.width; x++) { (int y = 0; y < main.map.height; y++) { tile = main.map.level1[y][x]; tilex = x * 21; tiley = y * 26; if(tile == 0){ g.drawimage(wall,tilex,tiley, 21, 26,null); } if(tile == 1){ g.drawimage(pointback,tilex, tiley, (int)21.42857142857143, 26,null); } if(tile == 2){ g.drawimage(blackback,tilex, tiley, (int)21.42857142857143, 26,null); } if(tile == 5) { if(!loaded) { ptilex = tilex; ptiley = tiley; loaded = true; } g.drawimage(blackback,tilex, tiley, (int)21.42857142857143, 26,null); } } } g.drawimage(pacman, ptilex, ptiley, playerwidth, playerheight, null); } public void tick() { playerposx = ptilex / 21; playerposy = ptiley / 26; int wallposx, wallposy; if(key_up && map.level1[playerposx][playerposy] == 1){ ptiley = ptiley - speed; } if(key_down && map.level1[playerposx][playerposy] == 1){ ptiley += speed; } if(key_left && map.level1[playerposx][playerposy] == 1){ ptilex -= speed; } if(key_right && map.level1[playerposx][playerposy] == 1){ ptilex += speed; } } } package main; import java.awt.event.*; public class input implements keylistener { public void keytyped(keyevent e) { } /** here have rather basic key listener * set can go left right or downwards right * if more directions needed delete "other key = false" */ public void keypressed(keyevent e) { system.out.println(keyevent.getkeytext(e.getkeycode())); if (e.getkeycode() == keyevent.vk_right) { game.key_right = true; game.key_left = false; game.key_up = false; game.key_down = false; } if (e.getkeycode() == keyevent.vk_left) { game.key_right = false; game.key_left = true; game.key_up = false; game.key_down = false; } if (e.getkeycode() == keyevent.vk_up) { game.key_right = false; game.key_left = false; game.key_up = true; game.key_down = false; } if (e.getkeycode() == keyevent.vk_down) { game.key_right = false; game.key_left = false; game.key_up = false; game.key_down = true; } } public void keyreleased(keyevent e) { system.out.println(e.getkeycode()); if (e.getkeycode() == keyevent.vk_right) { game.key_right = false; } if (e.getkeycode() == keyevent.vk_left) { game.key_left = false; } if (e.getkeycode() == keyevent.vk_up) { game.key_up = false; } if (e.getkeycode() == keyevent.vk_down) { game.key_down = false; } } } public class map { public static int width = 28; public static int height = 28; public static int[][] level1 = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,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,0}, {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0}, {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0}, {0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,0,2,2,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,1,1,0,0,2,2,2,2,0,0,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,2,2,2,2,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,1,1,0,0,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,0,0,1,1,1,0}, {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0}, {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0}, {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0}, {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,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,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; }

besides having silly error || instead of &&. hard identify problem without seeing much more of application. give few tips:

before adding collision detection, create sure motion working perfectly. when adding collision detection simplify testing bare minimum, illustration add together 1 wall. seek hardest break through wall, move @ every angle , watch unusual behavior such not beingness able move 1 time you've collided or moving through wall. you find problems when implementing collision detection if create character move collisions. should examine coordinates next 'step' , if have nail wall dont move. think it, if move character after programme collision detection , sees you've nail wall technically within wall! should calculate next coordinates character , if there no collision commit coordinates character.

make sure debug programme throughly, add together println statements: system.out.println("collision!") or debugger, whatever way best.

this should comment long...

edit: see question on codereview got closed, due fact said "my code isnt working". told inquire on there intent of having them doing here. improve , reorganize code, not prepare collision detection. did happen prepare commented @ comments in code!!! :)

here of problems found

your classes should follow java naming conventions, means camelcase captial letters start, game should game, input input ect... static variables should upper_snake_case, static variables declared @ top of class. the scope of variables on place, instance input class detects inputs changes variables in game class. 1 benefit of oop can break apart specific areas of application specific classes, dont want have god object have game class. becomes overwhelming , hard manage. maintain doing , pretty have unmanageable program. you have unusual variable naming or unusual usage of them. im looking @ ptilex, ptiley, playerposx, playerposy, tilex, tiley. why 6 variables? should need 2, x , y position of player. tile location @ time found using coordinates. your map class pretty much useless @ moment. have few static variables in there dont utilize oop construction it! have right thought making lets set class construction use. never declare global variable unless needs be, cut down scope of variable , increment necessary. when become more seasoned programmer have more knowledge scope should be.

im going give fixed version of code, farther moving methods , things around necessary future additions. removed alot of global variables silly have there.

game.class

import java.awt.borderlayout; import java.awt.canvas; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.image; import java.awt.point; import javax.swing.jframe; public class game extends canvas implements runnable { public static int playerheight = 16; public static int playerwidth = 16; public static int movementspeed = 3; public boolean running = false; private jframe frame; private image bufferimage; private graphics buffergraphics; private input input; private point playerlocation = null; private gamemap currentlevel; public game() { resources.loadresources(); loadlevel(new gamemap.level1()); frame = new jframe("pacman"); // creates frame our game input = new input(); // if(main_menu == true && game == false){ // buttons = new buttons(frame.getcontentpane()); //draws buttons // based on code in our graphics/buttons.java // } frame.setlayout(new borderlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); // ends programme on // click of // exit button // in top // right corner frame.setlocationbyplatform(true); frame.addkeylistener(new input()); frame.add(this, borderlayout.center); frame.pack(); // keeps size right frame.setresizable(false); // maintain false, if set true whenever // resizes our code not // function correctly frame.setvisible(true); this.addkeylistener(input); this.createbufferstrategy(2); // think double buffering, // not sure } /** * loads level game, changes dimensions of window fit game * @param gamemap */ private void loadlevel(gamemap gamemap) { currentlevel = gamemap; playerlocation = convertmaplocationtoscreenlocation(currentlevel.getplayerstartlocation()); dimension canvassize = new dimension(currentlevel.getwidth()*resources.tile_width, currentlevel.getheight()*resources.tile_height); setminimumsize(canvassize); setmaximumsize(canvassize); setpreferredsize(canvassize); } public void run() { long lasttime = system.nanotime(); double nspertick = 1000000000 / 60d; long lasttimer = system.currenttimemillis(); double delta = 0; int frames = 0; int ticks = 0; while (running == true) { long = system.nanotime(); delta += (now - lasttime) / nspertick; lasttime = now; boolean render = false; while (delta >= 1) { ticks++; tick(); delta -= 1; render = true; } seek { thread.sleep(20); // maintain frames going high } grab (interruptedexception e) { e.printstacktrace(); } if (render == true) { frames++; render(); } if (system.currenttimemillis() - lasttimer >= 1000) { lasttimer += 1000; frames = 0; ticks = 0; } } seek { thread.sleep(20); // maintain frames going high } grab (interruptedexception e) { e.printstacktrace(); } } public synchronized void start() { new thread(this).start(); running = true; } public synchronized void stop() { running = false; } public void render() { graphics g = getgraphics(); if (bufferimage == null) { bufferimage = createimage(this.getsize().width, this.getsize().height); buffergraphics = bufferimage.getgraphics(); } buffergraphics.setcolor(color.orange); buffergraphics.fillrect(0, 0, this.getsize().width, this.getsize().height); drawgame(buffergraphics); g.drawimage(bufferimage, 0, 0, this); } // had renamed drawgame, paint method used awt objects. caused serious bug repainting. public void drawgame(graphics g) { (int x = 0; x < currentlevel.getwidth(); x++) { (int y = 0; y < currentlevel.getheight(); y++) { int tile = currentlevel.gettileat(x, y); int tilex = x * resources.tile_width; int tiley = y * resources.tile_height; if (tile == gamemap.tile_wall) { g.drawimage(resources.wall, tilex, tiley, resources.tile_width, resources.tile_height, null); } if (tile == gamemap.tile_nothing) { g.drawimage(resources.black_back, tilex, tiley, resources.tile_width, resources.tile_height, null); } if (tile == gamemap.tile_point) { g.drawimage(resources.point, tilex, tiley, resources.tile_width, resources.tile_height, null); } /* not way find first location player, knowing location belongs map class if (tile == 5) { if (!loaded) { playerposx = tilex; playerposy = tiley; loaded = true; } g.drawimage(blackback, tilex, tiley, (int) 21.42857142857143, 26, null); }*/ } } g.drawimage(resources.pacman, playerlocation.x, playerlocation.y, playerwidth, playerheight, null); } public void tick() { int nextplayerposx = playerlocation.x; int nextplayerposy = playerlocation.y; if (input.key_up) { nextplayerposy -= movementspeed; } if (input.key_down) { nextplayerposy += movementspeed; } if (input.key_left) { nextplayerposx -= movementspeed; } if (input.key_right) { nextplayerposx += movementspeed; } // lets create sure next location doesnt collide wall, if dont move pacman! if(!doesplayercollidewith(nextplayerposx, nextplayerposy, gamemap.tile_wall)) { playerlocation.setlocation(nextplayerposx, nextplayerposy); } } /** * looks @ players screen location , gets map tiles each corner. * @param screenx * @param screeny * @return 4 map tiles each corner of pac man given screenx , screeny */ private int[] getplayercornercollisions(int screenx, int screeny) { int[] corners = new int[4]; point tilelocation = convertscreenlocationtomaplocation(screenx, screeny); corners[0] = currentlevel.gettileat(tilelocation.x, tilelocation.y); tilelocation = convertscreenlocationtomaplocation(screenx + playerwidth, screeny); corners[1] = currentlevel.gettileat(tilelocation.x, tilelocation.y); tilelocation = convertscreenlocationtomaplocation(screenx, screeny + playerheight); corners[2] = currentlevel.gettileat(tilelocation.x, tilelocation.y); tilelocation = convertscreenlocationtomaplocation(screenx + playerwidth, screeny + playerheight); corners[3] = currentlevel.gettileat(tilelocation.x, tilelocation.y); homecoming corners; } /** * checks if corners of player intersects given maptiletype * @param screenx * @param screeny * @param maptiletype * @return true if player intersects given map tile type */ public boolean doesplayercollidewith(int screenx, int screeny, int maptiletype) { for(int tiletype : getplayercornercollisions(screenx, screeny)) { if(tiletype == maptiletype) { homecoming true; } } homecoming false; } /** * takes screen location , converts coordinate in map * @param location * @return */ public point convertscreenlocationtomaplocation(point location) { homecoming convertscreenlocationtomaplocation(location.x, location.y); } public point convertscreenlocationtomaplocation(int x, int y) { homecoming new point(x/resources.tile_width, y/resources.tile_height); } public point convertmaplocationtoscreenlocation(point location) { homecoming convertmaplocationtoscreenlocation(location.x, location.y); } public point convertmaplocationtoscreenlocation(int x, int y) { homecoming new point(x*resources.tile_width, y*resources.tile_height); } public static void main(string[] args) { new game().start(); } }

gamemap.class - map.class

import java.awt.point; public abstract class gamemap { public static final int tile_wall = 0; public static final int tile_nothing = 1; public static final int tile_point = 2; public static final int tile_start_location = 5; public abstract int getwidth(); public abstract int getheight(); public abstract int[][] getleveldata(); public abstract java.awt.point getplayerstartlocation(); public int gettileat(int x, int y) { homecoming getleveldata()[y][x]; } public static class level1 extends gamemap { @override public int getwidth() { homecoming level_1_data[0].length; } @override public int getheight() { homecoming level_1_data.length; } @override public int[][] getleveldata() { homecoming level_1_data; } @override public point getplayerstartlocation() { for(int y=0;y<level_1_data.length;y++) { for(int x=0;x<level_1_data[y].length;x++) { if(level_1_data[y][x] == gamemap.tile_start_location) { homecoming new point(x, y); } } } // should never reach unless forgot set start location. throw new runtimeexception("no player start location found!"); } } private static int[][] level_1_data = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,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,0}, {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0}, {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0}, {0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,0,2,2,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,1,1,1,0,0,2,2,2,2,0,0,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,2,2,2,2,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,1,1,0,0,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,0,0,1,1,1,0}, {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0}, {0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0}, {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0}, {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,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,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} }; }

input.class

import java.awt.event.*; public class input implements keylistener { public boolean key_down = false; public boolean key_up = false; public boolean key_right = false; public boolean key_left = false; public void keytyped(keyevent e) { } /** here have rather basic key listener * set can go left right or downwards right * if more directions needed delete "other key = false" */ public void keypressed(keyevent e) { if (e.getkeycode() == keyevent.vk_right) { key_right = true; key_left = false; key_up = false; key_down = false; } if (e.getkeycode() == keyevent.vk_left) { key_right = false; key_left = true; key_up = false; key_down = false; } if (e.getkeycode() == keyevent.vk_up) { key_right = false; key_left = false; key_up = true; key_down = false; } if (e.getkeycode() == keyevent.vk_down) { key_right = false; key_left = false; key_up = false; key_down = true; } } public void keyreleased(keyevent e) { if (e.getkeycode() == keyevent.vk_right) { key_right = false; } if (e.getkeycode() == keyevent.vk_left) { key_left = false; } if (e.getkeycode() == keyevent.vk_up) { key_up = false; } if (e.getkeycode() == keyevent.vk_down) { key_down = false; } } }

resource.class

import java.awt.color; import java.awt.graphics; import java.awt.image; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; public final class resources { private final static string nl = system.getproperty("line.separator"); public static final int tile_width = 23;// 578; public static final int tile_height = 29;// 720; public static bufferedimage background = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static bufferedimage pacman = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static bufferedimage settings_background = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static bufferedimage level1 = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); /*public static bufferedimage level2 = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb);*/ public static bufferedimage points = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static bufferedimage point = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static bufferedimage wall = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static bufferedimage black_back = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static bufferedimage point_back = new bufferedimage(tile_width, tile_height, bufferedimage.type_int_rgb); public static void loadresources() { seek { background = imageio.read(new file("res\\background.png")); wall = imageio.read(new file("res\\maptile.png")); pacman = imageio.read(new file("res\\pacman.png")); settings_background = imageio.read(new file("res\\background.png")); level1 = imageio.read(new file("res\\level1.png")); point = imageio.read(new file("res\\points pacman.png")); black_back = imageio.read(new file("res\\blackback.png")); point_back = imageio.read(new file("res\\points.png")); } grab (ioexception e) { system.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ warning! ~~~~~~~~~~~~~~~~~~~~~~~~~~"); system.out.println("there image unsuccessfully loaded!" + nl + "the game may not work properly, check load images method spelling errors!"); system.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ warning! ~~~~~~~~~~~~~~~~~~~~~~~~~~" + nl + nl); } } }

java multidimensional-array collision pacman

No comments:

Post a Comment