Ember

Pong()

Jul 4th, 2011
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A pong game using no objects
  2.  
  3. //Initialize
  4. time = current_time;
  5. time_previous = time;
  6. ball_x = room_width/2;
  7. ball_y = room_height/2;
  8. ball_xspeed = choose(-2,2);
  9. ball_yspeed = choose(-1,1);
  10. //ball_direction = choose(45,135,225,315);
  11. p1_x = 16;
  12. p2_x = room_width-16;
  13. p1_y = 0;//room_height/2;
  14. p2_y = room_height/2;
  15. p1_h = 32;
  16. p2_h = 32;
  17. p1_s = 0;
  18. p2_s = 0;
  19. frames = 60;
  20. started = false;
  21. f_game = font_add("Fixedsys",8,false,false,0,255);
  22. f_ball = font_add("Fixedsys",32,false,false,0,255);
  23. fade = 1;
  24.  
  25. draw_set_color(c_white);
  26. draw_set_halign(fa_center);
  27. draw_set_valign(fa_middle);
  28.  
  29. //Sound
  30. if file_exists("C:\Windows\Media\Speech Misrecognition.wav") {s_hit = sound_add("C:\Windows\Media\Speech Misrecognition.wav",0,1); sounds = true;}
  31. else if file_exists("C:\Windows\Media\Windows Information Bar.wav") {s_hit = sound_add("C:\Windows\Media\Windows Information Bar.wav",0,1); sounds = true;}
  32. else sounds = false;
  33.  
  34. //Main loop
  35. while(1)
  36. {
  37.     //Update timing
  38.     time_previous = time;
  39.     time = current_time;
  40.    
  41.     //Sleep until next frame
  42.     sleep((1000/frames)-(time-time_previous));    
  43.    
  44.     //Handle the keyboard stuff and mouse stuff and stuff
  45.     io_handle();
  46.    
  47.     //Update quit and what-not
  48.     if keyboard_check_released(vk_escape) {break; game_end();}
  49.     if started = false if keyboard_check_pressed(vk_space) or mouse_check_button(mb_left) {started = true; ball_xspeed = choose(-2,2); ball_yspeed = choose(-1,1);}
  50.     if fade > 0.01 fade *= 0.95; else fade = 0;
  51.    
  52.     //Update Player
  53.     p2_y = mouse_y;
  54.     if p2_y > room_height-(p2_h/2) {p2_y = room_height-(p2_h/2);}
  55.     if p2_y < p2_h/2 {p2_y = p2_h/2;}
  56.    
  57.     //Update AI
  58.     if ((p2_s-p1_s) != 4 and (p2_s-p1_s) != 0) p1_y += (ball_y-p1_y)/(32-((p2_s-p1_s)*8));
  59.     if (p2_s-p1_s) = 0  p1_y += (ball_y-p1_y)/32;
  60.     if (p2_s-p1_s) = 4  p1_y += (ball_y-p1_y);
  61.    
  62.     //Update Ball
  63.     if started
  64.     {
  65.         ball_x += ball_xspeed;//lengthdir_x(ball_speed,ball_direction);
  66.         ball_y += ball_yspeed;//lengthdir_y(ball_speed,ball_direction);
  67.        
  68.         //Enemy paddle
  69.         if ball_x-8 <= p1_x and (ball_y-8 <= p1_y+p1_h and ball_y+8 >= p1_y-p1_h) and ball_xspeed < 0
  70.         {
  71.             ball_xspeed *= -1.025;
  72.             ball_yspeed *= 1.1;
  73.             if sounds sound_play(s_hit);
  74.         }
  75.         //Enemy goal
  76.         if ball_x-8 <= 0
  77.         {
  78.             started = false;
  79.             ball_x = room_width/2;
  80.             ball_y = room_height/2;
  81.             p2_s += 1;
  82.             fade = 1;
  83.         }
  84.        
  85.         //Player paddle
  86.         if ball_x+8 >= p2_x and (ball_y-8 <= p2_y+p2_h and ball_y+8 >= p2_y-p2_h) and ball_xspeed > 0
  87.         {
  88.             ball_xspeed *= -1.025;
  89.             ball_yspeed *= 1.025;
  90.             if sounds sound_play(s_hit);
  91.         }
  92.         //Player goal
  93.         if ball_x+8 >= room_width
  94.         {
  95.             started = false;
  96.             ball_x = room_width/2;
  97.             ball_y = room_height/2;
  98.             p1_s += 1;
  99.             fade = 1;
  100.         }
  101.        
  102.         //Room bounds
  103.         if ball_y+8 >= room_height or ball_y-8 <= 0
  104.         {
  105.             ball_yspeed *= -1.025;
  106.             ball_yspeed *= 1.025;
  107.             if sounds sound_play(s_hit);
  108.         }
  109.     }
  110.    
  111.     //Draw stuff
  112.     draw_clear(merge_color(c_black,c_white,fade));
  113.     draw_rectangle(0,p1_y-p1_h,p1_x,p1_y+p1_h,false);
  114.     draw_rectangle(p2_x,p2_y-p2_h,room_width,p2_y+p2_h,false);
  115.     draw_rectangle(ball_x-8,ball_y-8,ball_x+8,ball_y+8,false);
  116.     draw_set_font(f_game);
  117.     if !started draw_text(room_width/2,room_height/2-32,"Press <Spacebar> to start");
  118.     draw_text(32,4,p1_s);
  119.     draw_text(room_width-32,4,p2_s);
  120.     draw_set_font(f_ball);
  121.     if started draw_text(room_width/2,room_height/2,abs(ball_xspeed));
  122.     screen_refresh();
  123. }
Advertisement
Add Comment
Please, Sign In to add comment