Skip to content

Instantly share code, notes, and snippets.

@Codeplaza
Created January 5, 2014 11:31
Show Gist options
  • Save Codeplaza/8267214 to your computer and use it in GitHub Desktop.
Save Codeplaza/8267214 to your computer and use it in GitHub Desktop.
Pong - 2 Player Game
#include <allegro.h>
#define DOWN_RIGHT 0
#define UP_RIGHT 1
#define DOWN_LEFT 2
#define UP_LEFT 3
void moveBall(void);
void reserveVerticalDirection(void);
void reserveHorizontalDirection(void);
void respondToKeyboard(void);
void delay();
volatile int ball_x;
volatile int ball_y;
volatile double barL_y;
volatile double barR_y;
volatile int scoreL;
volatile int scoreR;
volatile int direction;
BITMAP *ball;
BITMAP *bar;
BITMAP *buffer;
FONT *fnt;
int main()
{
allegro_init();
install_keyboard();
install_timer();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
ball = load_bitmap("ball.bmp", NULL);
bar = load_bitmap("bar.bmp", NULL);
fnt = load_font("pFont.pcx", NULL, NULL);
buffer = create_bitmap(SCREEN_W, SCREEN_H);
set_pallete(desktop_pallete);
ball_x = SCREEN_W / 2;
ball_y = SCREEN_H / 2;
barL_y = SCREEN_H / 2;
barR_y = SCREEN_H / 2;
scoreL = 0;
scoreR = 0;
srand(time(NULL));
direction = rand() % 4;
install_int(moveBall, 3);
install_int(respondToKeyboard, 10);
while (!key[KEY_ESC])
{
clear_to_color(buffer, makecol(255, 255, 255));
blit(ball, buffer, 0, 0, ball_x, ball_y, ball->w, ball->h);
blit(bar, buffer, 0, 0, 0, barL_y, bar->w, bar->h);
blit(bar, buffer, 0, 0, 620, barR_y, bar->w, bar->h);
line(buffer, 0, 30, 640, 30, makecol(0, 0, 0));
textprintf_ex(buffer, font, 75, 0, makecol(0, 0, 0), -1, "Left Player Score : %d", scoreL);
textprintf_ex(buffer, font, 400, 0, makecol(0, 0, 0), -1, "Right Player Score : %d", scoreR);
textprintf_ex(buffer, font, 200, 20, makecol(0, 0, 0), -1, "Created by C-TOS (CyB3r-t3Rr0r)");
// vsync();
blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
clear_bitmap(buffer);
}
remove_int(moveBall);
remove_int(respondToKeyboard);
destroy_bitmap(ball);
destroy_bitmap(bar);
destroy_bitmap(buffer);
destroy_font(fnt);
return 0;
}
END_OF_MAIN()
void moveBall()
{
switch (direction)
{
case DOWN_RIGHT:
++ball_x;
++ball_y;
break;
case UP_RIGHT:
++ball_x;
--ball_y;
break;
case DOWN_LEFT:
--ball_x;
++ball_y;
break;
case UP_LEFT:
--ball_x;
--ball_y;
break;
}
if (ball_y <= 35 || ball_y >= 448)
reserveVerticalDirection();
/*
if (ball_x <= 0 || ball_x >= 608)
reserveHorizontalDirection();
*/
if (ball_x < 20 && (direction == DOWN_LEFT || direction == UP_LEFT))
{
if (ball_y > (barL_y - 39) && ball_y < (barL_y + 99))
reserveHorizontalDirection();
else if (ball_x <= -20)
{
++scoreR;
ball_x = SCREEN_W / 2;
ball_y = SCREEN_H / 2;
direction = rand() % 4;
delay();
}
}
if (ball_x > 580 && (direction == DOWN_RIGHT || direction == UP_RIGHT))
{
if (ball_y > (barR_y - 39) && ball_y < (barR_y + 99))
reserveHorizontalDirection();
else if (ball_x >= 620)
{
++scoreL;
ball_x = SCREEN_W / 2;
ball_y = SCREEN_H / 2;
direction = rand() % 4;
delay();
}
}
}
void reserveVerticalDirection()
{
if ((direction % 2) == 0)
++direction;
else
--direction;
}
void reserveHorizontalDirection()
{
direction = (direction + 2) % 4;
}
void respondToKeyboard()
{
if (key[KEY_A])
barL_y -= 3;
if (key[KEY_Z])
barL_y += 3;
if (key[KEY_UP])
barR_y -= 3;
if (key[KEY_DOWN])
barR_y += 3;
if (barL_y < 30)
barL_y = 30;
else if (barL_y > 380)
barL_y = 380;
if (barR_y < 30)
barR_y = 30;
else if (barR_y > 380)
barR_y = 380;
}
void delay()
{
long int i;
for (i = 123456789; i >= 0; i--);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment