User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 373,193 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,843 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 215 | Replies: 1
Reply
Join Date: May 2008
Posts: 2
Reputation: aznnytezx is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
aznnytezx aznnytezx is offline Offline
Newbie Poster

Help with snake tail for the game with arrays...

  #1  
May 18th, 2008
<code>import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class snake1
{
public static void main(String args[])
{
String name1= JOptionPane.showInputDialog("Enter Player one's name");
Windows myW=new Windows(name1);

myW.setSize(800,600);
myW.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
});
myW.show();
while(true)
{
myW.repaint();
try{Thread.sleep(60/myW.getLevel());}catch(Exception e){}
}
}
}
class Windows extends Frame implements KeyListener
{
int appleSize = 10;
int snakeSize = 10;
private int x,y,xold,yold,textnum,turn,count;
private boolean go,move,firsttime,win,tie,goTitle,play,selectLevel;
private int size;
private int level = 0;
private int AppleX, AppleY, SnakeX, SnakeY;
private boolean left,right,up,down;
private boolean hitWall,start;
private int numHit;
private String nam1,nam2,winperson;
private int snakeBody[][];
private int growth,hits,totalHits,seconds,currentLength,snakeLength;


public Windows(String n1)
{
nam1=n1;
play=false;
win = false;
x=265;
y=265;
AppleX=x;
AppleY=y;
xold=x;
yold=y;
textnum=6;
firsttime =true;
go =false;
move =false;
goTitle=true;
go=false;
addKeyListener(this);
selectLevel=false;
level=0;
SnakeX=265;
SnakeY=265;

left=false;
right=true;
up=false;
down=false;
size=5;
hitWall=false;
start=false;
growth = 1;

snakeLength = 999;
currentLength = 1;
snakeBody = new int[snakeLength][2];
snakeBody[0][0] = SnakeX;
snakeBody[0][1] = SnakeY;

}

public int getLevel()
{
return level;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
if(goTitle)
title(g);
if(firsttime){
selectLevel=true;
startgame(g);
movebox(g);
firsttime=false;
}
if(go){
selectLevel=true;
snakeGame(g);
}
if(play)
{
snakeGame(g);
selectLevel=false;
}




g.setFont(new Font("Courier",Font.BOLD,30));
g.setColor(Color.yellow);
g.drawString("X = ",490,100); // display x,y on screen
String xnum=Integer.toString(x);
g.drawString(xnum,550,100);
g.drawString("Y = ",640,100);
String ynum=Integer.toString(y);
g.drawString(ynum,700,100);
if(move)
movebox(g);
//repaint();
}


public void title(Graphics g)
{

g.setColor(Color.red);
g.fillOval(100,100,600,450);
g.fillRect(390,30,20,80);

//g.setColor(Color.black);
//g.drawRoundRect(100,150,20,10,20,20);
g.setColor(Color.black);
g.fillRect(0,0,800,600);
//g.setColor(Color.black);
//g.fillRect(100,100,600,400);
g.setColor(Color.red);
g.setFont(new Font("Courier",Font.ITALIC,36));
g.drawString("Snake Game", 300,200);
g.setColor(Color.white);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(410,250,6,6);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(404,250,6,6);
try{Thread.sleep(75);}catch(Exception e){}
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(400,240,20,20);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(400,220,20,20);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(320,200,20,20);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(340,200,20,20);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(360,200,20,20);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(380,200,20,20);
try{Thread.sleep(75);}catch(Exception e){}
g.drawOval(400,200,20,20);

try{Thread.sleep(400);}catch(Exception e){}
for(int k=0; k<4; k++)
{
g.setColor(Color.green);
g.setFont(new Font("Courier",Font.ITALIC,36));
g.drawString("How you like them APPLES!", 140,400);
try{Thread.sleep(400);}catch(Exception e){}
g.setColor(Color.red);
g.setFont(new Font("Courier",Font.ITALIC,36));
g.drawString("How you like them APPLES!", 140,400);
try{Thread.sleep(400);}catch(Exception e){}
}






goTitle=false;
firsttime=true;

}
public void startgame(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,800,600);
g.setColor(Color.white);
g.setFont(new Font("Courier",Font.ITALIC,30));
g.drawString("Snake Game", 300,75);
firsttime=false;


g.setColor(Color.yellow);
g.drawString("Easy",250,265);


g.setColor(Color.yellow);
g.drawString("Normal",250,365);


g.setColor(Color.yellow);
g.drawString("Hard",250,465);
}

