Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro.h>
- #define DOWN_RIGHT 0
- #define UP_RIGHT 1
- #define DOWN_LEFT 2
- #define UP_LEFT 3
- void moveBall(void);
- void respondToKeyboard(void);
- void reverseVerticalDirection(void);
- void reverseHorizontalDirection(void);
- int ball_x;
- int ball_y;
- int bar1_x;
- int direction;
- BITMAP *ball;
- BITMAP *buffer;
- SAMPLE *boing;
- BITMAP *bar;
- int main(void)
- {
- allegro_init();
- install_keyboard();
- install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
- set_color_depth(16);
- set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
- ball = load_bitmap("ball.bmp", NULL);
- buffer = create_bitmap(SCREEN_W, SCREEN_H);
- boing = load_sample("cartoon008.wav");
- bar = load_bitmap("paddle.bmp", NULL);
- ball_x = SCREEN_W / 2;
- ball_y = SCREEN_H / 2;
- bar1_x = SCREEN_H / 2;
- srand(time(NULL));
- direction = rand() % 4;
- while (!key[KEY_ESC])
- {
- moveBall();
- respondToKeyboard();
- 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, bar1_x, bar->w, bar->h);
- blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
- clear_bitmap(buffer);
- }
- destroy_bitmap(ball);
- destroy_bitmap(bar);
- destroy_bitmap(buffer);
- destroy_sample(boing);
- 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 <= 30 || ball_y >= 440)
- reverseVerticalDirection();
- if (ball_x <= 0 || ball_x >= 600)
- reverseHorizontalDirection();
- }
- void respondToKeyboard()
- {
- if (key[KEY_UP])
- bar1_x += -3;
- if (key[KEY_DOWN])
- bar1_x -= -3;
- if (bar1_x < 0)
- bar1_x = 0;
- else if (bar1_x > 380)
- bar1_x = 380;
- }
- void reverseVerticalDirection()
- {
- if ((direction % 2) == 0)
- ++direction;
- else
- --direction;
- play_sample(boing, 255, 128, 1000, 0);
- }
- void reverseHorizontalDirection()
- {
- direction = (direction + 2) % 4;
- play_sample(boing, 255, 128, 1000, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement