-
Java applet "game" help
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;
}
}
-
You've posted what you want, you've posted code, but you really haven't asked a specific question. No one is going to write this for you or augment your code -- if you need that, check out RentACoder.com, but we'll be more than happy to answer questions you may be having with your code. Best of luck.
-
Then my question will be, How does i do so the boll will move randomly?
at the moment my ball just move a specefi route. I want it to move random?
can any one help me?
-
depends how you define "randomly"... one thing you can do to start with is this:
instead of having "x-5", "y+5" everywhere, have "x+deltaX", "y+deltaY". Then, in your paint() method, before calling uppdatera(), have
deltaX = Math.random (5) + 3; // so that it's random between 3 and 7
deltaY = Math.random (5) + 3;