genaral snake game in java

Ads
 

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

Ads









Related Tutorials/Questions & Answers:
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
Advertisements
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
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  How to develop a game using java? give me a total code for any simple game
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
game
game  How to make 3D game in java Applet
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
Game  how can create simple game in java . thank u so match ,please help
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
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
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
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 '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'
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 '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
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

Ads