genaral snake game in java


genaral snake game in java

genaral snake game in java
4 Answer(s)      9 years ago
Posted in : Java Beginners
View Answers

January 30, 2009 at 7:06 AM


This is the source code of Snake game in swing file

import javax.swing.JFrame;

public class Snake extends JFrame {

public Snake() {

add(new Board());

setSize(320, 340);


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

January 30, 2009 at 7:07 AM

This is 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());


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


public void initGame() {

dots = 3;

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


timer = new Timer(DELAY, this);

public void paint(Graphics 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);


} else {

public void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(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)) {

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) {


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;


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 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
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  I want to make a JAVA 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 make that input field(eg: String str
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
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 Nim - Java Beginners
game Nim  how is the search tree for a game Nim looks like
Game  How to develop a game using java? give me a total code for any simple game
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  How to make 3D game in java Applet
Game  how can create simple game in java . thank u so match ,please help
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
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
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
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
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 - Swing AWT
java   Write Snake Game using Swings
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]
java - Java Interview Questions
java  Write Snake Game using Swings
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
tetris game code - Swing AWT
tetris game code  To develop a JAVA puzzle game which is a "variation" of the Tetris game
game programming
game programming  Hi. i have witten the script and have almost completed developing a social game like mafia wars. can any one plz help me by answering me some of my doubts.. after uploading the game on facebook how tokeep data
Blackjack game - IDE Questions
Blackjack game  Create a java code for a simple blackjack game for just one hand of Blackjack, it does not need to include money and betting
java program
java program  hello.. do u all have any source to do the java programming for Snake and Ladder Game or any reservation system.. help me plz
please provide code - Java Beginners
please provide code  provide code to snake game in J2SE
Chess Game
Chess Game  How could I make a basic 8x8 chess table with two dimensional arrays and insert the chess pieces in the array?   Please visit the following link:
Create a guessing game - Framework
Create a guessing game  Create a guessing game that picks a random number between 1 and 100 and lets you keep guessing until you get it right! You..., too. it is in java but i m not sure about the language, it is like c.print
Artifacts of cn.q-game
List of Artifacts of cn.q-game maven depenency