public void keyPressed(KeyEvent event)
{
if(selectLevel)
{
xold=x;
yold=y;
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
if(y<=265)
y=265;
else
y-=100;
move=true;
break;

case KeyEvent.VK_DOWN:
if(y>=465)
y=465;
else
y+=100;
move=true;
break;

case KeyEvent.VK_ENTER:
if(y==465) //HARD
level=3;
else if (y == 365) //MEDIUM
level=2;
else // EASY
level=1;

if(x==xold && y==yold)
{
play=true;
}
break;
}
}
else
{

if (event.getKeyCode() == KeyEvent.VK_UP)
{
if(!down)
{
down=false;
up=true;
}
left=false;
right=false;
move=false;
selectLevel=false;
}

if (event.getKeyCode() == KeyEvent.VK_DOWN)
{
if(!up)
{
up=false;
down=true;
}
left=false;
right=false;
move=false;
selectLevel=false;
}

if (event.getKeyCode() == KeyEvent.VK_LEFT)
{
if(!right)
{
right=false;
left=true;
}
up=false;
down=false;
move=false;
selectLevel=false;
}

if (event.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(!left)
{
left=false;
right=true;
}
up=false;
down=false;
move=false;
selectLevel=false;
}

if (event.getKeyCode() == KeyEvent.VK_ENTER)
play=true;
}
}

public void keyReleased(KeyEvent event)
{
}

public void keyTyped(KeyEvent event)
{
}

public void movebox(Graphics screen)
{

screen.setColor(Color.black); //blue rectangle to clear
screen.fillRect(490,80,260,30);

screen.setFont(new Font("Courier",Font.BOLD,30));
screen.setColor(Color.yellow);
screen.drawString("X = ",490,100); // display x,y on screen
String xnum=Integer.toString(x);
screen.drawString(xnum,550,100);
screen.drawString("Y = ",640,100);
String ynum=Integer.toString(y);
screen.drawString(ynum,700,100);


if(x!=xold || y!=yold)
{
screen.setColor(Color.black);
screen.fillRect(xold-10,yold,xold+110,yold);

screen.setFont(new Font("Courier",Font.ITALIC,30));


screen.setColor(Color.yellow);
screen.drawString("Easy",250,265);


screen.setColor(Color.yellow);
screen.drawString("Normal",250,365);


screen.setColor(Color.yellow);
screen.drawString("Hard",250,465);

screen.setColor(Color.red);
screen.drawLine(x-10,y,x+110,y);
}

xold=x;
yold=y;
move=false;
selectLevel=true;


}

public void snakeGame(Graphics g)
{
Random rnum=new Random();
g.setColor(Color.gray);
g.fillRect(0,0,800,600);
DrawApple(g, AppleX, AppleY);
g.setColor(Color.white);
g.setFont(new Font("Courier",Font.ITALIC,30));


if(left)
SnakeX-=size;
if(right)
SnakeX+=size;
if (up)
SnakeY-=size;
if(down)
SnakeY+=size;

drawSnake(g);

if (SnakeX<5)
{
hitWall=true;
SnakeX=785;

}
if(SnakeX>785)
{
hitWall=true;
SnakeX=5;
}
if(SnakeY<30)
{
hitWall=true;
SnakeY=585;
}
if(SnakeY>585)
{
hitWall=true;
SnakeY=30;
}






selectLevel=false;

boolean touchSnakeX = (AppleX >= SnakeX) && (AppleX <= SnakeX + snakeSize);
boolean touchSnakeY = (AppleY >= SnakeY) && (AppleY <= SnakeY + snakeSize);

boolean touchAppleX = (AppleX <= SnakeX) && (AppleX + appleSize >= SnakeX);
boolean touchAppleY = (AppleY <= SnakeY) && (AppleY + appleSize >= SnakeY);
//if(SnakeX==AppleX && SnakeY==AppleY)
if((touchSnakeX && touchSnakeY) || (touchAppleX && touchAppleY))
{
AppleX = rnum.nextInt(785/size)*size+size;
AppleY = rnum.nextInt(585/size)*size+size;
if(AppleX<30)
AppleX=30;
if(AppleX>785)
AppleX=785;
if(AppleY<5)
AppleY=5;
if(AppleY>785)
AppleY=785;

}
}

public void DrawApple(Graphics g, int x, int y)
{
g.setColor(Color.green);
g.fillRect(x,y,appleSize,appleSize);
}

public void drawSnake(Graphics g)
{
g.fillRect(SnakeX,SnakeY, snakeSize, snakeSize);

}

}




</code>

Can anyone help me go through this..?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: New Hampshire
Posts: 3,297
Reputation: jasimp is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 18
jasimp's Avatar
jasimp jasimp is offline Offline
Nearly a Senior Poster

Re: Help with snake tail for the game with arrays...

  #2  
May 19th, 2008
What is your problem?
A room without books is like a body without a soul.
Facts are meaningless. They can be used to prove anything that is even remotely true.
Go then, there are other worlds than these.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 10:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC