-
snake applet gulp.='(
problems
1. SNAKE DOESNT PRINT OR MOVE. confusion especially regarding the Snake class furthest below
2. id like the remove/add/grow to be checked.
thanks in advance
// The "Snake" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends Applet implements KeyListener, Runnable
{
Thread t;
int speed;
int x;
int y;
boolean eating;
int food;
Snake s;
Graphics g;
Color c;
/* start gets you to the run
*/
public void run ()
{
food = 0;
eating = false;
int x = 0;
// while (s.alive ())
// {
// if (food == 0)
// fp x = new fp ();
x = (int) (Math.random () * 9) + 1;
if (eating)
{
s.grow ();
//s.move()..?
food--;
if (food==0)
eating=false;
//s.drawString(750,750,"length of snake: "+s.length());
//for (double delay=0;double<speed;double++);
}
//else
//g.clearRect(10,700,10,700);
//}
//create a food point p SEMICOLON TERE RIGHT NOW...
// }
}
public void init ()
{
s = new Snake ();
setSize (800, 800);
t = null;
speed = 10000000;
addKeyListener (this);
reset (); //
run ();
}
public void paint (Graphics g)
{
// Place the body of the drawing method here
} // paint method
public void drawGrid ()
{
g = getGraphics ();
int Locx = 0;
int Locy = 0;
int Locx1 = 650;
int Locy1 = 0;
c = Color.green;
g.setColor (c);
while (Locx < 700)
{
sp x = new sp (Locx, Locy);
Locx++;
g.drawRect (Locx * 10, Locy * 10, 9, 9);
}
Locx = 0;
while (Locy < 700)
{
sp x = new sp (Locx, Locy);
Locy++;
g.drawRect (Locx * 10, Locy * 10, 9, 9);
}
Locy = 0;
while (Locy < 700)
{
sp x = new sp (Locx, Locy);
Locy++;
g.drawRect (690, Locy * 10, 9, 9);
}
Locx = 0;
while (Locx < 700)
{
sp x = new sp (Locx, Locy);
Locx++;
g.drawRect (Locx * 10, 690, 9, 9);
}
// Locx = 0;
// Locy1 = 0;
// while (Locy1 < 700)
// {
// sp x = new sp (Locx, Locy);
// Locy++;
// g.drawRect (Locx * 10, Locy * 10, 9, 9);
// }
}
public void reset ()
{
if (t != null)
t.stop ();
t = new Thread (this);
t.start ();
plot (g, c);
drawGrid ();
}
public void keyPressed (KeyEvent e)
{
int k = e.getKeyCode ();
if (k == KeyEvent.VK_DOWN)
s.changeDirection ('D');
else if (k == KeyEvent.VK_UP)
s.changeDirection ('U');
else if (k == KeyEvent.VK_LEFT)
s.changeDirection ('L');
else if (k == KeyEvent.VK_RIGHT)
s.changeDirection ('R');
}
public void keyTyped (KeyEvent e)
{
char k = e.getKeyChar ();
if (k == 's')
reset ();
else if (k == 'z')
speed += 100000;
else if (k == 'x')
speed -= 100000;
}
public void keyReleased (KeyEvent e)
{
}
public void plot (Graphics g, Color c)
{
g = getGraphics ();
c = Color.green;
g.setColor (c);
g.drawRect (x * 10, y * 10, 9, 9);
}
public sp up ()
{
sp z = new sp (x, y + 1);
return z;
}
public sp down ()
{
sp z = new sp (x, y - 1);
return z;
}
public sp left ()
{
sp z = new sp (x - 1, y);
return z;
}
public sp right ()
{
sp z = new sp (x + 1, y + 1);
return z;
}
} // Snake class
class SnakeNode
{
sp p;
SnakeNode next;
public SnakeNode ()
{
//IT MUST BE SET TO SOMETHING, BECAUSE 'never empty'? or maybe because always length 5..
next = null;
p = null;
}
}
/* class sp(SnakePoint Class)
*/
class sp
{
int direction;
int length;
int x;
int y;
public sp (int x, int y)
{
this.x = x;
this.y = y;
}
public int lengthR ()
{
return length;
}
public sp up ()
{
return new sp (x, y - 1);
}
public sp down ()
{
return new sp (x, y + 1);
}
public sp left ()
{
return new sp (x - 1, y);
}
public sp right ()
{
return new sp (x + 1, y);
}
public int getX ()
{
return x;
}
public int getY ()
{
return y;
}
public boolean equals (sp s)
{
return s.x == x && s.y == y;
}
}
class Snake
{
SnakeNode tail;
SnakeNode head;
// sp tail;
// sp head;
char direction;
int length;
public Snake ()
{
head = new SnakeNode ();
head.next = new SnakeNode ();
head.next.next = new SnakeNode ();
head.next.next.next = new SnakeNode ();
tail = new SnakeNode ();
head.next.next.next = tail;
length = 5;
//head.
// head.p = new sp (50, 50);
// head.next.p = new sp (50, 51);
// head.next.next.p = new sp (50, 52);
// head.next.next.next.p = new sp (50, 53);
direction = 'D';
//head=null;
// tail=null;
}
public boolean add ()
{
SnakeNode newNode = new SnakeNode ();
newNode.next = null;
if (empty ())
{
tail = newNode;
head = newNode;
}
else
{
head.next = newNode;
head = newNode;
}
return true;
}
public void grow ()
{
SnakeNode x = new SnakeNode ();
head.next = x;
head = x;
}
public void remove ()
{
if (!empty ())
tail = tail.next;
}
public void changeDirection (char s)
{
direction = s;
}
public int length ()
{
return length;
}
public SnakeNode head ()
{
return head;
}
public boolean empty ()
{
return tail == null && head == null;
}
public boolean onBody (sp q)
{
SnakeNode k = tail;
while (tail != head)
if (k.p.equals (q))
return true;
k = k.next;
return false;
}
public boolean onSnake (sp q)
{
SnakeNode k = tail;
if (q.equals (head.p))
return true;
while (tail != head)
if (k.p.equals (q))
return true;
k = k.next;
return false;
}
/* method alive
returns:boolean, true if snake is within bounds, and head is not on the body
*/
public boolean alive ()
{
if (!(head.p.getX () >= 0 && head.p.getY () >= 0 && head.p.getX () <= 700 && head.p.getY () <= 700))
return false;
else
{
SnakeNode l = tail;
while (l != head)
{
if (l.p.equals (head.p))
return false;
l = l.next;
}
}
return true;
}
public void move ()
{
grow ();
remove ();
}
}
-
updated version:
// The "Snake" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends Applet implements KeyListener, Runnable
{
Thread t;
int speed;
int x;
int y;
boolean eating;
int food;
Snake s;
Graphics g;
Color c;
/* start gets you to the run
*/
// public void run ()
// {
// food = 0;
// eating = false;
// while (s.alive ())
// {
// System.out.println ("hi");
// }
// }
public void run ()
{
int theX;
int theY;
theX = (int) (Math.random () * 700);
theY = (int) (Math.random () * 700);
x = (int) (Math.random () * 9) + 1;
sp foodP = new sp (theX, theY);
while (s.onSnake (foodP))
{
theX = (int) (Math.random () * 700);
theY = (int) (Math.random () * 700);
foodP = new sp (theX, theY);
}
food = 0;
eating = false;
g = getGraphics ();
g.clearRect (0, 800, 0, 800);
// while (s.alive ())
// {
// g=getGraphics();
// //if (food == 0)
// //{
// g.drawString ("" + x, foodP.getX (), foodP.getY ());
// //}
//
// }
// int x = 0;
// while (s.alive ())
// {
//
// // if (food == 0)
// // sp x = new fp ();
// int theX;
// int theY;
// x = (int) (Math.random () * 9) + 1;
// sp q = new sp (theX, theY);
// while (s.onSnake (q))
// {
// theX = (int) (Math.random () * 700);
// theY = (int) (Math.random () * 700);
// q = new sp (theX, theY);
// }
if (eating)
{
s.grow ();
//s.move()..?
food--;
//s.drawString(750,750,"length of snake: "+s.length());
//for (double delay=0;double<speed;double++);
}
// else
// {
// g = getGraphics ();
// // s.move ();
// // g.drawString ("" + x,theX, theY);
// }
// if (food == 0)
// eating = false;
// if (s.onSnake (z))
// eating = true;
// //SnakeNode k = s.head;
// //sp u = new sp (500, 500);
//
// g = getGraphics ();
// // g.drawRect (s.head.p.x * 10, s.head.p.y * 10, 9, 9);
// // s.grow ();
// //System.out.println ("ITS X" + s.head.p.x);
// // System.out.println ("ITS Y" + s.head.p.y);
// // g.drawRect (s.head.p.x*10, s.head.p.y*10, 9, 9);
// // g = getGraphics ();
// // s.draw (g);
// // while (k != s.tail)
// // {
// // k = k.next;
// //
// // }
//
// g.drawString ("length " + s.length, 700, 300);
//}
//else
//g.clearRect (0, 800, 0, 800);
// }
// }
//}
//create a food point p SEMICOLON TERE RIGHT NOW...
// }
}
public void init ()
{
s = new Snake ();
setSize (800, 800);
t = null;
speed = 10000000;
addKeyListener (this);
reset (); //
run ();
}
public void paint (Graphics g)
{
// Place the body of the drawing method here
} // paint method
public void drawGrid ()
{
g = getGraphics ();
int Locx = 0;
int Locy = 0;
int Locx1 = 650;
int Locy1 = 0;
c = Color.green;
g.setColor (c);
while (Locx < 700)
{
sp x = new sp (Locx, Locy);
Locx++;
g.drawRect (Locx * 10, Locy * 10, 9, 9);
}
Locx = 0;
while (Locy < 700)
{
sp x = new sp (Locx, Locy);
Locy++;
g.drawRect (Locx * 10, Locy * 10, 9, 9);
}
Locy = 0;
while (Locy < 700)
{
sp x = new sp (Locx, Locy);
Locy++;
g.drawRect (690, Locy * 10, 9, 9);
}
Locx = 0;
while (Locx < 700)
{
sp x = new sp (Locx, Locy);
Locx++;
g.drawRect (Locx * 10, 690, 9, 9);
}
// Locx = 0;
// Locy1 = 0;
// while (Locy1 < 700)
// {
// sp x = new sp (Locx, Locy);
// Locy++;
// g.drawRect (Locx * 10, Locy * 10, 9, 9);
// }
}
public void reset ()
{
if (t != null)
t.stop ();
t = new Thread (this);
t.start ();
g = getGraphics ();
plot (g, c);
drawGrid ();
}
public void keyPressed (KeyEvent e)
{
int k = e.getKeyCode ();
if (k == KeyEvent.VK_DOWN)
s.changeDirection ('D');
else if (k == KeyEvent.VK_UP)
s.changeDirection ('U');
else if (k == KeyEvent.VK_LEFT)
s.changeDirection ('L');
else if (k == KeyEvent.VK_RIGHT)
s.changeDirection ('R');
}
public void keyTyped (KeyEvent e)
{
char k = e.getKeyChar ();
if (k == 's')
reset ();
else if (k == 'z')
speed += 100000;
else if (k == 'x')
speed -= 100000;
}
public void keyReleased (KeyEvent e)
{
}
public void plot (Graphics g, Color c)
{
g = getGraphics ();
c = Color.green;
g.setColor (c);
g.setColor (Color.blue);
g.drawRect (x * 10, y * 10, 9, 9);
}
public sp up ()
{
sp z = new sp (x, y + 1);
return z;
}
public sp down ()
{
sp z = new sp (x, y - 1);
return z;
}
public sp left ()
{
sp z = new sp (x - 1, y);
return z;
}
public sp right ()
{
sp z = new sp (x + 1, y + 1);
return z;
}
} // Snake class
class SnakeNode
{
sp p;
SnakeNode next;
public SnakeNode ()
{
//IT MUST BE SET TO SOMETHING, BECAUSE 'never empty'? or maybe because always length 5..
next = null;
p = null;
}
public SnakeNode (sp z)
{
p = z;
if (z.direction == 'D')
next = new SnakeNode (z.down ());
else if (z.direction == 'U')
next = new SnakeNode (z.up ());
else if (z.direction == 'L')
next = new SnakeNode (z.left ());
else if (z.direction == 'R')
next = new SnakeNode (z.right ());
//next = null;
}
}
/* class sp(SnakePoint Class)
*/
class sp
{
int direction;
int length;
int x;
int y;
public sp (int x, int y)
{
this.x = x;
this.y = y;
}
public int lengthR ()
{
return length;
}
public sp up ()
{
return new sp (x, y - 1);
}
public sp down ()
{
return new sp (x, y + 1);
}
public sp left ()
{
return new sp (x - 1, y);
}
public sp right ()
{
return new sp (x + 1, y);
}
public int getX ()
{
return x;
}
public int getY ()
{
return y;
}
public boolean equals (sp s)
{
return s.x == x && s.y == y;
}
}
class Snake
{
SnakeNode tail;
SnakeNode head;
// sp tail;
// sp head;
char direction;
int length;
public Snake ()
{
// head = new SnakeNode ();
sp q = new sp (100, 100);
head = new SnakeNode (q);
direction = 'D';
q = q.down ();
head.next = new SnakeNode (q);
q = q.down ();
head.next.next = new SnakeNode (q);
q = q.down ();
head.next.next.next = new SnakeNode (q);
q = q.down ();
tail = new SnakeNode (q);
// head.next = new SnakeNode ();
// head.next.next = new SnakeNode ();
// head.next.next.next = new SnakeNode ();
// tail = new SnakeNode ();
// head.next.next.next = tail;
length = 5;
//head.
// head.p = new sp (50, 50);
// head.next.p = new sp (50, 51);
// head.next.next.p = new sp (50, 52);
// head.next.next.next.p = new sp (50, 53);
//head=null;
// tail=null;
}
public boolean add (sp s)
{
SnakeNode newNode = new SnakeNode (s);
newNode.next = null;
if (empty ())
{
tail = newNode;
head = newNode;
}
else
{
head.next = newNode;
head = newNode;
}
return true;
}
public void grow ()
{
if (direction == 'D')
add (head.p.down ());
else if (direction == 'U')
add (head.p.up ());
else if (direction == 'L')
add (head.p.left ());
else if (direction == 'R')
add (head.p.right ());
}
public void draw (Graphics g)
{
SnakeNode k = tail;
while (k != head)
{
g.setColor (Color.red);
g.drawRect (k.p.x * 10, k.p.y * 10, 9, 9);
k = k.next;
}
}
public void remove ()
{
if (!empty ())
tail = tail.next;
}
public void changeDirection (char s)
{
direction = s;
}
public int length ()
{
return length;
}
public SnakeNode head ()
{
return head;
}
public boolean empty ()
{
return tail == null && head == null;
}
public boolean onBody (sp q)
{
SnakeNode k = tail;
while (k != null && k != head)
{
if (k.p.equals (q))
return true;
k = k.next;
}
return false;
}
public boolean onSnake (sp q)
{
SnakeNode k = tail;
if (q.equals (head.p))
return true;
while (k != null && k != head)
{
if (k.p.equals (q))
return true;
k = k.next;
}
return false;
}
/* method alive
returns:boolean, true if snake is within bounds, and head is not on the body
*/
public boolean alive ()
{
if (!(head.p.getX () >= 0 && head.p.getY () >= 0 && head.p.getX () <= 700 && head.p.getY () <= 700))
return false;
else
{
SnakeNode l = tail;
while (l != head.next && l.next != null)
{
if (l.p.equals (head.p))
return false;
if (l.next != null)
l = l.next;
}
}
return true;
}
public void move ()
{
grow ();
remove ();
}
}
-
Well... nothing's being drawn because you have nothing in the paint( Graphics ) method... add drawGrid(); in there to start. And i don't see anywhere where you'd be drawing the snake..
Similar Threads
-
By Tataroz T. in forum Java
Replies: 1
Last Post: 10-20-2005, 06:19 AM
-
Replies: 1
Last Post: 10-07-2005, 08:51 AM
-
By Charlie Flynn in forum Java
Replies: 3
Last Post: 08-23-2001, 11:01 AM
-
Replies: 0
Last Post: 08-11-2001, 04:53 PM
-
Replies: 0
Last Post: 03-28-2000, 01:52 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks