Advertisement
Guest User

Untitled

a guest
May 30th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. program Breakout;
  2. uses Allegro5, Al5primitives;
  3. Const
  4. FPS = 60;
  5. length_paddle = 100;
  6. type
  7. Colors = (RED, WHITE, BLUE, YELLOW, GREEN, NONE);
  8. Ball = record
  9. BallX, BallY, BallS, BallD : integer;
  10. end;
  11.  
  12. Paddle = record
  13. Posx : integer;
  14. Ldir, Rdir : boolean;
  15. end;
  16.  
  17. Blocks = array [1..5, 1..10] of Colors;
  18. Var
  19. Display : ALLEGRO_DISPLAYptr;
  20. PBall : Ball;
  21. PPaddle : Paddle;
  22. PBlocks : Blocks;
  23. PColors : Colors;
  24. procedure init;
  25. var
  26. i, j : byte;
  27. begin
  28. al_init();
  29. al_init_primitives_addon();
  30. al_install_keyboard();
  31. PPaddle.Posx := 300;
  32. PBall.BallX := 350;
  33. PBall.BallY := 645;
  34. PBall.BallD := 290;
  35. PBall.BallS := 10;
  36. {
  37. for i:=1 to 5 do
  38. for j:=1 to 10 do
  39. PBlocks[i, j] := WHITE;
  40. }
  41. PBlocks[1, 1] := RED; PBlocks[1, 2] := YELLOW; PBlocks[1, 3] := RED; PBlocks[1, 4] := RED; PBlocks[1, 5] := RED; PBlocks[1, 6] := RED; PBlocks[1, 7] := RED; PBlocks[1, 8] := RED; PBlocks[1, 9] := RED; PBlocks[1, 10] := RED;
  42. end;
  43.  
  44. procedure GameLoop(PBall : Ball; PPaddle : Paddle);
  45.  
  46. procedure Draw(PPaddle : Paddle; PBall : Ball);
  47. var
  48. i, j : byte;
  49. Color : ALLEGRO_COLOR;
  50. begin
  51. al_clear_to_color(al_map_rgb(0, 0, 0));
  52. // Draw the Paddle
  53. al_draw_line(PPaddle.Posx, 680, PPaddle.Posx+length_paddle, 680, al_map_rgb(255, 255, 255), 30 );
  54.  
  55. // Draw ball
  56. al_draw_filled_rectangle(PBall.BallX, PBall.BallY, PBall.BallX+20, PBall.BallY+20, al_map_rgb(255, 255, 255));
  57. for i:=1 to 5 do
  58. for j:=0 to 9 do
  59. if PBlocks[i, j+1] <> NONE then begin
  60. case PBlocks[i, j+1] of
  61. RED : Color := al_map_rgb(220, 0, 0);
  62. WHITE : Color := al_map_rgb(255, 255, 255);
  63. BLUE : Color := al_map_rgb(17, 44, 214);
  64. YELLOW : Color := al_map_rgb(246, 246, 10);
  65. GREEN : Color := al_map_rgb(139, 105, 20);
  66. end;
  67. al_draw_line(j*70+10, i*50, j*70+75, i*50, Color, 30 );
  68. end;
  69. al_flip_display();
  70. end;
  71. Var
  72. timer : ALLEGRO_TIMERptr;
  73. redraw, Closed : boolean;
  74. event_queue : ALLEGRO_EVENT_QUEUEptr;
  75. ev : ALLEGRO_EVENT;
  76. i, j : byte;
  77. begin
  78. Closed := False;
  79. event_queue := al_create_event_queue();
  80. timer := al_create_timer(1.0/FPS);
  81. al_register_event_source(event_queue, al_get_keyboard_event_source);
  82. al_register_event_source(event_queue, al_get_display_event_source(Display));
  83. al_register_event_source(event_queue, al_get_timer_event_source(timer));
  84. al_start_timer(timer);
  85. While not(Closed) do begin
  86. al_wait_for_event(event_queue, ev);
  87. if (ev._type = ALLEGRO_EVENT_KEY_DOWN) then
  88. case ev.keyboard.keycode of
  89. ALLEGRO_KEY_LEFT : PPaddle.Ldir := True;
  90. ALLEGRO_KEY_RIGHT : PPaddle.Rdir := True;
  91. end
  92. else if (ev._type = ALLEGRO_EVENT_KEY_UP) then
  93. case ev.keyboard.keycode of
  94. ALLEGRO_KEY_LEFT : PPaddle.Ldir := False;
  95. ALLEGRO_KEY_RIGHT : PPaddle.Rdir := False;
  96. end
  97. else if (ev._type = ALLEGRO_EVENT_DISPLAY_CLOSE ) then Closed := True
  98. else if (ev._type = ALLEGRO_EVENT_TIMER) then begin
  99. if PPaddle.Ldir and (PPaddle.Posx-10>=10) then PPaddle.Posx -= 10
  100. else if PPaddle.Rdir and (PPaddle.Posx+10+length_paddle<=690) then PPaddle.Posx +=10;
  101. with PBall do begin
  102. Ballx += trunc(cos(Pi / 180 * BallD)*BallS);
  103. BallY += trunc(sin(Pi / 180 * BallD)*BallS) ;
  104. end;
  105. if (PBall.BallX <= 20) or (PBall.BallX >= 680) then PBall.BallD += (90 - PBall.BallD)*2;
  106. if (PBall.BallY <= 0) then PBall.BallD += (180 - PBall.BallD) *2;
  107. // Cheking Collision With the paddle
  108. if ((PBall.BallY >= 650) and (PBall.BallY<=660))
  109. and ((PBall.BallX+20 >= PPaddle.Posx) and (PBall.BallX+20<= PPaddle.Posx+length_paddle)) then begin
  110. PBall.BallD += (180 - PBall.BallD - random(4)) *2; // adding some randomness :3
  111. if (PBall.BallX in [PPaddle.Posx..PPaddle.Posx+40]) or (PBall.BallX in [PPaddle.Posx+60..PPaddle.Posx+length_paddle]) then PBall.BallS := 18
  112. else PBall.BallS := 10;
  113. end;
  114. // Check collision with blocks
  115. // I didn't use The power of maths but this works (it's Ugly and slow);
  116. for i:=1 to 5 do
  117. for j:=0 to 9 do
  118. // So the Ball Was hit
  119. if (((PBall.BallX >= j*70+10) and (PBall.BallX <= j*70+75)) and ((PBall.BallY >=i*50) and (PBall.BallY <=i*50+10))) and (PBlocks[i, j+1]<>NONE)then begin
  120. // hit from the below or above
  121. if (PBall.BallY <= i*50+10) or (PBall.BallY >= i) then
  122. PBall.BallD += (180 - PBall.BallD) *2
  123. // hit from the sides
  124. else if (PBall.BallX <= j*70+10) or ((PBall.BallX <= j*70+75)) then
  125. PBall.BallD += (90 - PBall.BallD) *2;
  126. PBlocks[i, j+1] := NONE;
  127. end;
  128. redraw := True;
  129. end;
  130. if redraw and al_is_event_queue_empty(event_queue) then begin
  131. Draw(PPaddle, PBall);
  132. redraw := False;
  133. end;
  134. end;
  135. end;
  136.  
  137. BEGIN
  138. init;
  139. Display := al_create_display(700, 700);
  140. al_set_window_title(Display, 'Crazy Breakout');
  141. GameLoop(PBall, PPaddle);
  142. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement