954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

simple snake game

hi Friends

can you pl help me to create a simple snake game.
give me with step by step creation.

thanks.

madhub2v
Light Poster
36 posts since Jun 2010
Reputation Points: 8
Solved Threads: 0
 

IOW "give me teh codez". No.

Post your code here along with all compiler messages and/or exceptions as well as a descritpion of what it is suppossed to do and what it actually does and exactly how they differ, and we will help you to correct your own code, but you are going to have to do it.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

i hava this code. working perfect.
but i want explanation for it.
I dont know event handling in this .

How snake is moving here?

wht does update and Paint methods do here?

import java.awt.*;
import java.applet.*;

public class snake extends Applet implements Runnable
{
Image dot[]=new Image[400];
Image back;
Image     offI;
Graphics  offG;

int x[]= new int[400];
int y[]= new int[400];
int rtemp=1;
int game=1;
int level;
int z;
int n;
int count=0;
int score=0;
int add=1;

Button b= new Button("Beginner");
Button i= new Button("Intermediate");
Button p= new Button("Professional");
Button X= new Button("Xtreamest");

String stemp;
String s;
String t;

boolean go[]=new boolean[400];
boolean left=false;
boolean right=false;
boolean up=false;
boolean down=false;
boolean started=false ;
boolean me=false;

Thread setTime;

public void init() 
{
add(b);
add(i);
add(p);
add(X);
setBackground(Color.black);
back = getImage(getCodeBase(), "screan.gif");
for (z=0 ; z < 400 ; z++){dot[z] = getImage(getCodeBase(), "dot.gif");  }
}


public void update(Graphics g)
 {
 Dimension d = this.size();
 if(offI == null)
 {
  offI = createImage(d.width, d.height);
  offG = offI.getGraphics();
 }
  offG.clearRect(0, 0, d.width, d.height);
  paint(offG);
  g.drawImage(offI, 0, 0, null);
  }

public void paint(Graphics g)
 {
  g.drawImage(back, 0, 0, this);
  g.setColor(Color.white);

 if(started)
 {
  g.setFont(new Font("Verdana", 1, 12));
  t = "Score "+score+"";
  g.drawString(t, 75, 220);
 }

 if(game==1) 
 { 
   g.setFont(new Font("Verdana", 1, 13));
   s = "Select Mode";
   g.drawString(s, 65, 30);

   b.move(75, 50); 
   i.move(68, 90);
   p.move(68, 130);
   X.move(73, 170);
 }

if((game==2)||(game==3))
{
if(!started)
{
g.setFont(new Font("Verdana", 1, 11));
t = "Use the key board arrows to move!";
g.drawString(t, 5, 215);
}
for (z=0 ; z <= n ; z++){  g.drawImage(dot[z],x[z],y[z],this);  }
me=true;
}

if(!me)
{
g.setFont(new Font("Verdana", 1, 11));
t = "by Omar Wally, http://crash.to/PLAY";
g.drawString(t, 5, 215);  
}

if(game==3)
{
g.setFont(new Font("Verdana", 1, 13));
s="Game Over";
g.drawString(s, 65, 60);
}

}

public void run()
{
for(z=4 ;z <400 ; z++) { go[z]=false;}
for(z=0 ; z<4 ; z++) { go[z]=true;x[z]=91;y[z]=91;}
n=3;
game=2;
score=0;
b.move(70, -100);
i.move(70, -100);
p.move(70, -100);
X.move(70, -100);
left=false;
right=false;
up=false;
down=false;
locateRandom(4);

while(true)
{
if (game==2)
{
if ((x[0]==x[n])&&(y[0]==y[n])){go[n]=true;locateRandom((n+1));score+=add; }
for(z = 399 ; z > 0 ; z--)
{
if (go[z]) 
{ 
x[z] = x[(z-1)]; y[z] = y[(z-1)]; 
if ((z>4)&&(x[0]==x[z])&&(y[0]==y[z])){ game=3; } 
}
}
if(left){ x[0]-=10; }
if(right){ x[0]+=10; }
if(up){ y[0]-=10; }
if(down){ y[0]+=10; }
}

if(y[0]>191){y[0]=191;game=3;}
if(y[0]<1){y[0]=1;game=3;}
if(x[0]>191){x[0]=191;game=3;}
if(x[0]<1){x[0]=1;game=3;}

if (game==3)
{ 
if (count <(1500/level)) { count++; } else { count=0;game=1;repaint();setTime.stop(); } 
}

repaint();
try{setTime.sleep(level);}
catch(InterruptedException e){}
}
}

public void locateRandom(int turn)
{
rtemp=(int)(Math.random()*20);
x[turn]=((rtemp*10)+1) ;
rtemp=(int)(Math.random()*20);
y[turn]=((rtemp*10)+1);
n++;
}

public boolean keyDown(Event e, int key) 
{
if ((key == Event.LEFT) &&(!right)){left = true; up = false; down = false;if(!started)started=true;}
if ((key == Event.RIGHT) && (!left)){right = true; up = false; down = false;if(!started)started=true;}
if ((key == Event.UP) && (!down)){ up = true; right = false; left = false;if(!started)started=true;}
if ((key == Event.DOWN) && (!up)){down = true; right = false; left = false;if(!started)started=true;}
return true;
}

public boolean action(Event event, Object obj)
{
stemp = (String) obj;

if(stemp.equals("Beginner"))
{
add=2;
level=100;
setTime = new Thread(this);
setTime.start();
return true;
}

if(stemp.equals("Intermediate"))
{
add=5;
level=70;
setTime = new Thread(this);
setTime.start();
return true;
}

if(stemp.equals("Professional"))
{
add=10;
level=40;
setTime = new Thread(this);
setTime.start();
return true;
}

if(stemp.equals("Xtreamest"))
{
add=20;
level=20;
setTime = new Thread(this);
setTime.start();
return true;
}


return false;
}                      


}
madhub2v
Light Poster
36 posts since Jun 2010
Reputation Points: 8
Solved Threads: 0
 

