Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners genaral snake game in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


chandrasekhar
genaral snake game in java
4 Answer(s)      6 years and 3 months ago
Posted in : Java Beginners



Advertisement
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
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
Game  How to develop a game using java? give me a total code for any simple game
game Nim - Java Beginners
game Nim  how is the search tree for a game Nim looks like
game
game  How to make 3D game in java Applet
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 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
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
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
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 poramboku.me@gmai.com
java - Swing AWT
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
java - Java Interview Questions
java  Write Snake Game using Swings
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
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: http://roseindia.net/java/example/java/swing/chess
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
please provide code - Java Beginners
please provide code  provide code to snake game in J2SE
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
How do i slow down the game (othello game)??
How do i slow down the game (othello game)??  Hello, I built an othello game that can be played by 2 humans , human and computer and two computers... with each other the fram doesn't appear until the game is finish. I think it is becase
Can anyone assist with the coding of this game?
Can anyone assist with the coding of this game?  I am in need of the coding for this game. I need to write a "Hangman" game that will randomly generate a word from a text file and will prompt the user to guess one letter
 

 

 

DMCA.com