PDA

View Full Version : programing java game applet help


Vlan
04-10-2010, 09:45 AM
Hey

I am trying to do a game in java applet. But i need som help.

I basicly wanna make a game there you should watch out for the ball/balls in a specefic area.

1. I want the boll move random in a specefic area.
2. I want another boll/image on the same screen, but i wanna move the second boll/image with my keyboard.
3. When everything abow works, i wanna make so "the second image/boll that you can move with the keyboard are gona watch out for the boll, "so he dosen,t touch the first boll that moves randomly.

Could any one help me plz?




Here is what i have come so far with:

import java.awt.Event;

import java.awt.Graphics;
import java.applet.*;
import java.awt.Color;
import java.util.*;


public class Uppgift_prog extends Applet {
int x=80;
int y=10;
int riktning=0;
boolean ner=true;
Random rnd = new Random();



public void init () {

}
public void paint (Graphics g){
g.drawRect(10, 10, 150, 150);
g.setColor(Color.RED);
g.fillOval(x,y, 10, 10);
for(int i=0;i<10000000;i++);
uppdatera();

}

public void uppdatera() {
System.out.println(riktning);
/*switch(riktning){
case 1:
if(x>10)
x=x-5;
break;
case 2:
if(x<150)
x=x+5;
break;
case 3:
if(y>5)
y=y-5;
break;
case 4:
if(y<150)
y=y+5;
break;



*/
if(ner){
if(y< 150)
y=y+5;
else if(y>=150){
y=y-5;
ner=false;
}

}
else{
if(y>0)
y=y-5;
else if(y<=0){
ner=true;
y=y+5;

}
}
if(ner){
if(x< 150)
x=x+5;
else if(x>=150){
x=x-5;
ner=false;


}
}
else{
if(x>0)
x=x-5;
else if(x<=0){
ner=true;
x=x+5;
}
}




for(int t=0;t<10000000;t++);
repaint ();


}
private int mathrandom() {
// TODO Auto-generated method stub
return 0;
}
public boolean keyDown(Event evt, int key){
switch(key){
case Event.LEFT:
riktning=1;
break;
case Event.RIGHT:
riktning=2;
break;
case Event.UP:
riktning=3;
break;
case Event.DOWN:
riktning=4;
break;


}
uppdatera();
return true;
}
}

cs_student
04-13-2010, 03:59 AM
If you want to detect collision with more than one ball I suggest you look into spatial partitioning for your collision detection alogrithm.

Also, next time you post, please be sure to use the code tags.


Can you explain what the purpose of your current code is? What does it do, why do you do it the way you do?

Also, making the ball move randomly may be more complex than you think. You may want to define some logical path it takes.

For example, if you want the ball to purely move randomly, it's likely to stay very much in the same position as it could move right one pixel, than left one pixel.

You may want to give it a vector for it's velocity, and change increment/decrement the direction/magnitude of the vector at random intervals. (this would also make it easy in implementing a collision handling algorithm).