IOW, Someone else already gave me the code, but I can't defend it to my instructor.

Try writing some code. Try doing your homework and you might actually learn something.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I want step by step dovelopment of this snake game.

madhub2v
Light Poster
36 posts since Jun 2010
Reputation Points: 8
Solved Threads: 0
 

Design, code, test, deploy.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

i am also working on the snake game.i am taking help from this site.check it out its in applet
http://www.javacooperation.gmxhome.de/indexEng.html

extemer
Junior Poster
Banned
188 posts since Apr 2010
Reputation Points: -7
Solved Threads: 10
 

Another who thinks they'll learn by not doing anything?

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
b.move(75, 50);    i.move(68, 90);   p.move(68, 130);   X.move(73, 170);


what is the use of "move" method here?

there is no method like "move" in AWT.?

pleas explain....

madhub2v
Light Poster
36 posts since Jun 2010
Reputation Points: 8
Solved Threads: 0
 

Using someone else's code for your homework is called "cheating". Nobody here is going to help you cheat.
Write your own code and you will get help.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

sory.

I m not cheating any body.
i m trying to learn my self

i found it from a book.

is it cheating to learn from any body who knows well?

madhub2v
Light Poster
36 posts since Jun 2010
Reputation Points: 8
Solved Threads: 0
 

>>i found it from a book.
>>is it cheating to learn from any body who knows well?

I don't believe that code comes from a book. If that person ``who knows well' writes such code, full with magic numbers, and no comments at all - just to give a few examples of bad practice - and then publishes this rubbish, then I wouldn't dare to say that this person ``knows well'.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

The reason why you're not getting much help is because you're copying and pasting code and asking us to explain it to you. You're not showing any effort at all.

We can help explain to you concepts, for example what's a method, and help you debug your code, but you have to actually try to write the code.

Furthermore, oftentimes writing code helps you answer a lot of your questions (e.g. where does the move() method come from, how does it work).

coil
Posting Whiz in Training
273 posts since Aug 2010
Reputation Points: 27
Solved Threads: 56
 
what is the use of "move" method here?


Here you need to use the API doc. It will explain to you what the move method does.
Find the object class that move is a method for and go to that class's API doc.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

thank you for your suggestions,

I understood what you mean.

madhub2v
Light Poster
36 posts since Jun 2010
Reputation Points: 8
Solved Threads: 0
 

back = getImage(getCodeBase(), "screan.gif");
i am using the netbean the snake game above mentioned , in which folder or location i put the image screen.gif plzzzzzzz some reply

abdul.qadir
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

@abdul.qadir
Please start your own thread with your problem.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

^ What Norm said.

Thread closed.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: