import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Random; import java.io.*; import java.awt.Color; import java.awt.event.ActionEvent; public class Snakes extends JPanel implements ActionListener,KeyListener{ Timer tmr = new Timer(10, this); Timer tm = new Timer(10,this); static JFrame frame = new JFrame(); static JButton btnStart; static JLabel ScoreLabel; static int x1=20, y1=20, w1=20, h1=20; static int x2=20, y2=20, w2=20, h2=20; static int RandomBox = 0; static int score = 0; static int direction = 0; Random randomGenerator = new Random(); public Snakes() { ScoreLabel = new JLabel ("Score:"); ScoreLabel.setBounds(5,445,100,20); add(ScoreLabel); tmr.start(); setLayout(null); this.requestFocus(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public int x2Generator(int x2,Random randomGenerator,int RandomBox) { RandomBox = randomGenerator.nextInt(5)+1; if(RandomBox==1) { x2 = 340; } else if(RandomBox==2) { x2 = 135; } else if(RandomBox==3) { x2 = 405; } else if(RandomBox==4) { x2 = 420; } else if(RandomBox==5) { x2 = 375; } return x2; } public int y2Generator(int y2,Random randomGenerator,int RandomBox) { RandomBox = randomGenerator.nextInt(5)+1; if(RandomBox==1) { y2 = 95; } else if(RandomBox==2) { y2 = 400; } else if(RandomBox==3) { y2 = 425; } else if(RandomBox==4) { y2 = 170; } else if(RandomBox==5) { y2 = 440; } return y2; } public void paintComponent(Graphics g) { super.paintComponent(g); tm.start(); g.setColor(Color.BLACK); g.fillRect(x1, y1, w1, h1); g.setColor(Color.BLUE); g.fillRect(x2, y2, w2, h2); } public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_RIGHT) { direction=1; } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { direction=2; } else if(e.getKeyCode()==KeyEvent.VK_UP) { direction=3; } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { direction=4; } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void delay() { try { Thread.sleep(100); } catch(Exception e) { } } public static void main (String args[]) { Snakes obj = new Snakes(); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.add(obj); } public void actionPerformed(ActionEvent e) { //Reset position of Snake in x and y through frame if(x1<0) { x1=490; } if(x1>490) { x1=0; } if(y1<0) { y1=460; } if(y1>460) { y1=0; } //Snake Arrow up, down, left, right if(direction==1) { x1=x1+1; } if(direction==2) { x1=x1-1; } if(direction==3) { y1=y1-1; } if(direction==4) { y1=y1+1; } //Snake and Random Box if((x1==x2)&&(y1==y2)) { x2 = x2Generator(x2, randomGenerator, RandomBox); y2 = y2Generator(y2, randomGenerator, RandomBox); score = score +1; ScoreLabel.setText("Score: "+score); } repaint(); } }
Give me a hint or please just post a code how to add tail
and please fix the score for me
----------------------------------------------------------------------
After some hours my snake has a tail. I still have a problem the whole Body/Tail including the head, it can go Up, Down, Left, Right, now my problem
is, it moves in one direction. Here's my new code.
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.Random; import java.io.*; import java.awt.Color; import java.awt.event.ActionEvent; public class Snakes extends JPanel implements ActionListener, KeyListener{ Timer tmr = new Timer(500, this); Timer tm = new Timer(10,this); static JFrame frame = new JFrame(); static JButton btnStart; static JLabel ScoreLabel; static int x1=20, y1=20, w1=20, h1=20; static int x2=20, y2=20, w2=20, h2=20; static int RandomBox = 0; static int score = 0; static int direction = 0; Random randomGenerator = new Random(); public Snakes() { JOptionPane.showMessageDialog(null, "Start"); ScoreLabel = new JLabel ("Score:"); ScoreLabel.setBounds(5,445,100,20); add(ScoreLabel); setLayout(null); this.requestFocus(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public int x2Generator(int x2,Random randomGenerator,int RandomBox) { RandomBox = randomGenerator.nextInt(5)+1; if(RandomBox==1) { x2 = 340; } else if(RandomBox==2) { x2 = 135; } else if(RandomBox==3) { x2 = 405; } else if(RandomBox==4) { x2 = 420; } else if(RandomBox==5) { x2 = 375; } return x2; } public int y2Generator(int y2,Random randomGenerator,int RandomBox) { RandomBox = randomGenerator.nextInt(5)+1; if(RandomBox==1) { y2 = 95; } else if(RandomBox==2) { y2 = 400; } else if(RandomBox==3) { y2 = 425; } else if(RandomBox==4) { y2 = 170; } else if(RandomBox==5) { y2 = 440; } return y2; } public void paintComponent(Graphics g) { super.paintComponent(g); tm.start(); g.setColor(Color.BLACK); g.fillOval(x1, y1, w1, h1); g.setColor(Color.RED); g.fillRect(x2, y2, w2, h2); } public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_RIGHT) { direction=1; } else if(e.getKeyCode()==KeyEvent.VK_LEFT) { direction=2; } else if(e.getKeyCode()==KeyEvent.VK_UP) { direction=3; } else if(e.getKeyCode()==KeyEvent.VK_DOWN) { direction=4; } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void delay() { try { Thread.sleep(5); } catch(Exception e) { } } public static void main (String args[]) { Snakes obj = new Snakes(); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.add(obj); } public void actionPerformed(ActionEvent e) { //Reset position of Snake in x and y through frame if(x1<0) { x1=490; } if(x1>490) { x1=0; } if(y1<0) { y1=460; } if(y1>460) { y1=0; } //Up, down, left, right if(direction==1) { x1=x1+1; delay(); } if(direction==2) { x1=x1-1; delay(); } if(direction==3) { y1=y1-1; delay(); } if(direction==4) { y1=y1+1; delay(); } //Snake tail and Random Box if(y2+h2>=y1&&x2+w2>=x1&&x2<=x1+w1&&y2<=y1||y2<=0) { x2 = x2Generator(x2, randomGenerator, RandomBox); y2 = y2Generator(y2, randomGenerator, RandomBox); w1=w1+20; ScoreLabel.setText("Score: "+score); score = score +1; } repaint(); } }
PS: Please give me a hint or fix the code for me