Jump to content
  • Advertisement
Sign in to follow this  
flump9

Making Pong

This topic is 2555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making pong in C++ and Allegro,and ive managed to make a ball that will bounce off EVERY side.I know i can make them go through the sides to score a point,but i don't know how to make the bats and make them work. What do i have to do?Iv etried experimenting, but that failed.Anyways,here is my code so far:

#include <allegro.h>
#include <cstdlib>

int x = 320;
int y = 240;

int tempX = 320;
int tempY = 240;

int dir = 1;

void moveCircle()
{
tempX = x;
tempY = y;

if(dir == 1 && x !=20 && y !=20){
--x;
--y;
} else if (dir == 2 && x !=20 && y !=460) {
--x;
++y;
} else if (dir == 3 && x !=620 && y !=20) {
++x;
--y;
} else if (dir == 4 && x !=620 && y !=460) {
++x;
++y;
} else {
dir = rand() % 4 + 1;
}

acquire_screen();
circlefill( screen, tempX, tempY, 20, makecol( 0, 0, 0));
circlefill( screen, x, y, 20, makecol( 128, 225, 0));
release_screen();

rest(10);

}

int main()
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

int y1 = 0;
int y2 = 0;

BITMAP *bat = load_bitmap("sprites/bat.bmp", NULL);
BITMAP *bat2 = load_bitmap("sprites/bat.bmp", NULL);

while (!key [KEY_ESC]) {
moveCircle();
}
return 0;
}

END_OF_MAIN ()

Share this post


Link to post
Share on other sites
Advertisement
I will present a skeleton of what the code could look like:

#include <allegro.h>
#include <cstdlib>

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

const int BALL_SIZE = 20;
const int BALL_SPEED = 1;

// TODO: these need values
const int PADDLE_WIDTH = /* ... */;
const int PADDLE_HEIGHT = /* ... */;
const int PADDLE_OFFSET_FROM_WALL = /* ... */;
const int PADDLE_SPEED = 1;

struct Ball
{
int x;
int y;
int vx;
int vy;
};

struct Paddle
{
int x;
int y;
int vy;
};

struct Pong
{
Ball ball;
Paddle left;
Paddle right;
BITMAP *sprite;
};

void updatePaddle(Paddle &paddle, Ball &ball)
{
// TODO:
// * Update paddle position
// * Ensure paddle doesn't leave the screen
// * Paddle/ball collision
}

void reset(Ball &ball)
{
ball.x = WINDOW_WIDTH / 2;
ball.y = WINDOW_HEIGHT / 2;
ball.vx = (rand() > (RAND_MAX / 2)) ? BALL_SPEED : -BALL_SPEED;
ball.vy = (rand() > (RAND_MAX / 2)) ? BALL_SPEED : -BALL_SPEED;
}

void update(Pong &pong)
{
Ball &ball = pong.ball;

// TODO:
// * Move ball
// * Keep ball in Y bounds
// * Detect point, reset ball (and perhaps implement score system)

// Update paddles and check collision
updatePaddle(pong.left, ball);
updatePaddle(pong.right, ball);
}

void draw(const Pong &pong)
{
// TODO:
// * Clear screen
// * Draw ball
// * Draw left paddle
// * Draw right paddle
}

void runAI(Paddle &paddle, const Ball &ball)
{
// TODO: check if ball is above or below paddle, and move accordingly
}

/*
* Returns false if user requests quit
*/
bool controls(Pong &pong)
{
// Alternatively, test with two A.I. players until you have the game working
runAI(pong.left, pong.ball);
runAI(pong.right, pong.ball);

// When the game works, then you can work on applying player input
// to one or both paddles

return !key[KEY_ESC];
}

int main()
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0);

// TODO: error handling on above calls?

BITMAP *sprite = load_bitmap("sprites/bat.bmp", NULL);
if(!sprite)
{
// TODO: error handling
}

Pong pong;

