Advertisement
__beka

Untitled

Jan 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <allegro.h>
  4.  
  5. #include "pong.h"
  6.  
  7. #define DOWN_RIGHT 0
  8. #define UP_RIGHT   1
  9. #define DOWN_LEFT  2
  10. #define UP_LEFT    3
  11.  
  12. DATAFILE *pong_datafile=NULL;
  13.  
  14. int bar1_y=30;      
  15. int bar2_y=430;      
  16. int ball_x;          
  17. int ball_y;          
  18. int direction=0;    
  19.  
  20. int score_p1=0;      
  21. int score_p2=0;      
  22.  
  23. BITMAP *buffer;    
  24.  
  25. int speed=2;        
  26.  
  27. int amout_of_hits=0;
  28.  
  29. int up_ran_speed=0;  
  30.  
  31.  
  32. void boing(void)
  33. {
  34.    play_sample(pong_datafile[pong_boing].dat,255,255,1000,0);
  35.    return;
  36. }
  37.  
  38.  
  39. int random_direction(void)
  40. {
  41.    up_ran_speed=random()%3;
  42.    return random()%3;
  43. }
  44.  
  45.  
  46. void move_ball(void)
  47. {
  48.  
  49.    if(amout_of_hits%10==0 && amout_of_hits!=0)
  50.    {
  51.              speed++;
  52.              amout_of_hits++;
  53.    }
  54.  
  55.    
  56.    switch(direction)
  57.    {
  58.       case DOWN_RIGHT:
  59.            ball_x+=speed+up_ran_speed;
  60.            ball_y+=speed;
  61.            if(ball_x>600)
  62.            {
  63.               if((ball_y>=bar2_y-40) &&( ball_y<=bar2_y+65))
  64.               {
  65.                        direction=DOWN_LEFT;
  66.                        amout_of_hits++;
  67.                        boing();
  68.               }
  69.               else
  70.               {
  71.                        score_p1++;
  72.                        ball_x=SCREEN_W/2;
  73.                        ball_y=35;
  74.                        direction=random_direction();
  75.               }
  76.            }
  77.            else
  78.            {
  79.  
  80.                  if(ball_y>420)
  81.                             direction=UP_RIGHT;
  82.            }
  83.  
  84.  
  85.  
  86.            break;
  87.       case UP_LEFT:
  88.            ball_x-=speed;
  89.            ball_y-=speed+up_ran_speed;
  90.            if(ball_x<5)
  91.            {
  92.               if((ball_y>=bar1_y-40) &&( ball_y<=bar1_y+65))
  93.               {
  94.                        direction=UP_RIGHT;
  95.                        amout_of_hits++;
  96.                        boing();
  97.               }
  98.               else
  99.               {
  100.                        direction=random_direction();
  101.                        score_p2++;
  102.                        ball_x=SCREEN_W/2;
  103.                        ball_y=35;
  104.  
  105.               }
  106.            }
  107.            else
  108.            {
  109.             if(ball_y<30)
  110.                         direction=DOWN_LEFT;
  111.            }
  112.            break;
  113.       case UP_RIGHT:
  114.            ball_x+=speed+up_ran_speed;
  115.            ball_y-=speed;
  116.            if(ball_x>600)
  117.            {
  118.                if((ball_y>=bar2_y-40) &&( ball_y<=bar2_y+65))
  119.                {
  120.                        direction=UP_LEFT;
  121.                        amout_of_hits++;
  122.                        boing();
  123.                }
  124.                else
  125.                {
  126.                         score_p1++;
  127.                         ball_x=SCREEN_W/2;
  128.                         ball_y=35;
  129.                         direction=random_direction();
  130.                }
  131.            }
  132.            else
  133.            if(ball_y<30)
  134.                         direction=DOWN_RIGHT;
  135.            break;
  136.       case DOWN_LEFT:
  137.            ball_x-=speed;
  138.            ball_y+=speed+up_ran_speed;
  139.            if(ball_x<5)
  140.            {
  141.                if((ball_y>=bar1_y-40) &&( ball_y<=bar1_y+65))
  142.                {
  143.                         direction=DOWN_RIGHT;
  144.                        amout_of_hits++;
  145.                        boing();
  146.                }
  147.                else
  148.                {
  149.                         score_p2++;
  150.                         ball_x=SCREEN_W/2;
  151.                         ball_y=35;
  152.                         direction=random_direction();
  153.                }
  154.            }
  155.            else
  156.            {
  157.                if(ball_y>420)
  158.                         direction=UP_LEFT;
  159.            }
  160.  
  161.    }
  162.  
  163.    draw_rle_sprite(buffer,pong_datafile[pong_ball].dat,ball_x,ball_y);
  164.  
  165.    return;
  166. }
  167.  
  168.  
  169. void key_respond(void)
  170. {
  171.      
  172.      poll_joystick();
  173.  
  174.  
  175.     if(key[KEY_DOWN])
  176.        bar1_y+=6+up_ran_speed;
  177.  
  178.     if(key[KEY_UP])
  179.        bar1_y-=6+up_ran_speed;
  180.  
  181.     if(bar1_y<=30)
  182.        bar1_y=30;
  183.  
  184.     if(bar1_y>430)
  185.        bar1_y=430;
  186.  
  187.     if(joy_down || key[KEY_Z])
  188.        bar2_y+=6+up_ran_speed;
  189.  
  190.     if(joy_up || key[KEY_A])
  191.        bar2_y-=6+up_ran_speed;
  192.  
  193.     if(bar2_y<=30)
  194.        bar2_y=30;
  195.  
  196.     if(bar2_y>430)
  197.        bar2_y=430;
  198.  
  199.    
  200.     draw_rle_sprite(buffer,pong_datafile[pong_bar].dat,0,bar1_y);
  201.     draw_rle_sprite(buffer,pong_datafile[pong_bar].dat,635,bar2_y);
  202.  
  203.  
  204.    return;
  205. }
  206.  
  207.  
  208. void pong_game(void)
  209. {
  210.    
  211.    direction=random_direction();
  212.  
  213.  
  214.    while(!key[KEY_ESC])
  215.    {
  216.      
  217.       move_ball();
  218.      
  219.       key_respond();
  220.  
  221.      
  222.       textout(buffer,pong_datafile[pong_text].dat,"Player 1 Score:",150,0,254);
  223.       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);
  224.       textout(buffer,pong_datafile[pong_text].dat,"Player 2 Score:",350,0,254);
  225.       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);
  226.  
  227.       textout(buffer,pong_datafile[pong_text].dat,"keyboard",0,0,255);
  228.       textout(buffer,pong_datafile[pong_text].dat,"joystick",640-text_length(pong_datafile[pong_text].dat,"joystick"),0,255);
  229.  
  230.      
  231.       line(buffer,0,30,640,30,10);
  232.  
  233.    
  234.       blit(buffer,screen,0,0,0,0,640,480);
  235.  
  236.      
  237.       clear(buffer);
  238.    }
  239.    return;
  240. }
  241.  
  242. int main(void)
  243. {
  244.  
  245.  allegro_init();
  246.  
  247.  
  248.  install_timer();
  249.  
  250.  
  251.  install_keyboard();
  252.  
  253.  
  254.  
  255.  initialise_joystick();
  256.  
  257.  
  258.  install_mouse();
  259.  
  260.  
  261.  if((pong_datafile=load_datafile("pong.dat"))==NULL)
  262.  {
  263.          
  264.          allegro_exit();
  265.          
  266.          printf("Error loading \"pong.dat\"\n%s\n\n", allegro_error);
  267.          exit(1);
  268.  }
  269.  
  270.  
  271.  printf("Setting up the Sound.");
  272.  
  273.  if(install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL) != 0)
  274.  {
  275.          
  276.          allegro_exit();
  277.          
  278.          printf("Error setting up Sound\n%s\n\n", allegro_error);
  279.      
  280.  
  281.  }
  282.  
  283.  
  284.  printf("Setting up graphics mode 640x480.\n");
  285.  
  286.  if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0)
  287.  {
  288.        
  289.          allegro_exit();
  290.          
  291.          printf("Error setting graphics mode\n%s\n\n", allegro_error);
  292.          exit(1);
  293.  }
  294.  
  295.  
  296.  
  297.  set_pallete(pong_datafile[pong_pal].dat);
  298.  
  299.  
  300.  buffer=create_bitmap(640,480);
  301.  clear(buffer);
  302.  
  303.  
  304.  ball_x=SCREEN_W/2;
  305.  ball_y=35;
  306.  
  307.  
  308.  pong_game();
  309.  
  310.  
  311.  allegro_exit();
  312.  
  313.  
  314.  if(score_p1>score_p2)
  315.             printf("Player 1 beat Player 2\n");
  316.  else
  317.             printf("Player 2 beat Player 1\n");
  318.  
  319.  return;
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement