Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <allegro.h>
- #include "pong.h"
- #define DOWN_RIGHT 0
- #define UP_RIGHT 1
- #define DOWN_LEFT 2
- #define UP_LEFT 3
- DATAFILE *pong_datafile=NULL;
- int bar1_y=30;
- int bar2_y=430;
- int ball_x;
- int ball_y;
- int direction=0;
- int score_p1=0;
- int score_p2=0;
- BITMAP *buffer;
- int speed=2;
- int amout_of_hits=0;
- int up_ran_speed=0;
- void boing(void)
- {
- play_sample(pong_datafile[pong_boing].dat,255,255,1000,0);
- return;
- }
- int random_direction(void)
- {
- up_ran_speed=random()%3;
- return random()%3;
- }
- void move_ball(void)
- {
- if(amout_of_hits%10==0 && amout_of_hits!=0)
- {
- speed++;
- amout_of_hits++;
- }
- switch(direction)
- {
- case DOWN_RIGHT:
- ball_x+=speed+up_ran_speed;
- ball_y+=speed;
- if(ball_x>600)
- {
- if((ball_y>=bar2_y-40) &&( ball_y<=bar2_y+65))
- {
- direction=DOWN_LEFT;
- amout_of_hits++;
- boing();
- }
- else
- {
- score_p1++;
- ball_x=SCREEN_W/2;
- ball_y=35;
- direction=random_direction();
- }
- }
- else
- {
- if(ball_y>420)
- direction=UP_RIGHT;
- }
- break;
- case UP_LEFT:
- ball_x-=speed;
- ball_y-=speed+up_ran_speed;
- if(ball_x<5)
- {
- if((ball_y>=bar1_y-40) &&( ball_y<=bar1_y+65))
- {
- direction=UP_RIGHT;
- amout_of_hits++;
- boing();
- }
- else
- {
- direction=random_direction();
- score_p2++;
- ball_x=SCREEN_W/2;
- ball_y=35;
- }
- }
- else
- {
- if(ball_y<30)
- direction=DOWN_LEFT;
- }
- break;
- case UP_RIGHT:
- ball_x+=speed+up_ran_speed;
- ball_y-=speed;
- if(ball_x>600)
- {
- if((ball_y>=bar2_y-40) &&( ball_y<=bar2_y+65))
- {
- direction=UP_LEFT;
- amout_of_hits++;
- boing();
- }
- else
- {
- score_p1++;
- ball_x=SCREEN_W/2;
- ball_y=35;
- direction=random_direction();
- }
- }
- else
- if(ball_y<30)
- direction=DOWN_RIGHT;
- break;
- case DOWN_LEFT:
- ball_x-=speed;
- ball_y+=speed+up_ran_speed;
- if(ball_x<5)
- {
- if((ball_y>=bar1_y-40) &&( ball_y<=bar1_y+65))
- {
- direction=DOWN_RIGHT;
- amout_of_hits++;
- boing();
- }
- else
- {
- score_p2++;
- ball_x=SCREEN_W/2;
- ball_y=35;
- direction=random_direction();
- }
- }
- else
- {
- if(ball_y>420)
- direction=UP_LEFT;
- }
- }
- draw_rle_sprite(buffer,pong_datafile[pong_ball].dat,ball_x,ball_y);
- return;
- }
- void key_respond(void)
- {
- poll_joystick();
- if(key[KEY_DOWN])
- bar1_y+=6+up_ran_speed;
- if(key[KEY_UP])
- bar1_y-=6+up_ran_speed;
- if(bar1_y<=30)
- bar1_y=30;
- if(bar1_y>430)
- bar1_y=430;
- if(joy_down || key[KEY_Z])
- bar2_y+=6+up_ran_speed;
- if(joy_up || key[KEY_A])
- bar2_y-=6+up_ran_speed;
- if(bar2_y<=30)
- bar2_y=30;
- if(bar2_y>430)
- bar2_y=430;
- draw_rle_sprite(buffer,pong_datafile[pong_bar].dat,0,bar1_y);
- draw_rle_sprite(buffer,pong_datafile[pong_bar].dat,635,bar2_y);
- return;
- }
- void pong_game(void)
- {
- direction=random_direction();
- while(!key[KEY_ESC])
- {
- move_ball();
- key_respond();
- textout(buffer,pong_datafile[pong_text].dat,"Player 1 Score:",150,0,254);
- textout(buffer,pong_datafile[pong_text].dat,itoa(score_p1,NULL,10),text_length(pong_datafile[pong_text].dat,"Player 1 Score:")+150,0,10);
- textout(buffer,pong_datafile[pong_text].dat,"Player 2 Score:",350,0,254);
- textout(buffer,pong_datafile[pong_text].dat,itoa(score_p2,NULL,10),text_length(pong_datafile[pong_text].dat,"Player 2 Score:")+350,0,10);
- textout(buffer,pong_datafile[pong_text].dat,"keyboard",0,0,255);
- textout(buffer,pong_datafile[pong_text].dat,"joystick",640-text_length(pong_datafile[pong_text].dat,"joystick"),0,255);
- line(buffer,0,30,640,30,10);
- blit(buffer,screen,0,0,0,0,640,480);
- clear(buffer);
- }
- return;
- }
- int main(void)
- {
- allegro_init();
- install_timer();
- install_keyboard();
- initialise_joystick();
- install_mouse();
- if((pong_datafile=load_datafile("pong.dat"))==NULL)
- {
- allegro_exit();
- printf("Error loading \"pong.dat\"\n%s\n\n", allegro_error);
- exit(1);
- }
- printf("Setting up the Sound.");
- if(install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL) != 0)
- {
- allegro_exit();
- printf("Error setting up Sound\n%s\n\n", allegro_error);
- }
- printf("Setting up graphics mode 640x480.\n");
- if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0)
- {
- allegro_exit();
- printf("Error setting graphics mode\n%s\n\n", allegro_error);
- exit(1);
- }
- set_pallete(pong_datafile[pong_pal].dat);
- buffer=create_bitmap(640,480);
- clear(buffer);
- ball_x=SCREEN_W/2;
- ball_y=35;
- pong_game();
- allegro_exit();
- if(score_p1>score_p2)
- printf("Player 1 beat Player 2\n");
- else
- printf("Player 2 beat Player 1\n");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement