I am here trying to make a Snake-Food game. In this game Snake tries to eat as much food as he can. It is just a prototype. At the onset I added a Rectangle (emulating Snake) into the JFrame, it is functioning properly, i.e. when I press Up, Down, etc buttons. the rectangle moves to that direction. Then I tried adding a small Rectangle(emulating Food) into the JFrame but it was visible nowhere. I also tried using Box layout to satisfy my need but it bifurcates whole JFrame to give half to Snake and half to Food, having no relation at all. Please suggest what can I do to avoid this mess and how to add relation between Snake and Food. Snake must eat the food, but in my program as Snake reaches near the food, it seems as if it's going behind any surface and disappears inches away from food.
Main Snake Frame where I am trying to add those two Rectangles
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class SnakeFrame extends JFrame implements Runnable { Snake snk ; Increase i ; Decrease d ; int width, height ; boolean foodPresent ; Random rand ; Food food ; SnakeFrame() { setSize(500,500) ; setTitle("Snake") ; snk = new Snake(0,0) ; this.add(snk) ; foodPresent = false ; rand = new Random() ; width = this.getSize().width; height = this.getSize().height ; this.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode() ; if(keyCode == KeyEvent.VK_LEFT) dec(snk,"Left") ; if(keyCode == KeyEvent.VK_RIGHT) inc(snk,"Right") ; if(keyCode == KeyEvent.VK_DOWN) inc(snk,"Down") ; if(keyCode == KeyEvent.VK_UP) dec(snk,"Up") ; } }) ; Thread t = new Thread(this) ; t.start() ; } public void interrupt() { if(i != null) i.stop() ; if(d != null) d.stop() ; } public void inc(Snake x, String direction) { interrupt() ; i = new Increase(x,direction, width, height) ; i.start() ; } public void dec(Snake x, String direction) { interrupt() ; d = new Decrease(x,direction, width, height) ; d.start() ; } public void run() { int height = this.getSize().height ; inc(snk,"Right"); while(true) { if(foodPresent == false) { int x = rand.nextInt(width) ; int y = rand.nextInt(height) ; food = new Food(x,y) ; food.setSize(25,25) ; this.add(food) ; food.repaint() ; foodPresent = true ; } int gx = snk.x ; int gy = snk.y ; if(gx == food.x && gy == food.y) this.remove(food) ; snk.repaint() ; try { Thread.sleep(5) ; } catch(InterruptedException e) {} validate() ; } } public static void main(String[] args) { new SnakeFrame().setVisible(true) ; } }
Snake Program
import java.awt.*; import javax.swing.*; public class Snake extends JPanel { int x, y ; Snake(int x, int y) { this.x = x ; this.y = y ; } public void paintComponent(Graphics g) { super.paintComponent(g) ; g.fillRect(x,y,50,50); } }
Food Program
import java.awt.*; import javax.swing.*; public class Food extends JPanel { int x, y ; Food(int x, int y) { this.x = x ; this.y = y ; } public void paintComponent(Graphics g) { super.paintComponent(g) ; g.fillRect(x,y,25,25); } }
Increase Coordinates(Thread)
import java.awt.*; import javax.swing.*; public class Increase extends Thread { Snake t ; String direction ; int width, height ; Increase(Snake t, String direction, int width, int height) { this.t = t ; this.direction = direction ; this.width = width ; this.height = height ; } public void run() { while(true) { if(direction.equals("Right")) { if(t.x >=width-50) t.x = -20 ; t.x += 1 ; } else if(direction.equals("Down")) { if(t.y >= height-50) t.y = -20 ; t.y += 1; } try { Thread.sleep(5) ; } catch(InterruptedException e) {} } } }
Decrease Coordinates(Thread)
public class Decrease extends Thread { Snake t ; String direction ; int width, height ; Decrease(Snake t, String direction, int width, int height) { this.t = t ; this.direction = direction ; this.width = width ; this.height = height ; } public void run() { while(true) { if(direction.equals("Up")) { if(t.y <= 0) t.y = height ; t.y -= 1 ; } else if(direction.equals("Left")) { if(t.x <= 0) t.x = width; t.x -= 1; } try { Thread.sleep(5) ; } catch(InterruptedException e) {} } } }