Guest User

Untitled

a guest
May 26th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 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=0;
  9.  
  10. BITMAP *buffer[2];
  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_bitmap(buffer[page_active]);
  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(buffer[page_active]);
  156. draw_sprite(screen, buffer[page_active], 0, 0);
  157. page_active=1-page_active;
  158. }
  159. }
  160. /////////////////////////
  161. //#####################//
  162. /////////////////////////
  163. void kierunek()
  164. {
  165. while(!key[KEY_SPACE]&&!key[KEY_ESC])
  166. {
  167. vsync();
  168. acquire_bitmap(buffer[page_active]);
  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(buffer[page_active]);
  191. draw_sprite(screen, buffer[page_active], 0, 0);
  192. page_active=1-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_bitmap(buffer[page_active]);
  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.  
  222. draw_sprite(screen, buffer[page_active], 0, 0);
  223. release_bitmap(buffer[page_active]);
  224. page_active=1-page_active;
  225. ruch_check=0;
  226. for(i=0;i<ilosc;i++)
  227. {
  228. ruch_check+=bile[i].v.len();
  229. }
  230. }
  231. }
  232.  
  233. /////////////////////////
  234. //#####################//
  235. /////////////////////////
  236.  
  237. /////////////////////////
  238. //#####################//
  239. /////////////////////////
  240.  
  241. int main()
  242. {
  243. init();
  244. //acquire_screen();
  245.  
  246. clear_to_color(screen,black);
  247.  
  248. bile[0].v=vec(8,0);
  249. bile[0].poz=vec(100,300);
  250. bile[0].color=makecol(255,255,255);
  251. bile[0].r=10;
  252. bile[0].tarcie=1;
  253.  
  254. bile[1].color=makecol(0,0,255);
  255. bile[1].poz=vec(300,300);
  256. bile[1].v=vec(0,0);
  257. bile[1].r=10;
  258. bile[1].tarcie=1;
  259.  
  260. bile[2].color=makecol(0,255,0);
  261. bile[2].poz=vec(400,250);
  262. bile[2].v=vec(0,0);
  263. bile[2].r=10;
  264. bile[2].tarcie=1;
  265.  
  266. bile[3].color=makecol(255,0,0);
  267. bile[3].poz=vec(400,350);
  268. bile[3].v=vec(0,0);
  269. bile[3].r=10;
  270. bile[3].tarcie=1;
  271.  
  272. bile[4].color=makecol(0,255,255);
  273. bile[4].poz=vec(500,300);
  274. bile[4].v=vec(0,0);
  275. bile[4].r=10;
  276. bile[4].tarcie=1;
  277.  
  278. bile[5].color=makecol(255,255,0);
  279. bile[5].poz=vec(500,400);
  280. bile[5].v=vec(0,0);
  281. bile[5].r=10;
  282. bile[5].tarcie=1;
  283.  
  284. bile[6].color=makecol(255,0,254);
  285. bile[6].poz=vec(500,200);
  286. bile[6].v=vec(0,0);
  287. bile[6].r=10;
  288. bile[6].tarcie=1;
  289. black=makecol(0,100,10);
  290. ruch_check=666;
  291. while(ilosc==7&&!key[KEY_ESC]);
  292. {
  293. acquire_bitmap(buffer[page_active]);
  294. sila();
  295. kierunek();
  296. odbijanie();
  297. draw_sprite(screen, buffer[page_active], 0, 0);
  298. release_bitmap(buffer[page_active]);
  299. }
  300. deinit();
  301. return 0;
  302.  
  303. }
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312. END_OF_MAIN()
  313.  
  314. void init() {
  315. int depth, res;
  316. allegro_init();
  317. depth = desktop_color_depth();
  318. if (depth == 0) depth = 32;
  319. set_color_depth(depth);
  320. res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_WIDTH, GFX_HEIGHT, 0, 0);
  321. buffer[0]=create_bitmap(GFX_WIDTH, GFX_HEIGHT);
  322. buffer[1]=create_bitmap(GFX_WIDTH, GFX_HEIGHT);
  323.  
  324. if (res != 0) {
  325. allegro_message(allegro_error);
  326. exit(-1);
  327. }
  328.  
  329. install_timer();
  330. install_keyboard();
  331. install_mouse();
  332. /* add other initializations here */
  333. }
  334.  
  335. void deinit() {
  336. clear_keybuf();
  337. /* add other deinitializations here */
  338. }
Add Comment
Please, Sign In to add comment