Hi !!!!
I am a new one in coding in general. My first real project is to do a psp pong. But the last thing I have to do it's the ball and the game is finished. So if anyone of you can do me a little code that only move the ball. I already know how to draw it.
What I need :
1-It bounce on the Y of the psp screen.
2-It give points in the X of the psp screen. (the variable are points and points 2)
3-It bounce on mp and mp2 (the pallets)
Thanks a lot !!!!!
Anyone can help me please ??? (for a psp pong)
Since I don't know anything about your setup of the movement variables, I just suppose you hold the ball position with the variables ballx and bally and the movement vector in movex and movey (which need to be signed ints).1-It bounce on the Y of the psp screen.
At your movement code for the ball, just do something like that:
Code: Select all
ballx += movex;
bally += movey;
if (bally<=0 || bally>=272) movey = -movey;
Could you specify this more? I hardly understand what you want, but by the game logic I would suppose you want the game to increase points for the appropriate player if the ball reaches the a players table end (goes beyond the vertical screen edge)? Well, for that, just do something like that:2-It give points in the X of the psp screen. (the variable are points and points 2)
Code: Select all
if (ballx<=0) {
// player1 on the left let the ball go beyond the screen, so player2 gets 1 point
points2 += 1;
// add ball reset here
}
if (ballx>=480) {
// player2 on the right let the ball go beyond the screen, player1 gets a point
points1 += 1;
// add ball reset here
}
I suppose you mean it should bounce off the player sticks, correct? Then however, it would be neccessary to have at least a little insight in how you manage your player sticks, esp their position.3-It bounce on mp and mp2 (the pallets)
However, it will come down to some simple collision detection, so lets say you have your stick position set by stick1_y, your stick is 5 pixels from the left of the screen and your stick is stick_h pixels long. Then it would be something like that:
Code: Select all
if (ballx==5) {
if (bally>=stick1_y && bally<=stick1_y+stick_h) {
movex = -movex;
}
}
Hi !!!!
Thanks for your fast response.
I put your code and all but the ball dont move :(.
Here's my code
Thanks for your fast response.
I put your code and all but the ball dont move :(.
Here's my code
Code: Select all
int drawball(void)
{
ballx += movex;
bally += movey;
if (bally<=0 || bally>=272) movey = -movey;
if (ballx<=0) {
// player1 on the left let the ball go beyond the screen, so player2 gets 1 point
points2 += 1;
int ballx=136;
int bally=240;
}
if (ballx>=480) {
// player2 on the right let the ball go beyond the screen, player1 gets a point
points += 1;
int ballx=136;
int bally=240;
}
if (ballx==10) {
if (bally>=cy && bally<=cy+25) {
movex = -movex;
}
}
if (ballx==10) {
if (bally>=cy2 && bally<=cy2+25) {
movex = -movex;
}
}
DrawImage(ballblack, ballx, bally, 0, 0, ball, ball2, PAINTSRC);
return;
}
Well, sure it won't move if you define and initialize the ballx and bally variables each time after you did add the movement vector (if that even was initialized before). You have to make ballx and bally global and initalize them at the start of your program to some value, then also initialize the movement vector to something, like movex = 2; movey = 1; These two variables hold how fast and in which direction the ball is moving, so this is essential (and most likely will need some tweaks to get the ball to move at the correct speed).
Also, this part of the code won't work properly
Because it assumes the player2 is at the same x position (ballx==10) as player1, which obviously isn't the case. Change the second ballx==10 to ballx==player2x to fix that.
Code: Select all
static int ballx = 136, bally = 240;
static int movex = 2;
static int movey = 1;
int drawball(void)
{
ballx += movex;
bally += movey;
if (bally<=0 || bally>=272) movey = -movey;
if (ballx<=0) {
// player1 on the left let the ball go beyond the screen, so player2 gets 1 point
points2 += 1;
ballx=136;
bally=240;
movex = 2; // Also reset the movement vector
movey = 1;
}
if (ballx>=480) {
// player2 on the right let the ball go beyond the screen, player1 gets a point
points += 1;
ballx=136;
bally=240;
movex = -2; // Reset movement vector
movey = 1;
}
if (ballx==10) {
if (bally>=cy && bally<=cy+25) {
movex = -movex;
}
}
if (ballx==10) {
if (bally>=cy2 && bally<=cy2+25) {
movex = -movex;
}
}
DrawImage(ballblack, ballx, bally, 0, 0, ball, ball2, PAINTSRC);
return;
}
Also, this part of the code won't work properly
Code: Select all
if (ballx==10) {
if (bally>=cy && bally<=cy+25) {
movex = -movex;
}
}
if (ballx==10) {
if (bally>=cy2 && bally<=cy2+25) {
movex = -movex;
}
}