genaral snake game in java

genaral snake game in java

View Answers

January 30, 2009 at 7:06 AM

Hi

This is the source code of Snake game in swing

Snake.java file

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();
}
}

January 30, 2009 at 7:07 AM

This is Board.java file:

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);
}

January 30, 2009 at 7:09 AM

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;
}
}
}
}


Thanks....

June 19, 2011 at 3:31 PM

this program can not run ... please solve it thinks









Related Tutorials/Questions & Answers:
genaral snake game in java - Java Beginners
genaral snake game in java  can u please send code for fallowing write a snake game program using swings?  Hi This is the source code of Snake game in swing Snake.java file import javax.swing.JFrame
plz help me to write a snake game using swings - Swing AWT
plz help me to write a snake game using swings  write snake game program using swings
Advertisements
genaral - Java Interview Questions
java general path example  What is the Java general path example?  i dont know your mail address to send the answer. post it and get the reply
java game
java game  Can anyone please give me a game source code
JAVA Game
JAVA Game  I want to make a JAVA game...in dat game...d player has to input a word in 10 secnds...4 dat m running a delay loop... but the problem is not...i am not getting any way...to make that input field(eg: String str
Game
Game  How to develop a game using java? give me a total code for any simple game
Create a Swamp Game in Java
Create a Swamp Game in Java  You are to create a game called â??Get Out of My Swampâ??. In this game an ogre, called â??Hekâ??, wanders around his... two ogre enemies in the same place they kill the ogre and the game ends. Full
game
game  How to make 3D game in java Applet
Snake Problem
Snake Problem  Hello, to one and all.I am trying to develop a snake game using J2ME and its almost finish but in this apps i have used multiple... it calls t1(thread) in which i have used t1.start() method to allow snake to move right
Game
Game  how can create simple game in java . thank u so match ,please help
game Nim - Java Beginners
game Nim  how is the search tree for a game Nim looks like
Java Game Pontoon
Java Game Pontoon  Could someone help me create a programme for the game pontoon //play a game of pontoon import java.util.*; public class Pontoon { public static void main (String args[]) { //deal initial hands
game
game  Hi thank you for answers.I am devoloping a java game caled Duck Hunt Game in netbeans6.7.1 software and I need help in some codes...,if you have this game's code please post me as soon as possible
Java game Pontoon
Java game Pontoon  Hello. I have trouble with my pontoon code. I ahve done it using simple java programming. It is working BUT it dosent stops when the players hand rises ober 24 points. where in game the player should be bust
Java program for connectfour game
Java program for connectfour game  Hello, my program is about connectFour game. I need help with resolving the logic for this problem. I appreciate...; ConnectFourServer game = new ConnectFourServer(); private ConnectFourClient
java - Swing AWT
java   Write Snake Game using Swings
ModuleNotFoundError: No module named 'Snake'
ModuleNotFoundError: No module named 'Snake'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'Snake' How to remove the ModuleNotFoundError: No module named 'Snake'
Craps Game Java Programming Help
Craps Game Java Programming Help  Craps is a casino game that involves the throwing of a pair of dice. Based on the throw, the thrower either gets to continue throw (and win money), or stops throwing (and loses money). Write your
who know full coding of guessing game in java?
who know full coding of guessing game in java?  There is only 1 player. Use JAVA Random toolkit to generate random number between 1 to 10. Once user... again or to exit the game by type 1 to proceed the game and type 2 to end the game
Java game bulls and cows
Java game bulls and cows In this tutorial, you will learn how to implement bulls and cows game in java. The game bulls and cows is an ancient game...;quit", the user will come out from the game. If the user inputs 4 digit
ModuleNotFoundError: No module named 'aldryn-snake'
ModuleNotFoundError: No module named 'aldryn-snake'  Hi, My Python... 'aldryn-snake' How to remove the ModuleNotFoundError: No module named 'aldryn-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'aldryn-snake'
ModuleNotFoundError: No module named 'aldryn-snake'  Hi, My Python... 'aldryn-snake' How to remove the ModuleNotFoundError: No module named 'aldryn-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'aldryn-snake'
ModuleNotFoundError: No module named 'aldryn-snake'  Hi, My Python... 'aldryn-snake' How to remove the ModuleNotFoundError: No module named 'aldryn-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nevolution-snake'
ModuleNotFoundError: No module named 'nevolution-snake'  Hi, My... named 'nevolution-snake' How to remove the ModuleNotFoundError: No module named 'nevolution-snake' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'py-snake'
ModuleNotFoundError: No module named 'py-snake'  Hi, My Python...-snake' How to remove the ModuleNotFoundError: No module named 'py-snake... to install padas library. You can install py-snake python with following
ModuleNotFoundError: No module named 'py-snake'
ModuleNotFoundError: No module named 'py-snake'  Hi, My Python...-snake' How to remove the ModuleNotFoundError: No module named 'py-snake... to install padas library. You can install py-snake python with following
ModuleNotFoundError: No module named 'py-snake'
ModuleNotFoundError: No module named 'py-snake'  Hi, My Python...-snake' How to remove the ModuleNotFoundError: No module named 'py-snake... to install padas library. You can install py-snake python with following
ModuleNotFoundError: No module named 'python-snake'
ModuleNotFoundError: No module named 'python-snake'  Hi, My Python... 'python-snake' How to remove the ModuleNotFoundError: No module named 'python-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'python-snake'
ModuleNotFoundError: No module named 'python-snake'  Hi, My Python... 'python-snake' How to remove the ModuleNotFoundError: No module named 'python-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'python-snake'
ModuleNotFoundError: No module named 'python-snake'  Hi, My Python... 'python-snake' How to remove the ModuleNotFoundError: No module named 'python-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'rocket-snake'
ModuleNotFoundError: No module named 'rocket-snake'  Hi, My Python... 'rocket-snake' How to remove the ModuleNotFoundError: No module named 'rocket-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'snake-case'
ModuleNotFoundError: No module named 'snake-case'  Hi, My Python... 'snake-case' How to remove the ModuleNotFoundError: No module named 'snake-case' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'snake-eyes'
ModuleNotFoundError: No module named 'snake-eyes'  Hi, My Python... 'snake-eyes' How to remove the ModuleNotFoundError: No module named 'snake-eyes' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'snake-guice'
ModuleNotFoundError: No module named 'snake-guice'  Hi, My Python... 'snake-guice' How to remove the ModuleNotFoundError: No module named 'snake-guice' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'snake-gym'
ModuleNotFoundError: No module named 'snake-gym'  Hi, My Python... 'snake-gym' How to remove the ModuleNotFoundError: No module named 'snake... have to install padas library. You can install snake-gym python with following
ModuleNotFoundError: No module named 'snake_nupic'
ModuleNotFoundError: No module named 'snake_nupic'  Hi, My Python... 'snake_nupic' How to remove the ModuleNotFoundError: No module named 'snake_nupic' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'snake-on-pygame'
ModuleNotFoundError: No module named 'snake-on-pygame'  Hi, My... named 'snake-on-pygame' How to remove the ModuleNotFoundError: No module named 'snake-on-pygame' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'snake-pit'
ModuleNotFoundError: No module named 'snake-pit'  Hi, My Python... 'snake-pit' How to remove the ModuleNotFoundError: No module named 'snake... have to install padas library. You can install snake-pit python with following
ModuleNotFoundError: No module named 'Snake-Steak'
ModuleNotFoundError: No module named 'Snake-Steak'  Hi, My Python... 'Snake-Steak' How to remove the ModuleNotFoundError: No module named 'Snake-Steak' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'aldryn-snake'
ModuleNotFoundError: No module named 'aldryn-snake'  Hi, My Python... 'aldryn-snake' How to remove the ModuleNotFoundError: No module named 'aldryn-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'camel-and-snake'
ModuleNotFoundError: No module named 'camel-and-snake'  Hi, My... named 'camel-and-snake' How to remove the ModuleNotFoundError: No module named 'camel-and-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'console-snake'
ModuleNotFoundError: No module named 'console-snake'  Hi, My... 'console-snake' How to remove the ModuleNotFoundError: No module named 'console-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'curses-snake'
ModuleNotFoundError: No module named 'curses-snake'  Hi, My Python... 'curses-snake' How to remove the ModuleNotFoundError: No module named 'curses-snake' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'frigga-snake'
ModuleNotFoundError: No module named 'frigga-snake'  Hi, My Python... 'frigga-snake' How to remove the ModuleNotFoundError: No module named 'frigga-snake' error? Thanks   Hi, In your python
BRICK-BREAKER game concept - Java Beginners
BRICK-BREAKER game concept  I am planning to write a program for BRICK-BREAKER GAME. can any 1 help me any 1 can u plz help me frzzz?? you can also send the mails to [email protected]
game programming code - Swing AWT
game programming code  write a snake program using swings with step by step explanation?. please send me this source code to my mail id with step by step explanation
java - Java Interview Questions
java  Write Snake Game using Swings
tetris game code - Swing AWT
tetris game code  To develop a JAVA puzzle game which is a "variation" of the Tetris game
ModuleNotFoundError: No module named 'snake_progress_bar'
ModuleNotFoundError: No module named 'snake_progress_bar'  Hi, My... named 'snake_progress_bar' How to remove the ModuleNotFoundError: No module named 'snake_progress_bar' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'snake-rl-melanol'
ModuleNotFoundError: No module named 'snake-rl-melanol'  Hi, My... named 'snake-rl-melanol' How to remove the ModuleNotFoundError: No module named 'snake-rl-melanol' error? Thanks   Hi, In your

Ads