3 Replies - 11281 Views - Last Post: 23 May 2008 - 08:33 AM Rate Topic: -----

#1 Aperos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-May 08

Small Game with KeyListener

Posted 22 May 2008 - 06:03 PM

Okay so for my final project in AP Comp Sci is to build a small working game of Pokemon. Since I don't have a very good teacher, he never taught us advanced programming or tips for example I don't know how to properly use threads. Well I searched through the web teaching myself these things and I think I have a build on the keys and movements(no animations built in yet!!) For those who don't know the character has to be focused in the center of the applet at all times so thats why I move the background image with a black background behind it. Well okay to my problem...I thought I have a build but when I run the applet it freezes my computer. I believe I have a problem with looping but any help for me will be greatly appreciated since this is like 50% of my grade hehe.

Well here is my code:
import java.applet.*;
import java.awt.*;

public class pokemon extends Applet implements Runnable
{ 
	int x_pos = 0;
	int y_pos = 0;
	
	private Image dbImage;
	private Graphics dbg;
	
	int x_speed = 12;
	int y_speed = 12;
	int appletsize_x = 425;
	int appletsize_y = 575;
	
	int tile_horiz = 17;
	int tile_vertic = 23;
	
	AudioClip background;
	Image backImage, mainChar, standUp, standDown, standRight, standLeft;
	
public void init() {
	setBackground (Color.black);
	
	background = getAudioClip (getCodeBase(), "");//would work but no sound file present(TESTED!)
	
	backImage = getImage (getCodeBase (), "images/route1.gif"); 
	
	standDown = getImage (getCodeBase (), "images/mainCharacter/standDown.gif");
	standUp = getImage (getCodeBase (), "images/mainCharacter/standUp.gif");
	standLeft = getImage (getCodeBase (), "images/mainCharacter/standLeft.gif");
	standRight = getImage (getCodeBase (), "images/mainCharacter/standRight.gif");
}

public void start() {
	Thread th = new Thread (this);
	th.start ();
	mainChar = standDown;
}

public void stop() { }

public void destroy() { }

public boolean keyDown (Event e, int key) { 
	// user presses left cursor key 
	if (key == Event.RIGHT){ 
		mainChar = standRight;
		while(x_pos > x_pos - tile_horiz){
			x_pos -= x_speed;
			repaint();
		}
	}
	// user presses right cursor key 
	else if (key == Event.LEFT){ 
		mainChar = standLeft;
		while(x_pos < x_pos + tile_horiz){
			x_pos += x_speed;
			repaint();
		}
	//user presses up cursor key
	}else if (key == Event.UP){ 
		mainChar = standUp;
		while(y_pos > y_pos - tile_vertic){
			y_pos -= y_speed;
			repaint();
		}
	//user presses down cursor key
	}else if (key == Event.DOWN){ 
		mainChar = standDown;
		while(y_pos < y_pos + tile_vertic){
			y_pos += y_speed;
			repaint();
		}
	}else{ 
		// testing other keys
		System.out.println ("Character: " + (char)key + " Integer Value: " + key);
	}

	return true;

}

public void run () { 
	Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
	while (true)
	{
		try
		{
			Thread.sleep (20);
		}
		catch (InterruptedException ex)
		{
			
		}
		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
	}

}
public void update (Graphics g)
{
	if (dbImage == null)
	{
		dbImage = createImage (this.getSize().width, this.getSize().height);
		dbg = dbImage.getGraphics ();
	}

	dbg.setColor (getBackground ());
	dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

	dbg.setColor (getForeground());
	paint (dbg);
	
	g.drawImage (dbImage, 0, 0, this);
}

public void paint (Graphics g) {
	g.drawImage (backImage, x_pos, y_pos, this);
	
	g.drawImage(mainChar, 217, 287, this);
	}
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Small Game with KeyListener

#2 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Small Game with KeyListener

Posted 22 May 2008 - 06:15 PM

What is the use of your thread ?

It does nothing beside:
- setting its priority to the lowest one.
- sleeping 20 millisec
- setting its priority to the highest possible.
- sleeping 20 millisec
- setting its priority to the highest possible.
- sleeping 20 millisec
- setting its priority to the highest possible.
- sleeping 20 millisec
- setting its priority to the highest possible.
...
kind of useless
Was This Post Helpful? 0
  • +
  • -

#3 Aperos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-May 08

Re: Small Game with KeyListener

Posted 23 May 2008 - 07:41 AM

Exactly. I was never taught how to use threads like I said in my explanation and I figured it wasnt doing anything but every basic game I look up for help always has a thread in it so I don't know. That's why I am asking for some help/suggestions.
Was This Post Helpful? 0
  • +
  • -

#4 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Small Game with KeyListener

Posted 23 May 2008 - 08:33 AM

Usually the thread would contain timed updates so that these can be separate from the main thread, and won't interfere, so if your character should walk across the screen, and you want to perform a repeated task to update positional information, then you could place this inside the thread's run method, and then time the update method calls from in there to control your frame rate. If you don't have any timed updates, then you probably don't need an extra thread unless you wanted to use it for one time initialisation tasks such as loading sound and images, which is a good idea since it means that if the images/sounds fail to load, you can handle it, and display something in the applet (as the separate thread won't hang the whole thing waiting for images that aren't there) if that makes sense!

There are some good examples here:
http://www.softlooku...games/index.asp

This post has been edited by Ellie: 23 May 2008 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1