// Initialise sprite
pong.sprite = sprite;

// Initialise ball
reset(pong.ball);

// Initialise left paddle
pong.left.x = PADDLE_OFFSET_FROM_WALL;
pong.left.y = WINDOW_HEIGHT / 2;
pong.left.vy = 0;

// Initialise right paddle
pong.left.x = WINDOW_WIDTH - PADDLE_WIDTH - PADDLE_OFFSET_FROM_WALL;
pong.left.y = WINDOW_HEIGHT / 2;
pong.left.vy = 0;

// Run game
while (controls(pong))
{
update(pong);
draw(pong);
rest(10);
}
return 0;
}
END_OF_MAIN()



A few things to note:

  • Use named constants over magic numbers
  • No global mutable variables
  • Data structures make it clear which data belongs to which object
  • Separating control, rendering and game logic makes everything a little clearer
  • Decompose functions by using helper functions to accomplish specific tasks
  • Note we can then reuse these functions (e.g. ball reset function, or using one function for two paddles)
  • We don't need to load the same bitmap twice, you can just paint it twice
  • Separating the X and Y velocity, rather than having a direction, will make collision resolution simpler (x axis collision? invert vx!)

Share this post


Link to post
Share on other sites
Well, it seems hard. I'm only a begginer, and this is the first thing i'm doing.I know some stuff, but this just seems really hard in my eyes. Do you know any good tutorials?

Share this post


Link to post
Share on other sites
This tutorial covers creating a Pong game in detail, however it uses SFML instead of SDL.

Truth told, this stuff is tricky, you need to put a lot of effort in to learn anything, no tutorial in the world is going to take the learning curve away.

Share this post


Link to post
Share on other sites

Well, it seems hard. I'm only a begginer, and this is the first thing i'm doing.
[/quote]
If you're a beginner, you should start out making simple text based/console programs. Writing a full pong application is actually not a beginner task.

Share this post


Link to post
Share on other sites


Well, it seems hard. I'm only a begginer, and this is the first thing i'm doing.

If you're a beginner, you should start out making simple text based/console programs. Writing a full pong application is actually not a beginner task.
[/quote]

sol like text based games?

Share this post


Link to post
Share on other sites
Sure. An excellent example might be a full text based noughts and crosses game (Tic Tac Toe might be more familiar for those of a North American persuasion). It could have a basic menu, a game where the player players an A.I. of varying difficulty and a two player mode. You could even add save/load features. Does that sound like something you might manage?

To gauge your ability, what is the biggest program you have completed to date? You should always be aiming a bit bigger than that, but not too big.

Share this post


Link to post
Share on other sites
I have made no really complete program in C++, I've just started this RPG,and yes it sounds like i can manage it and i will make this Tic-Tac-Toe first.

Share this post


Link to post
Share on other sites

I have made no really complete program in C++...
[/quote]
If you haven't already, I would then recommend you purchase (or loan from your library) a book. Completing the exercises at the end of each chapter will give you valuable experience in problem solving, problem deconstruction, program design and applying various techniques.

Writing even a moderate program like I proposed is a feat to take pride in as a beginner. If you completed any programs, I would recommend an even simpler program again, the classic "Guess the number" is one of the first truly interactive programs most beginners attempt.

Share this post


Link to post
Share on other sites


I have made no really complete program in C++...

If you haven't already, I would then recommend you purchase (or loan from your library) a book. Completing the exercises at the end of each chapter will give you valuable experience in problem solving, problem deconstruction, program design and applying various techniques.

Writing even a moderate program like I proposed is a feat to take pride in as a beginner. If you completed any programs, I would recommend an even simpler program again, the classic "Guess the number" is one of the first truly interactive programs most beginners attempt.
[/quote]


Well, i've just made a menu on a console, it was quite easy,actually just some while and switch statements.So i have made a simple menu with Play, Help and Quit. Now i just need to make the help screen and then i will do the tic-tac-toe.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!