Results 1 to 19 of 19
Like Tree3Likes
  • 1 Post By SurfMan
  • 1 Post By Junky
  • 1 Post By Tolls

Thread: Snake game

  1. #1
    surbhijain93 is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Snake game

    Java games Snake

    The link contains the snake game i am understanding.
    Can someone that understands it fully,pls mail me at surbhijain93@gmail.com
    I just want to discuss it with someone.
    Please I would be highly obliged.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Snake game

    If you have questions about a program, please post the code on the forum and ask your questions about it.
    Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Snake game

    But if we do that, how will others benefit from your questions and the pending discussions?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Snake game

    Guys, it's not so hard, you know! You have to move the snake around so that it eats the apples. Every time the snake eats an apple, it grows a bit bigger. The game is over when it hits the wall or eats itself.

    I used to have this on my Nokia 3310. That reminds me to recharge it and play it again! It was tons of fun!!!1one!
    gimbal2 likes this.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Snake game

    lol :) I feel soooooooo dumb for not getting it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Snake game

    Snakes don't eat apples.

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Snake game

    Stop nitpicking, you obviously suck at Snakes/Nibbles. You're just jealous :P

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Snake game

    I'm sure somewhere in the world at some point in time there was a snake that mistakenly swallowed an apple, perhaps because it had dead mouse guts attached to it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Snake game

    Snakes and apples have never been a good combination...

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Snake game

    Quote Originally Posted by gimbal2 View Post
    I'm sure somewhere in the world at some point in time there was a snake that mistakenly swallowed an apple, perhaps because it had dead mouse guts attached to it.
    Don't be silly; even that snake in paradise gave his apple to Eve (in digust, I'm sure). Snakes hate apples even when they're disguised as mice. Mice, on the other hand, try to look like apples as much as they can, but snakes are more intelligent than that; silly mice ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Snake game

    Here are some mice disguised as apples...



    (Source: https://en.wikipedia.org/wiki/Apple_Mouse)

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Snake game

    Quote Originally Posted by JosAH View Post
    Don't be silly; even that snake in paradise gave his apple to Eve (in digust, I'm sure).
    Well I can't argue with that!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Snake game

    Quote Originally Posted by SurfMan View Post
    Here are some mice disguised as apples...
    No, those are snakes too with very fat bottoms with an apple tattooed on them; you obviously never paid attention during biology lessons ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    surbhijain93 is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Snake game

    Can anyone run this?
    pls tell how to run it

    i have put both files in a folder
    i.e. Board.java and Snake.java in one folder
    when i run snake.java it says cant find board class.
    pls help

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Snake game

    Please post the code here on the forum along with your questions. Copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    surbhijain93 is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Snake game

    Quote Originally Posted by Norm View Post
    Please post the code here on the forum along with your questions. Copy the full text of the error message and paste it here.


    Board.java
    package snake2;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;

    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.Timer;


    public class Board extends JPanel implements ActionListener {

    private final int WIDTH = 300;
    private final int HEIGHT = 300;
    private final int DOT_SIZE = 10;
    private final int ALL_DOTS = 900;
    private final int RAND_POS = 29;
    private final int DELAY = 140;

    private int x[] = new int[ALL_DOTS];
    private int y[] = new int[ALL_DOTS];

    private int dots;
    private int apple_x;
    private int apple_y;

    private boolean left = false;
    private boolean right = true;
    private boolean up = false;
    private boolean down = false;
    private boolean inGame = true;

    private Timer timer;
    private Image ball;
    private Image apple;
    private Image head;


    public Board() {
    addKeyListener(new TAdapter());

    setBackground(Color.black);

    ImageIcon iid = new ImageIcon(this.getClass().getResource("dot.png"));
    ball = iid.getImage();

    ImageIcon iia = new ImageIcon(this.getClass().getResource("apple.png") );
    apple = iia.getImage();

    ImageIcon iih = new ImageIcon(this.getClass().getResource("head.png")) ;
    head = iih.getImage();

    setFocusable(true);
    initGame();
    }


    public void initGame() {

    dots = 3;

    for (int z = 0; z < dots; z++) {
    x[z] = 50 - z*10;
    y[z] = 50;
    }

    locateApple();

    timer = new Timer(DELAY, this);
    timer.start();
    }


    public void paint(Graphics g) {
    super.paint(g);

    if (inGame) {

    g.drawImage(apple, apple_x, apple_y, this);

    for (int z = 0; z < dots; z++) {
    if (z == 0)
    g.drawImage(head, x[z], y[z], this);
    else g.drawImage(ball, x[z], y[z], this);
    }

    Toolkit.getDefaultToolkit().sync();
    g.dispose();

    } else {
    gameOver(g);
    }
    }


    public void gameOver(Graphics g) {
    String msg = "Game Over";
    Font small = new Font("Helvetica", Font.BOLD, 14);
    FontMetrics metr = this.getFontMetrics(small);

    g.setColor(Color.white);
    g.setFont(small);
    g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2,
    HEIGHT / 2);
    }


    public void checkApple() {

    if ((x[0] == apple_x) && (y[0] == apple_y)) {
    dots++;
    locateApple();
    }
    }


    public void move() {

    for (int z = dots; z > 0; z--) {
    x[z] = x[(z - 1)];
    y[z] = y[(z - 1)];
    }

    if (left) {
    x[0] -= DOT_SIZE;
    }

    if (right) {
    x[0] += DOT_SIZE;
    }

    if (up) {
    y[0] -= DOT_SIZE;
    }

    if (down) {
    y[0] += DOT_SIZE;
    }
    }


    public void checkCollision() {

    for (int z = dots; z > 0; z--) {

    if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
    inGame = false;
    }
    }

    if (y[0] > HEIGHT) {
    inGame = false;
    }

    if (y[0] < 0) {
    inGame = false;
    }

    if (x[0] > WIDTH) {
    inGame = false;
    }

    if (x[0] < 0) {
    inGame = false;
    }
    }

    public void locateApple() {
    int r = (int) (Math.random() * RAND_POS);
    apple_x = ((r * DOT_SIZE));
    r = (int) (Math.random() * RAND_POS);
    apple_y = ((r * DOT_SIZE));
    }

    public void actionPerformed(ActionEvent e) {

    if (inGame) {
    checkApple();
    checkCollision();
    move();
    }

    repaint();
    }


    private class TAdapter extends KeyAdapter {

    public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if ((key == KeyEvent.VK_LEFT) && (!right)) {
    left = true;
    up = false;
    down = false;
    }

    if ((key == KeyEvent.VK_RIGHT) && (!left)) {
    right = true;
    up = false;
    down = false;
    }

    if ((key == KeyEvent.VK_UP) && (!down)) {
    up = true;
    right = false;
    left = false;
    }

    if ((key == KeyEvent.VK_DOWN) && (!up)) {
    down = true;
    right = false;
    left = false;
    }
    }
    }
    }




    Snake.java
    package snake2;

    import javax.swing.JFrame;


    public class Snake extends JFrame {

    public Snake() {

    add(new Board());

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(320, 340);
    setLocationRelativeTo(null);
    setTitle("Snake");

    setResizable(false);
    setVisible(true);
    }

    public static void main(String[] args) {
    new Snake();
    }
    }



    error- cannot find Board class

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Snake game

    If this is some game that someone else has written, you have downloaded and cannot get installed and play it, then I suggest you contact the author and ask them.
    gimbal2 likes this.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Snake game

    Please copy the FULL text of the error message and paste it here. Don't leave off any of it,

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Snake game

    Along with Norm's request, you need to say exactly what you are doing.
    What command are you running?
    Where are you running it from in relation to where those files are located?
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Processing Snake game help
    By Bigchickenmac in forum New To Java
    Replies: 1
    Last Post: 04-11-2013, 06:35 PM
  2. Snake Game
    By FrankElliot9 in forum New To Java
    Replies: 5
    Last Post: 12-03-2012, 06:01 AM
  3. Snake Game
    By LuluMM in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 07:48 AM
  4. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 11:35 PM
  5. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 01:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •