java - Snake Game issues, snake does not move -
i trying recreate simple snake game programming class. using java on program eclipse. in case didn't know how work game, game when snake eats dot, grows , game on when snake hits itself. appreciated!
import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.point; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.image.bufferedimage; import java.util.linkedlist; import java.util.random; import javax.swing.jframe; public class source extends jframe implements runnable, keylistener { private final int boxheight = 15; //each individual box height private final int boxwidth = 15; //each individual box width private final int gridwidth = 25; //total width of boxes in grid private final int gridheight = 25; //total height of boxes in grid jframe frame = new jframe(); private linkedlist<point> snake; public point fruit; public int direction = direction.nodirection; private thread runthread; private graphics globalgraphics; private int score = 0; public void paint (graphics g) { setbounds(0,0,500,500); snake = new linkedlist<point>(); generatedefaultsnake(); placefruit(); globalgraphics = g.create(); this.addkeylistener(this); if (runthread == null){ runthread = new thread(this); runthread.start(); } } public void generatedefaultsnake(){ score = 0; snake.clear(); snake.add(new point (0,2)); snake.add(new point (0,1)); snake.add(new point (0,0)); direction = direction.nodirection; } public void draw (graphics g){ //main method of drawn g.clearrect(0, 0, boxwidth * gridwidth + 10, boxheight * gridheight +20); //create new image bufferedimage buffer = new bufferedimage(boxwidth * gridwidth + 10, boxheight * gridheight +20, bufferedimage.type_int_argb); graphics buffergraphics = buffer.getgraphics(); drawfruit(buffergraphics); drawgrid(buffergraphics); drawsnake(buffergraphics); drawscore(buffergraphics); //flip g.drawimage(buffer, 0,0, boxwidth * gridwidth +10, boxheight * gridheight +20, this); } public void move(){ //directions point head = snake.peekfirst(); //head of snake, allows have body follow in chronological order point newpoint = head; snake.remove(snake.peeklast()); //removes end of tail if(newpoint.equals(fruit)) { score += 10; point addpoint = (point) newpoint.clone(); //the snake has hit fruit switch(direction){ case direction.north: newpoint = new point (head.x, head.y -1); break; case direction.south: newpoint = new point(head.x,head.y +1); break; case direction.west: newpoint = new point(head.x -1,head.y); break; case direction.east: newpoint = new point(head.x + 1,head.y); break; } snake.push(addpoint); placefruit(); } else if (newpoint.x < 0 || newpoint.x > (gridwidth - 1)){ //we went out of bounds, reset game generatedefaultsnake(); return; } else if (newpoint.y < 0 || newpoint.y > (gridheight - 1 )){ //we went out of bounds, reset game generatedefaultsnake(); return; } else if (snake.contains(newpoint)){ //we ran ourselves, reset game generatedefaultsnake(); return; } //if reach point of game, still snake.push(newpoint); //pushes points 1 point ahead when eat fruit , adds fruit ate @ end } public void drawscore(graphics g){ g.drawstring("score: " + score,0, boxheight * gridheight +10); } public void drawgrid (graphics g){ //drawing outer rectangle g.drawrect(0,0, gridwidth * boxwidth, gridheight * boxheight); //creates outer rectangle //drawing vertical lines of grid (int x = boxwidth; x < gridwidth * boxwidth; x += boxwidth){ g.drawline(x, 0, x, boxheight * gridheight); } //drawing horizontal lines of grid for(int y = boxheight; y < gridheight * boxheight; y += boxheight){ g.drawline(0, y, gridwidth * boxwidth, y); } } public void drawsnake(graphics g){ g.setcolor(color.green); ( point p : snake){ g.fillrect(p.x * boxwidth, p.y * boxheight, boxwidth, boxheight); } g.setcolor(color.black); } public void drawfruit(graphics g){ g.setcolor(color.red); g.filloval(fruit.x * boxwidth, fruit.y * boxheight, boxwidth, boxheight); g.setcolor(color.black); } public void placefruit() { random rand = new random(); int randomx = rand.nextint(gridwidth); int randomy = rand.nextint(gridheight); point randompoint = new point(randomx, randomy); while (snake.contains(randompoint)){ randomx = rand.nextint(gridwidth); randomy = rand.nextint(gridheight); randompoint = new point(randomx, randomy); } fruit = randompoint; } public void run() { while(true){ //runs indefinitely, every second objects in loop move move(); draw(globalgraphics); try{ thread.currentthread(); thread.sleep(100); //game updating every tenth of second (.1 of second) } catch (exception e){ e.printstacktrace(); } } } @override public void keypressed(keyevent e) { switch (e.getkeycode()) { case keyevent.vk_up: if(direction != direction.south) direction = direction.north; break; case keyevent.vk_down: if(direction != direction.north) direction = direction.south; break; case keyevent.vk_right: if(direction != direction.west) direction = direction.east; break; case keyevent.vk_left: if(direction != direction.east) direction = direction.west; break; } } public class direction { public static final int nodirection = 0; public static final int north = 1; public static final int south = 2; public static final int west = 3; public static final int east = 4; } public class snake extends jframe{ c = new source(); c.setpreferredsize(new dimension (640,480)); c.setvisible(true); c.setfocusable(true); } @override public void keyreleased(keyevent arg0) { // todo auto-generated method stub } @override public void keytyped(keyevent arg0) { // todo auto-generated method stub } }*
the issue in move() method, fail in last case(else if (snake.contains(newpoint))), because new point you've created inside of current snake.
i'm working on solution you.
edit:
this section wrong:
if(newpoint.equals(fruit)) { score += 10; point addpoint = (point) newpoint.clone(); //the snake has hit fruit switch(direction){ case direction.north: newpoint = new point (head.x, head.y -1); break; case direction.south: newpoint = new point(head.x,head.y +1); break; case direction.west: newpoint = new point(head.x -1,head.y); break; case direction.east: newpoint = new point(head.x + 1,head.y); break; } snake.push(addpoint); placefruit(); } edit 2: brainless box right points , addpoint should in check hitting fruit. added logic handle fruit bit better.
public void move(){ //directions point head = snake.peekfirst(); //head of snake, allows have body follow in chronological order point newpoint = head; snake.remove(snake.peeklast()); //removes end of tail point addpoint = (point) newpoint.clone(); switch(direction) { case direction.north: newpoint = new point (head.x, head.y -1); break; case direction.south: newpoint = new point(head.x,head.y +1); break; case direction.west: newpoint = new point(head.x -1,head.y); break; case direction.east: newpoint = new point(head.x + 1,head.y); break; } //the snake has hit fruit if(newpoint.equals(fruit)) { score += 10; fruit = null; snake.push(addpoint); } else if (newpoint.x < 0 || newpoint.x > (gridwidth - 1)){ //we went out of bounds, reset game generatedefaultsnake(); return; } else if (newpoint.y < 0 || newpoint.y > (gridheight - 1 )){ //we went out of bounds, reset game generatedefaultsnake(); return; } else if (snake.contains(newpoint)){ //we ran ourselves, reset game generatedefaultsnake(); return; } //if reach point of game, still placefruit(); snake.push(newpoint); //pushes points 1 point ahead when eat fruit , adds fruit ate @ end } in addition that, add if (fruit != null) return; first line of placefruit() method.
Comments
Post a Comment