swing - erasing dots from map pacman java -
i'm coding pacman, have pacman move, loading map txt, , collision walls. next step dot eraser when pacman on dot field. need suggestion how it, i've tried erase paiting dot @ same color background, doesnt work. here code
board.java
public class board extends jpanel implements actionlistener { private timer timer; private map m; private player p; public board(){ m = new map(); p = new player(); addkeylistener(new klawa()); setbackground(color.black); setfocusable(true); timer = new timer(25, this); timer.start(); } @override public void paint(graphics g){ super.paint(g); for(int x=0; x<14; x++){ for(int y=0; y<14; y++){ if(m.getmap(x,y).equals("s")){ g.drawimage(m.getwall(),x *32,y *32, this); } if(m.getmap(x,y).equals("k")){ g.drawimage(m.getdot(),x *32,y *32, this); } } } g.drawimage(p.getplayer(),p.gettilex()* 32 ,p.gettiley() * 32,null); } public void erase(graphics g){ super.paint(g); if(m.getmap(p.gettilex(),p.gettiley()).equals("k")){ g.drawimage(m.geterase(),p.gettilex(),p.gettiley(), this); } } public class klawa extends keyadapter { @override public void keypressed(keyevent e){ int keycode = e.getkeycode(); if (keycode == keyevent.vk_w){ if(!m.getmap(p.gettilex(),p.gettiley() - 1).equals("s")){ p.move(0, -1); } } if (keycode == keyevent.vk_s){ if(!m.getmap(p.gettilex(),p.gettiley() + 1).equals("s")){ p.move(0, 1); } } if (keycode == keyevent.vk_a){ if(!m.getmap(p.gettilex() - 1,p.gettiley()).equals("s")){ p.move(-1, 0); } } if (keycode == keyevent.vk_d){ if(!m.getmap(p.gettilex() + 1,p.gettiley() ).equals("s")){ p.move(1, 0); } } } public void keyrelased(keyevent e){ } @override public void keytyped(keyevent e){ } } @override public void actionperformed(actionevent e) { repaint(); } }
map.java
public class map { private scanner m; private string map[] = new string[16]; private image wall, dot, erase; public map(){ wall = new imageicon("c:\\users\\kotek\\desktop\\pacman\\sciana.png").getimage(); dot = new imageicon("c:\\users\\kotek\\desktop\\pacman\\kropka.png").getimage(); erase = new imageicon("c:\\users\\kotek\\desktop\\pacman\\wymaz.png").getimage(); openfile(); readfile(); closefile(); } public image getwall(){ return wall; } public image getdot(){ return dot; } public image geterase(){ return erase; } public string getmap(int x, int y){ string index = map[y].substring(x, x+1); return index; } public void openfile(){ try{ m = new scanner(new file("c:\\users\\kotek\\desktop\\pacman\\mapa.txt")); }catch(filenotfoundexception e){ system.out.println("blad podczas ladowania mapy"); } } public void readfile(){ while(m.hasnext()){ for(int i=0; i<14; i++){ map[i] = m.next(); } } } public void closefile(){ m.close(); } }
in class map should implement method
public void setmap(int x, int y, string value ){ //remove "k" string }
in erase() method clean model's cell , call repaint()
Comments
Post a Comment