Urgent!
Author |
Message |
xzecution2
|
Posted: Mon Jan 19, 2009 9:39 pm Post subject: Urgent! |
|
|
Hi, so I'm creating a program that needs a ship to have AI and all i need it to do is to be able to move randomly across the x-axis and shoot downwards randomly.
here's an excerpt from my code that makes the ship.
Java: | import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
public class Player2
{
// Declaring integral variables
private int x_pos= 150;
private int y_pos= 150;
private Image xwing;
private int minx = 1, maxx= 900;
private int x_speed = 3;
public Player2 (int x, int y )
{
x_pos = x;
y_pos = y;
}
public Shot generateShot ()
{
Shot shot = new Shot (x_pos, y_pos );
return shot;
}
public void drawPlayer2 (Graphics g )
{
g. setColor(Color. blue);
int [] x_poly = {x_pos, x_pos - 15, x_pos, x_pos + 15};
int [] y_poly = {y_pos, y_pos + 15, y_pos + 10, y_pos + 15};
g. fillPolygon(x_poly, y_poly, 4);
}
} |
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="java"]Code Here[/syntax] |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Mon Jan 19, 2009 9:41 pm Post subject: RE:Urgent! |
|
|
First, use code tags. Second, title your thread descriptively; 'urgent' tells us nothing of your problem. |
|
|
|
|
|
xzecution2
|
Posted: Mon Jan 19, 2009 9:45 pm Post subject: RE:Urgent! |
|
|
sorry about that, I don't think i'll be able to edit it now though since you can't edit posts that have been replied to. |
|
|
|
|
|
DemonWasp
|
Posted: Tue Jan 20, 2009 1:02 am Post subject: RE:Urgent! |
|
|
I'm assuming that you have:
Player (basic player, not used on its own)
HumanPlayer extends Player (the human, controlled by the keyboard)
AIPlayer extends Player (the computer)
Then you need to add a method (let's call it "update") to Player, overridden by AIPlayer, which runs the AI to determine what to do. In HumanPlayer, update() checks the keyboard for input and moves that player.
Then your main game loop looks like this:
code: | loop
for ( Player player : Players ) {
player.update();
}
for ( Player player : Players ) {
player.draw ( g );
}
end loop |
|
|
|
|
|
|
|
|