I've been making a snake game, and everything works, tho the algorythm of the snake movement isnt that good, but its fine.
Now, I also have an apple on the screen, and the problem is i can't figure out a way to find if the snakes head & the apple have even 1 common point.
this is the code of the screen & apple:
Screen:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Snake; import Tools.ThreadTool; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.*; import javax.swing.*; /** * * @author User */ public class Screen extends JFrame implements Runnable,KeyListener{ private int difficulty; private int direction; //0-up 1- down 2-left 3- right private Thread t; private ArrayList<Integer> snakeX,snakeY; private panel panel; private int w,h; private Apple apple; public Screen(int difficulty,int sW,int sH){ super("Snake"); this.difficulty = difficulty; this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(sW, sH); this.setLocationRelativeTo(null); snakeX = new ArrayList<Integer>(); snakeY = new ArrayList<Integer>(); //set width and height of snake w = sW/40; h=w; //add head snakeX.add(this.getWidth()/2); snakeY.add(this.getHeight()/2); direction = (int)(Math.random()*4); apple = new Apple(this, w, h); apple.generateApple(); panel = new panel(); panel.setBackground(new Color(0, 153, 51)); //dark green add(panel); this.addKeyListener(this); } @Override public void run() { while(isAlive()){ // change to alive for(int i=snakeX.size();i>1 && snakeX.size() == snakeY.size();i--){ snakeX.set(i-1, snakeX.get(i-2)); snakeY.set(i-1, snakeY.get(i-2)); } if(direction==0){ snakeY.set(0, snakeY.get(0)-h); if(/*snakeX.get(0) == apple.getX() && */snakeY.get(0) == apple.getY()) { snakeX.add(snakeX.get(snakeX.size()-1)); snakeY.add(snakeY.get(snakeY.size()-1)+h); } } else if(direction==1) snakeY.set(0, snakeY.get(0)+h); else if(direction==2) snakeX.set(0, snakeX.get(0)-w); else if(direction==3) snakeX.set(0, snakeX.get(0)+w); repaint(); ThreadTool.sleep(t, 300/(difficulty+1)); } } public void startThread(){ t = new Thread(this); t.start(); } private boolean isAlive(){ return snakeX.get(0)>=0 && snakeX.get(0)+w<=this.getWidth() && snakeY.get(0)>=0 && snakeY.get(0)+h<=this.getHeight(); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_RIGHT && direction != 2) direction=3; else if(e.getKeyCode()==KeyEvent.VK_LEFT && direction !=3) direction = 2; else if(e.getKeyCode()==KeyEvent.VK_UP && direction!=1) direction=0; else if(e.getKeyCode()==KeyEvent.VK_DOWN && direction!=0) direction = 1; } @Override public void keyReleased(KeyEvent e) { } class panel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); apple.drawMe(g); for(int i=0;i<snakeX.size() && snakeX.size() == snakeY.size();i++){ g.setColor(Color.green); g.fillRect(snakeX.get(i), snakeY.get(i), w, h); g.setColor(Color.black); g.drawRect(snakeX.get(i), snakeY.get(i), w, h); System.err.println("painting at " + snakeX.get(i) + ", " + snakeY.get(i)+","+i); } } } }
Apple:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Snake; import java.awt.Color; import java.awt.Graphics; /** * * @author User */ public class Apple { private Screen screen; private int x,y; private int w,h; public Apple(Screen screen,int w,int h){ this.screen = screen; this.w = w; this.h = h; } public void generateApple(){ x = (int)(Math.random()*( (screen.getWidth() - w) - 0 + 1) + 0); y=(int)(Math.random()*( ( screen.getHeight() - h) - 0 + 1) + 0); } public void drawMe(Graphics g){ g.setColor(Color.red); g.fillRect(x, y, w, h); g.setColor(Color.black); g.drawRect(x, y, w, h); } public int getX(){ return x; } public int getY(){ return y; } }