1 Replies - 1190 Views - Last Post: 06 July 2008 - 06:42 PM Rate Topic: -----

#1 DillonSalsman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 144
  • Joined: 30-October 07

Snake-like game, can't get snake to grow : |

Posted 06 July 2008 - 06:05 PM

Alrighty so this is pretty much a snake clone
Something you should know:
The grid is 20*20 from 0-19 each axis.
Each grid square is 20*20 pixels.

Anyways, I can't figure out a way to make the snake GROW every time it eats a "target"
Any suggestions?
import java.awt.*;
import java.applet.*;
import java.util.Random;

public class Movement extends Applet implements Runnable
{
//the players x/y cords
	int p_x;
	int p_y;
//the players movement speeds
	int x_speed;
	int y_speed;
//dun duh ner
	int score;
//targets x/y
	int t_x;
	int t_y;
//will  be used to generate targets position
	Random rnd;
	//AudioClip fart; 
//whether the game is running or not
	boolean running;
//whether or not you've died
	boolean dead;

	public void init()
	{
		this.setSize(400, 400);
		rnd = new Random();
		//fart = getAudioClip (getCodeBase(), "fart.au");
		p_x = 5;
		p_y = 10;
		x_speed = 0;
		y_speed = 0;
		t_x = rnd.nextInt(20);
		t_y = rnd.nextInt(20);
		score = 0;
		running = false;
		
	}
	public void start ()
	{
		Thread th = new Thread (this);
		th.start ();
	}
	
	public void run()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		
		while(true)
		{
					  //moves player automatically ever sleep cycle 
			p_x += x_speed;
			p_y += y_speed;
					 //checks if player should be.. dead
			if(p_x > 19){x_speed = 0; dead = true; running = false;}
			if(p_y > 19){y_speed = 0; dead = true; running = false;}
			if(p_x < 0) {x_speed = 0; dead = true; running = false;}
			if(p_y < 0) {y_speed = 0; dead = true; running = false;}
			repaint();
			
			try
			{
				//this controls the speed at which the player moves lower number == faster
			Thread.sleep (80);
			}
			catch (InterruptedException ex)
			{
			}
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}
	
	public void paint(Graphics g)
	{	
		//if the player is on the same space as the target
		if(p_x == t_x && p_y == t_y)
		{
			score();
		}
		//if the game has is running
		if(running == false)
		{
			this.setBackground(Color.black);
			g.setColor(Color.WHITE);
			g.drawString("Press SPACE to start", 180, 200);
			//if the game WAS running but the player died.. it lets the player know he died
			if(dead == true)
			{
				g.drawString("YOU LOSE :(", 100, 100);
				p_x = 5;
				p_y = 10;
				moveTarget();
			}
			g.setColor(Color.BLACK);
		}
		else//if the game is being played
		{
			this.setBackground(Color.green);
			//draw target
			g.setColor(Color.RED);
			g.fill3DRect(t_x*20, t_y*20, 20, 20, true);		
			//draw player
			g.setColor(Color.BLACK);
			g.fill3DRect(p_x*20, p_y*20, 20, 20, true);
			//draw score
			g.drawString(String.valueOf(score), 80, 15);
		}
	}
	
	public boolean keyDown(Event e, int key)
	{
		//sets which way the player moves after every thread sleep interval
		if(key == Event.DOWN) {y_speed = 1; x_speed = 0;}
		else if(key == Event.UP){y_speed = -1; x_speed = 0;}
		else if(key == Event.RIGHT){y_speed = 0; x_speed = 1;}
		else if(key == Event.LEFT){y_speed = 0; x_speed = -1;}
		//starts the game if it hasn't started
		if(key == ' ') running = true;
		
		repaint();
		return true;
	}
	
	//moves the target to a random place on the map
	public void moveTarget()
	{
		t_x = rnd.nextInt(20);
		t_y = rnd.nextInt(20);
		//this should keep the target from spawning underneath the player
		if(t_x == p_x && t_y == p_y) moveTarget();
	}
	
	public void score()
	{
		//pretty self explanitory.. add score, play the sound, respawns the target elsewhere
		score += 5;
		//fart.play();
		moveTarget();
	}
}



Edit: Added some more comments and commented out anything to do with the sound file.. its not important enough to upload :P

This post has been edited by DillonSalsman: 06 July 2008 - 06:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Snake-like game, can't get snake to grow : |

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Snake-like game, can't get snake to grow : |

Posted 06 July 2008 - 06:42 PM

Cut & Pasted your code can't make it run
Don't see where you draw your targets
Don't see how you can draw a snake that is not a perfect rectangle
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1