x

Started by
14 comments, last by maxfire 12 years, 2 months ago
I have never used allegro 5 but I will create some sudo code that you will be able to convert to your needs, il post this tonight for you :). And if you are using a game loop rather than event driven you will probably need a queue.
Advertisement
ok so you have you game loop, I will just lay out how I would do the basic movement. If I was to do the whole thing you wouldnt be learning anythign tongue.png

im not sure how you code your controls work but I will use arrow keys for player 1 and WS for player 2

finally not knowing your framwework means that i will made logical substitutions on how events may be handled

ok well I scratched this together using notepad on my laptop as my computer has died on me so sorry for any syntax errors

main(int)
{
Player player1;
player player2;
While(!end)
{
//events
if(event)
{
string button = event.getButton(); //this is totally made up for this example many frameworks handle this differantly I know SDL has its own event lableing system ( yes string is very bad way of doing this but makes it clearer as an example)
bool upordown = event.upordown(); // again I dont know your framework so making this up to make scence if( button == up || button == down )
{
player1.HandleEvent( button );// each object should know how to handle its own even in my oppinion
}
else if( button == W || button == S )
{
player2.HandleEvent( button, upordown ):
}
else
{
//anything elts like program close/exit for example
}
}
//updates
player1.Update();
player2.Update();

//drawing
player1.Draw();
player2.Draw();
}
return 0;
}

//player class
class Player
{
public:

//contructor and deconstructor
void Update();
void Draw();
void HandleEvent( string button, bool upordown );

private:

int direction;
int speed;
int _VelocityX;
int _VelocityY;
int X;
int Y;
}
void Player::HandleEvent( string button, bool upordown )
{
//w and up handed in the same way for this example, you can create some sort of id system where w and up return the same id
if( button == w || button == up )
{
if( upordown == true )
{
speed = BALL_SPEED; //probably a const or passed in the constructor
}
else
{
speed = 0;
}

direction = 180;//assumes mathamatic cos and sign where 0 is east direction
}
//elts do the same for down with direction as 90//update velocities if speed is 0 ball will stop

_VelocityX = int ( cos( ( _RotationAngle + 0.0 ) * M_PI / 180.0 ) * ( _CurrentMovementSpeed * _FrameDeltaTime ) ) );
_VelocityY = int ( sin( ( _RotationAngle + 0.0 ) * M_PI / 180.0 ) * (_CurrentMovementSpeed * _FrameDeltaTime ) ) );

}
void Player::Update()
{
x += _VelocityX;
y += _VelocityY;
}

void Player::Draw()
{
draw however your frameworks draws
}



This is my take on doing something like this, might be some people that disagree but hope it helps. Note the ball movement can be done in exactly the same way but using collision detection to change direction rather than some event from the keyboard smile.png.
Thank you MaxFire, this is very generous of you. You went above and beyond, and I greatly appreciate it. This is the most help I have ever received from the site...(no offense to the people that answered, I appreciate your help as well). I totally understand that you didn't do the all thing, it is all about the learning experience on my part. Thanks again.
Nps dude happy to help, give us A howler with anything elts
I have a question, was it or is it possible to have ball movement with out classes. Reason I ask is because I haven't started to dive in that area as of yet, soon will. Before you helped me, I used linear equations for ball movement. I want to know if what I did was the right way to go, here is some source code:




int ball_x = 454;
int ball_y = 350;
int ball_rad = 10;
int fx = (ball_y/ball_x) + 1;
int fx1 = (-(ball_y)/ball_x) - 1;
int fx2 = (ball_y/ -(ball_x)) + 1;
int fx3 = (-(ball_y) / -(ball_x)) + 1;


if(!run)
{
if(fx)
{
ball_x = ball_x + 10;
ball_y = ball_y - 10;

}
else if(fx1)
{
ball_x = ball_x + 10;
ball_y = ball_y + 10;

}
else if(fx2)
{
ball_x = ball_x - 10;
ball_y = ball_y - 10;

}
else if(fx3)
{
ball_x = ball_x - 10;
ball_y = ball_y + 10;

}
else
{
rand() * fx * fx1 * fx2 * fx3;



}


al_draw_filled_circle(ball_x, ball_y, ball_rad,al_map_rgb(250,0,0)); // Ball

al_flip_display(); // Helps what has been drawn visible on screen. Switches from blank screen to the actually image
al_clear_to_color(al_map_rgb(0,0,0));
int ball_x = 454;
int ball_y = 350;
int ball_rad = 10;
int fx = (ball_y/ball_x) + 1;
int fx1 = (-(ball_y)/ball_x) - 1;
int fx2 = (ball_y/ -(ball_x)) + 1;
int fx3 = (-(ball_y) / -(ball_x)) + 1;

if(!run)
{
if(fx)
{
ball_x = ball_x + 10;
ball_y = ball_y - 10;
}
else if(fx1)
{
ball_x = ball_x + 10;
ball_y = ball_y + 10;
}
else if(fx2)
{
ball_x = ball_x - 10;
ball_y = ball_y - 10;
}
else if(fx3)
{
ball_x = ball_x - 10;
ball_y = ball_y + 10;
}
else
{
rand() * fx * fx1 * fx2 * fx3;
}

al_draw_filled_circle(ball_x, ball_y, ball_rad,al_map_rgb(250,0,0)); // Ball
al_flip_display(); // Helps what has been drawn visible on screen. Switches from blank screen to the actually image
al_clear_to_color(al_map_rgb(0,0,0));



***Sorry if the source code doesn't appear in the proper format****
yea you can do it without classes, but if your using c++ then you may as use them. If you are not conformal with classes I suggest reading up on them now because they are one of the core foundation principles of nearly every program :).

With your linear equations are you trying to achieve a random direction? it might be slightly over complicating it but if it works then should be fine

Another suggestion for you would be to define a static const for your speed makes it neater and clearer :)

static const int BALL_SPEED = 10;
Velocity += BALL_SPEED;

This topic is closed to new replies.

Advertisement