Guest User

Untitled

a guest
May 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. #include <allegro.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include "vector.h"
  5.  
  6. const int GFX_HEIGHT = 600, GFX_WIDTH = 800;
  7.  
  8. int page_active=1;
  9.  
  10. BITMAP *buffer[3];
  11.  
  12. float d_time=1;
  13. int black=makecol(0,100,10),i,j;
  14. float alfa=90,v,ruch_check=2;
  15.  
  16.  
  17. void init();
  18.  
  19. void deinit();
  20. /////////////////////////
  21. //#####################//
  22. /////////////////////////
  23. struct bila
  24. {
  25. int r, color;
  26. vec poz;
  27. vec v,v1,v2;
  28. float tarcie;
  29. };
  30. bila bile[7];
  31. int ilosc=7;
  32. /////////////////////////
  33. //#####################//
  34. /////////////////////////
  35. void ruch(bila &a)
  36. {
  37. a.poz+=a.v*d_time;
  38. a.v*=a.tarcie;
  39. a.tarcie-=0.00001;
  40. }
  41. /////////////////////////
  42. //#####################//
  43. /////////////////////////
  44. void kolizja(bila &a, bila &b)
  45. {
  46. vec S=a.poz + a.v * d_time -( b.poz + b.v * d_time);
  47. if(S.len()<=a.r+b.r)
  48. {
  49.  
  50.  
  51. //odbicie od nieruchomej kulki W=S.perp(); n=S.normalize(); N=dot(-(bila.v),n)*n; L=bila.v+N; bila.v=-bila.v+2*L;
  52.  
  53. //odbicie dla nieruchomej kulki z nadaniem jej predkosci alfa=acos(dot(bila.v.normalize(),S.normalize())); biala.v=(bila.v.len()*cos(alfa))*S.normalize(); bila.v=bila.v-biala.v;
  54.  
  55. vec n = S.normalize();
  56. vec x1 = dot(a.v,n)*n;
  57. vec x2 = dot(b.v,n)*n;
  58. vec p1 = a.v - x1;
  59. vec p2 = b.v - x2;
  60. a.v = x2 + p1;
  61. b.v = x1 + p2;
  62. a.v*=a.tarcie;
  63. b.v*=b.tarcie;
  64. a.tarcie-=0.001;
  65. b.tarcie-=0.001;
  66. //ruch(a);
  67. //ruch(b);
  68. }
  69. }
  70. /////////////////////////
  71. //#####################//
  72. /////////////////////////
  73. void sciana(bila &a)
  74. {
  75. if(a.poz.y-a.r<=0)
  76. {
  77. //a.v.y*=-1;
  78. a.v*=a.tarcie;
  79. a.tarcie-=0.001;
  80. a.poz.y=0+a.r;
  81. vec n=(vec(0,0) - vec(60,0)).perp().normalize();
  82. vec N = dot(-(a.v), n) * n;
  83. vec L=a.v+N;
  84. a.v=-a.v+2*L;
  85. }
  86. if(a.poz.y+a.r>=600)
  87. {
  88. // a.v.y*=-1;
  89. a.v*=a.tarcie;
  90. a.tarcie-=0.001;
  91. a.poz.y=600-a.r;
  92. vec n=(vec(0,0) - vec(60,0)).perp().normalize();
  93. vec N = dot(-(a.v), n) * n;
  94. vec L=a.v+N;
  95. a.v=-a.v+2*L;
  96. }
  97. if(a.poz.x-a.r<=0)
  98. {
  99. // a.v.x*=-1;
  100. a.v*=a.tarcie;
  101. a.tarcie-=0.001;
  102. a.poz.x=0+a.r;
  103. vec n=(vec(0,0) - vec(0,60)).perp().normalize();
  104. vec N = dot(-(a.v), n) * n;
  105. vec L=a.v+N;
  106. a.v=-a.v+2*L;
  107. }
  108. if(a.poz.x+a.r>=800)
  109. {
  110. //a.v.x*=-1;
  111. a.v*=a.tarcie;
  112. a.tarcie-=0.001;
  113. a.poz.x=800-a.r;
  114. vec n=(vec(0,0) - vec(0,60)).perp().normalize();
  115. vec N = dot(-(a.v), n) * n;
  116. vec L=a.v+N;
  117. a.v=-a.v+2*L;
  118.  
  119. }
  120. }
  121. /////////////////////////
  122. //#####################//
  123. /////////////////////////
  124. void rysuj(bila a)
  125. {
  126. //acquire_screen();
  127. circlefill(buffer[page_active],a.poz.x,a.poz.y,a.r,a.color);
  128. //release_screen();
  129. }
  130. /////////////////////////
  131. //#####################//
  132. /////////////////////////
  133. void sila()
  134. {
  135. while (!key[KEY_ENTER]&&!key[KEY_ESC])
  136. {
  137. vsync();
  138. //acquire_screen();
  139. clear_to_color(buffer[page_active],black);
  140.  
  141. for(i=0;i<ilosc;i++)
  142. {
  143. rysuj(bile[i]);
  144. }
  145. line(buffer[page_active],bile[0].poz.x,bile[0].poz.y,bile[0].poz.x+(bile[0].v.x)*5,bile[0].poz.y+bile[0].v.y,makecol(100,100,250));
  146. if(key[KEY_RIGHT])
  147. {
  148. bile[0].v.x++;
  149. }
  150. if(key[KEY_LEFT]&&bile[0].v.x>0)
  151. {
  152. bile[0].v.x--;
  153. }
  154.  
  155. release_bitmap(screen);
  156. draw_sprite(screen, buffer[page_active], 0, 0);
  157. page_active=3-page_active;
  158. }
  159. }
  160. /////////////////////////
  161. //#####################//
  162. /////////////////////////
  163. void kierunek()
  164. {
  165. while(!key[KEY_SPACE]&&!key[KEY_ESC])
  166. {
  167. vsync();
  168. //acquire_screen();
  169. clear_to_color(buffer[page_active],black);
  170. bile[0].v.x=(bile[0].v.len()*sin((alfa*M_PI)/180));
  171. bile[0].v.y=(bile[0].v.len()*cos((alfa*M_PI)/180));
  172.  
  173.  
  174. for(i=0;i<ilosc;i++)
  175. {
  176. rysuj(bile[i]);
  177. }
  178.  
  179. line(buffer[page_active],bile[0].poz.x,bile[0].poz.y,bile[0].poz.x+(bile[0].v.x)*5,bile[0].poz.y+(bile[0].v.y)*5,makecol(100,100,250));
  180.  
  181. if(key[KEY_UP])
  182. {
  183. alfa++;
  184. }
  185. if(key[KEY_DOWN])
  186. {
  187. alfa--;
  188. }
  189.  
  190. release_bitmap(screen);
  191. draw_sprite(screen, buffer[page_active], 0, 0);
  192. page_active=3-page_active;
  193. }
  194. }
  195. /////////////////////////
  196. //#####################//
  197. /////////////////////////
  198. void odbijanie()
  199. {
  200. while (!key[KEY_ESC]&&ruch_check>=0.01)
  201. {
  202. vsync();
  203. //acquire_screen();
  204. clear_to_color(buffer[page_active],black);
  205.  
  206.  
  207. for(i=0;i<ilosc;i++)
  208. {
  209. for(j=0;j<ilosc;j++)
  210. {
  211. if(i!=j)
  212. {
  213. kolizja(bile[i],bile[j]);
  214. }
  215. }
  216. ruch(bile[i]);
  217. sciana(bile[i]);
  218. rysuj(bile[i]);
  219. }
  220. printf("%1.1f\n",bile[0].v.len());
  221. release_bitmap(screen);
  222. draw_sprite(screen, buffer[page_active], 0, 0);
  223. page_active=3-page_active;
  224. ruch_check=0;
  225. for(i=0;i<ilosc;i++)
  226. {
  227. ruch_check+=bile[i].v.len();
  228. }
  229. }
  230. }
  231.  
  232. /////////////////////////
  233. //#####################//
  234. /////////////////////////
  235.  
  236. /////////////////////////
  237. //#####################//
  238. /////////////////////////
  239.  
  240. int main()
  241. {
  242. init();
  243. //acquire_screen();
  244.  
  245. clear_to_color(screen,black);
  246.  
  247. bile[0].v=vec(8,0);
  248. bile[0].poz=vec(100,300);
  249. bile[0].color=makecol(255,255,255);
  250. bile[0].r=10;
  251. bile[0].tarcie=1;
  252.  
  253. bile[1].color=makecol(0,0,255);
  254. bile[1].poz=vec(300,300);
  255. bile[1].v=vec(0,0);
  256. bile[1].r=10;
  257. bile[1].tarcie=1;
  258.  
  259. bile[2].color=makecol(0,255,0);
  260. bile[2].poz=vec(400,250);
  261. bile[2].v=vec(0,0);
  262. bile[2].r=10;
  263. bile[2].tarcie=1;
  264.  
  265. bile[3].color=makecol(255,0,0);
  266. bile[3].poz=vec(400,350);
  267. bile[3].v=vec(0,0);
  268. bile[3].r=10;
  269. bile[3].tarcie=1;
  270.  
  271. bile[4].color=makecol(0,255,255);
  272. bile[4].poz=vec(500,300);
  273. bile[4].v=vec(0,0);
  274. bile[4].r=10;
  275. bile[4].tarcie=1;
  276.  
  277. bile[5].color=makecol(255,255,0);
  278. bile[5].poz=vec(500,400);
  279. bile[5].v=vec(0,0);
  280. bile[5].r=10;
  281. bile[5].tarcie=1;
  282.  
  283. bile[6].color=makecol(255,0,254);
  284. bile[6].poz=vec(500,200);
  285. bile[6].v=vec(0,0);
  286. bile[6].r=10;
  287. bile[6].tarcie=1;
  288. black=makecol(0,100,10);
  289. ruch_check=666;
  290. while(ilosc==7);
  291. {
  292. sila();
  293. kierunek();
  294. odbijanie();
  295. }
  296. deinit();
  297. return 0;
  298.  
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. END_OF_MAIN()
  309.  
  310. void init() {
  311. int depth, res;
  312. allegro_init();
  313. depth = desktop_color_depth();
  314. if (depth == 0) depth = 32;
  315. set_color_depth(depth);
  316. res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_WIDTH, GFX_HEIGHT, 0, 0);
  317. buffer[0]=create_video_bitmap(GFX_WIDTH, GFX_HEIGHT);
  318. buffer[1]=create_video_bitmap(GFX_WIDTH, GFX_HEIGHT);
  319. buffer[2]=create_video_bitmap(GFX_WIDTH, GFX_HEIGHT);
  320. if (res != 0) {
  321. allegro_message(allegro_error);
  322. exit(-1);
  323. }
  324.  
  325. install_timer();
  326. install_keyboard();
  327. install_mouse();
  328. /* add other initializations here */
  329. }
  330.  
  331. void deinit() {
  332. clear_keybuf();
  333. /* add other deinitializations here */
  334. }
Add Comment
Please, Sign In to add comment