> I have a main method, pitch, player and a ball
> method.
None of these sound like good names for a method, which would normally include verbs. Player and Ball are perfect candidates for classes.
> The main method creates 1 ball, 1 pitch and
> 2 players.
By the way, if you mean static public void main(String[] args): applets don't have main methods.
> Now this all works fine and i got the
> ball to bounce of the pitch wall no bother. The only
> way that works is if i have the pitch sides as
> static variables.
Are you sure you know what "static" means?
> As you can imagine if the ball is flying all over
> the place the variable y and variable x will change
> so i cant make the ball position change, and the
> same applies with the players. They'll be moving all
> over the place as well.
Sounds like you don't have a consistant data model.
> The probles arrives when i try and tell the ball
> where the players positions are. If i make the x's
> and y's variables public
All attributes should be private.
> for ball and 2 players and
> output them as a string onto the screen, that works
> in main. ex. ("ball x pos :" + ball1.ball_x_pos, 30,
> 50); This seems to work grand.
> But when i try and pass the ball's x and y pos
> into player 1 like so in main,
> player1.did_ball_bounce(ball1.ball_x_pos,
> ball_y_pos), player1 only sees them 2 variables as 0,
Probably because you're not passing them correctly and there's a bug in your code.
> but main can see them as what they are.
> If you want to see my code i'll post it up for
> you guys, but it's a bit long,
Then only post the relevant parts.
package ping;
import java.applet.*;
import java.awt.*;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Color;
public class main extends Applet implements Runnable
{
private Thread th;
private player player1;
private player player2;
private pitch pitch1;
private ball ball1;
private int playerUP = -2;
private int playerDOWN = 2;
private int playerRIGHT = 2;
private int playerLEFT = -2;
private boolean player1UP;
private boolean player1DOWN;
private boolean player1RIGHT;
private boolean player1LEFT;
private boolean player2UP;
private boolean player2DOWN;
private boolean player2RIGHT;
private boolean player2LEFT;
private Image dbImage;
private Graphics dbg;
int check = 0;
public void init()
{
setBackground(Color.black);
setForeground(Color.white);
player1 = new player(54,160);
player2 = new player(754,160);
pitch1 = new pitch();
ball1 = new ball();
}
public void start()
{
th = new Thread(this);
th.start();
}
public void run()
{
while(true)
{
ball1.move_ball();
repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
}
}
}
public boolean keyDown(Event e, int key)
{
return true;
}
public boolean keyUp(Event e, int key)
{
return true;
}
public void update(Graphics g)
{
if(dbImage == null)
{
dbImage = createImage(1210,1010);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0,0,getSize().width, this.getSize().height);
dbg.setColor(getForeground());
dbg.drawString("player1 top" + player1.player_top, 50, 50);
dbg.drawString("player1 bottom" + player1.player_bottom, 50, 60);
dbg.drawString("player1 left " + player1.player_left, 50, 70);
dbg.drawString("player1 right " + player1.player_right, 50,80);
dbg.drawString("Ball_top " + ball1.ball_top, 400, 50);
dbg.drawString("ball_bottom " + ball1.ball_bottom, 400, 60);
dbg.drawString("ball_left " + ball1.ball_left, 400, 70);
dbg.drawString("ball_right " + ball1.ball_right, 400, 80);
dbg.drawString("player2 top" + player2.player_top, 700, 50);
dbg.drawString("player2 bottom" + player2.player_bottom, 700, 60);
dbg.drawString("player2 left " + player2.player_left, 700, 70);
dbg.drawString("player2 right " + player2.player_right,700,80);
paint(dbg);
g.drawImage(dbImage,0,0,this);
}
public void paint(Graphics g)
{
pitch1.drawPitch(g);
player1.draw_player(g);
player2.draw_player(g);
ball1.draw_ball(g);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
player:
package ping;
import java.awt.Graphics;
import java.awt.Color;
public class player
{
private int x_pos;
private int y_pos;
private int pitch_top = pitch.down_y;
private int pitch_bottom = ((pitch.down_y) + (pitch.height));
private int pitch_left = pitch.in_x;
private int pitch_right = ((pitch.in_x) + pitch.width);
private int player_lenght = 80;
private int player_width = 20;
public int player_top = y_pos;
public int player_bottom = (y_pos + player_lenght);
public int player_left = x_pos;
public int player_right = (x_pos + player_width);
public player(int x, int y)
{
x_pos = x;
y_pos = y;
}
public void move_playerX(int speed)
{
if((x_pos > pitch_left) && (x_pos < pitch_right - player_width))
{
x_pos = x_pos + speed;
}
else if((x_pos == pitch_left) && (speed > 0))
{
x_pos = x_pos + speed;
}
else if((x_pos == pitch_right - player_width) && (speed < 0))
{
x_pos = x_pos + speed;
}
}
public void move_playerY(int speed)
{
if((y_pos > pitch_top) && (y_pos < pitch_bottom - player_lenght))
{
y_pos = y_pos + speed;
}
if((y_pos == pitch_top) && (speed > 0))
{
y_pos = y_pos + speed;
}
if((y_pos == pitch_bottom - player_lenght) && (speed < 0))
{
y_pos = y_pos + speed;
}
}
public void draw_player(Graphics g)
{
update();
g.setColor(Color.white);
g.fillRect(x_pos,y_pos,20,80);
}
public void update()
{
player_top = y_pos;
player_bottom = y_pos + player_lenght;
player_left = x_pos;
player_right = x_pos + player_width;
}
public void ball_hit_player()
{
I really dont no what to put in here in order to get the ball_x and ball_y in order to make it bounce back prop of the player
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ball:
package ping;
import java.awt.Graphics;
import java.awt.Color;
public class ball
{
private static int radius = 20;
private int pitch_top = pitch.pitch_top;
private int pitch_bottom = pitch.pitch_bottom;
private int pitch_left = pitch.pitch_left;
private int pitch_right = pitch.pitch_right;
private int ball_x = ((pitch_left + pitch_right) / 2);
private int ball_y = ((pitch_top + pitch_bottom) / 2);
public int ball_x_speed = -3;
private int ball_y_speed = 0;
public int ball_top = 0;
public int ball_bottom = 0;
public int ball_right = 0;
public int ball_left = 0;
public void move_ball()
{
rules();
ball_x = ball_x + ball_x_speed;
ball_y = ball_y + ball_y_speed;
}
public void draw_ball(Graphics g)
{
update();
g.setColor(Color.white);
g.fillOval(ball_x,ball_y,radius,radius);
}
public void update()
{
ball_top = ball_y;
ball_bottom = ball_y + radius;
ball_left = ball_x;
ball_right = ball_x + radius;
}
public void bounce_of_wall()
{
if((ball_top + ball_y_speed) < pitch_top)
{
y_out_of_bounds(ball_top, ball_y_speed);
}
if((ball_bottom + ball_y_speed) > pitch_bottom)
{
y_out_of_bounds(ball_bottom, ball_y_speed);
}
if((ball_right + ball_x_speed) > pitch_right)
{
x_out_of_bounds(ball_right, ball_x_speed);
}
if((ball_left + ball_x_speed) < pitch_left)
{
x_out_of_bounds(ball_left,ball_x_speed);
}
}
public void x_out_of_bounds(int x, int y)
{
int distance_out_of_bounds = 0;
int side = x;
int speed = y;
distance_out_of_bounds = side + speed;
distance_out_of_bounds = side - distance_out_of_bounds;
ball_x = ball_x + ball_x_speed;
ball_x_speed = ball_x_speed * -1;
}
public void y_out_of_bounds(int x, int y)
{
int distance_out_of_bounds = 0;
int side = x;
int speed = y;
distance_out_of_bounds = side + speed;
distance_out_of_bounds = side - distance_out_of_bounds;
ball_y = ball_y + ball_y_speed;
ball_y_speed = ball_y_speed * -1;
}
public void rules()
{
bounce_of_wall();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pitch:
package ping;
import java.awt.Graphics;
import java.awt.Color;
public class pitch
{
public static int in_x = 4;
public static int down_y = 4;
public static int width = 800;
public static int height = 400;
public static int pitch_top = down_y;
public static int pitch_bottom = (down_y + height);
public static int pitch_left = in_x;
public static int pitch_right = (in_x + width);
public void drawPitch(Graphics g)
{
g.setColor(Color.white);
g.drawRect(in_x,down_y,width,height);
g.drawRect(in_x,down_y,(width/2),height);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
i dont know what to do in order to pass the ball1 x and y pos into player1 and player2. I know all my variables should be private but some are public because i want to see the values in order to debug the problem.
any help would do.
cheers.