Results 1 to 8 of 8
Thread: snake not biting itself
- 08-24-2013, 03:14 PM #1
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
snake not biting itself
So a couple of days ago i figured i should try making snake (genius) with my noooby programming skills.
After posting here 2 times and reading about 10 tutorials i am here with my last (?) question.
How would you detect if the snake is "biting" itself, i tried 2 things (which both failed totally)the latest one waschecking if there was any identical coordinates to the head.
Java Code:package xyexs.snake.src; import java.awt.BorderLayout; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class Main { // Variables static int direction = 0; JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION); static Panel panel = new Panel(); static Score scoreboard = new Score(); static int score = 0; public static ArrayList<Integer> chainX = new ArrayList<Integer>(); public static ArrayList<Integer> chainY = new ArrayList<Integer>(); static Boolean doGrow = false; // Main public static void main(String[] args) { chainX.add(0, 500); chainY.add(0, 350); chainX.add(0, 500); chainY.add(0, 300); chainX.add(0, 500); chainY.add(0, 250); JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setResizable(false); window.add(panel, BorderLayout.CENTER); window.add(scoreboard, BorderLayout.SOUTH); window.setSize(Reference.WINDOW_X, Reference.WINDOW_Y); window.pack(); window.setVisible(true); int growCounter = 0; int moveCounter = 0; while (true) { try { Thread.sleep(Reference.TICK_TIME); } catch (InterruptedException e) { System.err.println("Sleep interrupted ( " + e + " )"); } ++growCounter; ++moveCounter; if (growCounter == Reference.GROW_TICK) { growCounter = 0; doGrow = true; } if (moveCounter == Reference.MOVE_TICK) { Boolean isByTop = (chainY.get(0) == 0); Boolean isByBot = (chainY.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_Y); Boolean isByLeft = (chainX.get(0) == 0); Boolean isByRight = (chainX.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_X); Boolean facingSnake = false; for (int i = 1; i < chainX.size() ; i++) { if (chainX.get(0) == chainX.get(i) && chainY.get(0)== chainY.get(i)) { facingSnake = true; System.out.println("Dont bite yourself! Breaking"); break; } } if (facingSnake == true) { break; } if (isByTop == true && direction == 0) { break; } else if (isByBot == true && direction == 2) { break; } else if (isByRight == true && direction == 1) { break; } else if (isByLeft == true && direction == 3) { break; } else { if (direction == 0) { chainX.add(0, chainX.get(0)); chainY.add(0, chainY.get(0) - Reference.PIXEL_SIZE); } else if (direction == 1) { chainX.add(0, chainX.get(0) + Reference.PIXEL_SIZE); chainY.add(0, chainY.get(0)); } else if (direction == 2) { chainX.add(0, chainX.get(0)); chainY.add(0, chainY.get(0) + Reference.PIXEL_SIZE); } else if (direction == 3) { chainX.add(0, chainX.get(0) - Reference.PIXEL_SIZE); chainY.add(0, chainY.get(0)); } if (doGrow == false) { chainY.remove(chainX.size() - 1); chainX.remove(chainX.size() - 1); } else { doGrow = false; } panel.repaint(); } moveCounter = 0; } score = score + Reference.SCORE_INCREMENT; scoreboard.setText("Score: " + score); } ImageIcon image = new ImageIcon("\\C:\\Java\\Snake\\resources\\Lost.png"); JLabel imageLabel = new JLabel(); imageLabel.setIcon(image); imageLabel.setVisible(true); window.remove(panel); window.add(imageLabel, BorderLayout.CENTER); window.pack(); } // Public Functions public static int getDir() { return direction; } public static void setDir(int dir) { direction = dir; } }
EDIT: never works..Last edited by Xyexs; 08-24-2013 at 06:20 PM.
- 08-24-2013, 05:08 PM #2
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
Re: snake not biting itself
bump
- 08-24-2013, 06:47 PM #3
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
Re: snake not biting itself
EDITED OUT:
i thought i solved it but i didn't.
It works sometimes, better than nothing...
latest attempt:
Java Code:package xyexs.snake.src; import java.awt.BorderLayout; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class Main { // Variables static int direction = 0; JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION); static Panel panel = new Panel(); static Score scoreboard = new Score(); static int score = 0; public static ArrayList<Integer> chainX = new ArrayList<Integer>(); public static ArrayList<Integer> chainY = new ArrayList<Integer>(); static Boolean doGrow = false; // Main public static void main(String[] args) { chainX.add(0, 500); chainY.add(0, 400); chainX.add(0, 500); chainY.add(0, 350); chainX.add(0, 500); chainY.add(0, 300); chainX.add(0, 500); chainY.add(0, 250); JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setResizable(false); window.add(panel, BorderLayout.CENTER); window.add(scoreboard, BorderLayout.SOUTH); window.setSize(Reference.WINDOW_X, Reference.WINDOW_Y); window.pack(); window.setVisible(true); int growCounter = 0; int moveCounter = 0; while (true) { try { Thread.sleep(Reference.TICK_TIME); } catch (InterruptedException e) { System.err.println("Sleep interrupted ( " + e + " )"); } ++growCounter; ++moveCounter; if (growCounter == Reference.GROW_TICK) { growCounter = 0; doGrow = true; } if (moveCounter == Reference.MOVE_TICK) { Boolean isByTop = (chainY.get(0) == 0); Boolean isByBot = (chainY.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_Y); Boolean isByLeft = (chainX.get(0) == 0); Boolean isByRight = (chainX.get(0) + Reference.PIXEL_SIZE == Reference.WINDOW_X); Boolean facingSnake = false; for (int i = 1; i < chainX.size(); i++) { if (direction == 0) { if (chainX.get(0) == chainX.get(i) && chainY.get(0) - Reference.PIXEL_SIZE == chainY.get(i)) { facingSnake = true; System.out.println("OUCH! You bit me! (facing north)"); break; } } else if (direction == 1) { if (chainX.get(0) + Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) { facingSnake = true; System.out.println("OUCH! You bit me! (facing east)"); break; } } else if (direction == 2) { if (chainX.get(0) == chainX.get(i) && chainY.get(0) + Reference.PIXEL_SIZE == chainY.get(i)) { facingSnake = true; System.out.println("OUCH! You bit me! (facing south)"); break; } } else if (direction == 3) { if (chainX.get(0) - Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) { facingSnake = true; System.out.println("OUCH! You bit me! (facing west)"); break; } } } if (facingSnake == true) { break; } if (isByTop == true && direction == 0) { break; } else if (isByBot == true && direction == 2) { break; } else if (isByRight == true && direction == 1) { break; } else if (isByLeft == true && direction == 3) { break; } else { if (direction == 0) { chainX.add(0, chainX.get(0)); chainY.add(0, chainY.get(0) - Reference.PIXEL_SIZE); } else if (direction == 1) { chainX.add(0, chainX.get(0) + Reference.PIXEL_SIZE); chainY.add(0, chainY.get(0)); } else if (direction == 2) { chainX.add(0, chainX.get(0)); chainY.add(0, chainY.get(0) + Reference.PIXEL_SIZE); } else if (direction == 3) { chainX.add(0, chainX.get(0) - Reference.PIXEL_SIZE); chainY.add(0, chainY.get(0)); } if (doGrow == false) { chainY.remove(chainX.size() - 1); chainX.remove(chainX.size() - 1); } else { doGrow = false; } panel.repaint(); } moveCounter = 0; } score = score + Reference.SCORE_INCREMENT; scoreboard.setText("Made by Xyexs Score: " + score+ " Length: " + chainX.size()); } ImageIcon image = new ImageIcon("\\C:\\Java\\Snake\\resources\\Lost.png"); JLabel imageLabel = new JLabel(); imageLabel.setIcon(image); imageLabel.setVisible(true); window.remove(panel); window.add(imageLabel, BorderLayout.CENTER); window.pack(); } // Public Functions public static int getDir() { return direction; } public static void setDir(int dir) { direction = dir; } }
Last edited by Xyexs; 08-24-2013 at 07:07 PM.
- 08-24-2013, 11:24 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: snake not biting itself
Have you tried deriving line segments from the points that make up the snake and seeing when the
line segments intersect with other line segments. See Line2D (Java Platform SE 7 )
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-25-2013, 12:44 AM #5
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
Re: snake not biting itself
I didn't understand a word :P
- 08-25-2013, 01:12 AM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: snake not biting itself
Well, I am not certain how your snake is constructed so I will pretend it is a series of coordinates that slither around. Let's say the head starts at (x1,y1), the next section at (x2,y2), then (x3, y3), and (x4.y4). If you want to see if the segment of the snake (x1,y1,x2,y2) crosses some other segment (say x45,y45,x46,y46) then if the two segments cross each other (intersect) then the snake crossed itself. There is a method in the link I provided earlier which has methods to detect the intersection of two line segments.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-25-2013, 02:55 AM #7
Member
- Join Date
- Apr 2013
- Posts
- 36
- Rep Power
- 0
Re: snake not biting itself
It's based off 2 arraylists (chainX and chainY) that contain them like {50,100,150 (pixel_size is 50, its kind of a "part" of the snake)} and {200,200,200}
id did this:
Java Code:for (int i = 1; i < chainX.size(); i++) { if (direction == 0) { if (chainX.get(0) == chainX.get(i) && chainY.get(0) - Reference.PIXEL_SIZE == chainY.get(i)) { facingSnake = true; System.out.println("OUCH! You bit me! (facing north)"); Break; } } else if (direction == 1) { if (chainX.get(0) + Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) { facingSnake = true; System.out.println("OUCH! You bit me! (facing east)"); break; } } else if (direction == 2) { if (chainX.get(0) == chainX.get(i) && chainY.get(0) + Reference.PIXEL_SIZE == chainY.get(i)) { FacingSnake = true; System.out.println("OUCH! You bit me! (facing south)"); break; } } else if (direction == 3) { if (chainX.get(0) - Reference.PIXEL_SIZE == chainX.get(i) && chainY.get(0) == chainY.get(i)) { facingSnake = true; System.out.println("OUCH! You bit me! (facing west)"); break; } } }
- 08-25-2013, 08:49 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: snake not biting itself
If you had used Points for the coordinates of the snake segments, a snake would look like this:
Java Code:List<Point> snake= ...;
Java Code:if (snake.lastIndexOf(snake.get(0)) > 0) // snake has bitten itself.
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
Snake Game
By FrankElliot9 in forum New To JavaReplies: 5Last Post: 12-03-2012, 06:01 AM -
altered snake game
By dmp in forum AndroidReplies: 1Last Post: 04-17-2012, 06:49 AM -
Snake Game
By LuluMM in forum New To JavaReplies: 0Last Post: 03-08-2012, 07:48 AM -
snake and ladder
By angela in forum Java AppletsReplies: 2Last Post: 04-09-2011, 06:28 PM -
Snake Game
By mustachMan in forum New To JavaReplies: 2Last Post: 12-10-2009, 11:35 PM
Bookmarks