Advertisement
Guest User

Untitled

a guest
May 31st, 2012
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.50 KB | None | 0 0
  1. program pjp;
  2.  
  3. uses wincrt, wingraph, winmouse, sysutils, dos;
  4.  
  5. type Tball = record
  6.                     x, y, radius, vy:integer;
  7.                     check, onscreen:boolean;
  8.               end;
  9.      Tblock = record
  10.                     x1, x2, y1, y2, vy, vx:integer;
  11.                end;
  12.  
  13. var driver, mode:smallint;
  14.     key:char;
  15.     balls:array[1..2] of Tball;
  16.     block:Tblock;
  17.     lim,acelcount,vxb,auxcount:integer;
  18.  
  19. procedure randomball(n:integer);
  20. begin
  21.     with balls[n] do
  22.     begin
  23.         onscreen:=true;
  24.         check:=false;
  25.         randomize;
  26.         x:=getmaxx;
  27.         y:=random(getmaxy+2200);
  28.         radius:=40;
  29.     end;
  30. end;
  31.  
  32. procedure initialize(n:integer);
  33. begin
  34.     UpdateGraph(UpdateOff);
  35.     with block do
  36.     begin
  37.         x2:=getmaxx div 2;
  38.         x1:=x2 - 100;
  39.         y2:=getmaxy;
  40.         y1:=y2-200;
  41.         vy:=0;
  42.         vx:=0;
  43.     end;
  44.     with balls[n] do
  45.         begin
  46.             randomball(n);
  47.         end;
  48. end;
  49.  
  50. procedure acelerateball(acel:integer);
  51. begin
  52.     if (acelcount mod 13 = 0) and (acelcount<>0) and (auxcount<>acelcount) then begin
  53.     vxb:=vxb+acel;
  54.     auxcount:=acelcount;
  55.     end;
  56. end;
  57.  
  58. procedure moveball;
  59. var n:integer;
  60. begin
  61.     acelerateball(-1);
  62.     for n := 1 to lim do
  63.     with balls[n] do
  64.     begin
  65.         if onscreen = true then
  66.         x:= x + vxb;
  67.     end;
  68. end;
  69.  
  70. procedure killball;
  71. var cont:integer;
  72. begin
  73.     for cont:= 1 to lim do
  74.     with balls[cont] do
  75.     begin
  76.         if x+radius<0 then begin
  77.             onscreen:=false;
  78.         end;
  79.     end;
  80. end;
  81.  
  82. procedure alternateball;
  83. var n,rdm:integer;
  84. begin
  85.     for n := 1 to lim do
  86.         begin
  87.             with balls[n] do
  88.             if onscreen = true then
  89.             if check=false then
  90.                 begin
  91.                     randomize;
  92.                     rdm:=random(20);
  93.                     if (x<getmaxx div 2) and (x>getmaxx div 3) and (rdm<5) then
  94.                         begin
  95.                             check:=true;
  96.                             if n = lim then
  97.                             randomball(1)
  98.                             else randomball(n+1);
  99.                             acelcount:=acelcount+1;                
  100.                         end;
  101.                     if x<getmaxx div 3 then
  102.                         begin
  103.                             check:=true;
  104.                             if n = lim then
  105.                             randomball(1)
  106.                             else randomball(n+1);
  107.                             acelcount:=acelcount+1;
  108.                         end;
  109.                 end;
  110.         end;
  111. end;
  112.  
  113. procedure moveblock;
  114. begin
  115.     with block do
  116.     begin
  117.         x1:=x1+vx;
  118.         x2:=x2+vx;
  119.         y1:=y1+vy;
  120.         y2:=y2+vy;
  121.         if vx>0 then
  122.         vx:=vx-1;
  123.         if vx<0 then
  124.         vx:=vx+1;
  125.         if y2<getmaxy then
  126.         vy:=vy+1
  127.         else if y2>getmaxy then
  128.             begin
  129.                 vy:=0;
  130.                 y2:=getmaxy;
  131.                 y1:=y2-200;
  132.             end;
  133.         if y1<0 then
  134.             begin
  135.                 y1:=0;
  136.                 y2:=200;
  137.             end;
  138.         if x1<0 then
  139.             begin
  140.                 x1:=0;
  141.                 x2:=100;
  142.             end;
  143.         if x2>getmaxx then
  144.             begin
  145.                 x2:=getmaxx;
  146.                 x1:=getmaxx-100;
  147.             end;
  148.     end;
  149. end;
  150.  
  151.  
  152. procedure draw;
  153. var cont:integer;
  154. begin
  155.     clearviewport; 
  156.     setcolor(white);
  157.     SetFillStyle(solidfill, white);
  158.     setlinestyle(solidln, doublewidth, 4);
  159.     with block do
  160.     begin
  161.         FillRect(x1,y1,x2,y2);
  162.     end;
  163.     for cont := 1 to lim do
  164.     with balls[cont] do
  165.     if onscreen=true then
  166.     begin
  167.         circle(x,y,radius);
  168.     end;
  169. end;
  170.  
  171. procedure collisioncheck;
  172. var cont:integer;
  173. begin
  174.     for cont:= 1 to lim do
  175.     with balls[cont] do
  176.         begin
  177. if ((y-radius<block.y2) and (y-radius>block.y1)) or ((y+radius<block.y2) and (y+radius>block.y1)) then
  178. if ((x-radius<block.x2) and (x-radius>block.x1)) or ((x+radius<block.x2) and (x+radius>block.x1)) then
  179. delay(100); //the delay is just for knowing that the collision happened. gonna change it later.
  180.         end;
  181. end;
  182.  
  183. procedure joystick;
  184. begin
  185.     key:=readkey;
  186.     case key of
  187.     #0:begin
  188.         key:=readkey;
  189.         case key of
  190.         #72:with block do
  191.             begin
  192.                 vy:=-25;
  193.             end;
  194.         #80:with block do
  195.             begin
  196.         y1:=y2-100; //make it get half of its height
  197.         repeat
  198.             moveblock; //these are the drawing routines.
  199.             moveball;  //they are in another procedure, which is the 'main loop'
  200.             collisioncheck;
  201.             draw;      //i expected the code to run inside here with the block's
  202.             alternateball; //height changed, and as soon as the arrow key gets released
  203.             updateGraph(updateNow);  //it should go back to the 'main loop'
  204.             killball;
  205.             delay(10);
  206.         until keypressed = false; //<--thats what i think is not working
  207.         y1:=y2-200; //this would make the block get normal again
  208.             end;
  209.         #75:with block do
  210.             begin
  211.                 vx:=-20;
  212.             end;
  213.         #77:with block do
  214.             begin
  215.                 vx:=20;
  216.             end;
  217.             end;
  218.         end;
  219.     end;   
  220. end;
  221.  
  222. procedure game;
  223. begin
  224.     initialize(1);
  225.     draw;
  226.     repeat
  227.         repeat
  228.             moveblock;
  229.             moveball;
  230.             draw;
  231.             collisioncheck;
  232.             alternateball;
  233.             updateGraph(updateNow);
  234.             killball;
  235.             delay(10);
  236.         until keypressed;
  237.         joystick;
  238.         moveblock;
  239.         draw;
  240.     until key = #27;
  241. end;
  242.    
  243. //main
  244. begin
  245. lim:=2;
  246. acelcount:=0;
  247. vxb:=-5;
  248. driver:=nopalette;
  249. mode:=mMaximized;
  250.     initgraph(driver, mode, 'Test');
  251.         game;
  252.     closegraph;
  253. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement