Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include<vector>
  4. #include<ctime>
  5. using namespace std;
  6. struct Birdblock
  7. {
  8. sf::Sprite bird;
  9. sf::Texture texture;
  10. float x=250,y=384,timefall=0;
  11. float scale_ratio=0.1588f;
  12. sf::Vector2f scale;
  13. float width=272*scale_ratio,height=225*scale_ratio;
  14. Birdblock()
  15. {
  16. if(!texture.loadFromFile("redbird.png"))
  17. cout<<"Error";
  18. bird.setTexture(texture);
  19. bird.setOrigin(sf::Vector2f(136.f,112.5f));
  20. bird.setScale(sf::Vector2f(scale_ratio,scale_ratio));
  21. scale = bird.getScale();
  22. bird.setPosition(x,y);
  23. }
  24. void update(float window_width,float window_height)
  25. {
  26. bird.setPosition(sf::Vector2f(x,y));
  27. }
  28. void draw(sf::RenderWindow& window)
  29. {
  30. window.draw(bird);
  31. }
  32. void moveleft(float window_width, float window_height)
  33. {
  34. bird.setScale(scale.x * -1, scale.y * 1);
  35. if(bird.getRotation()!=45)
  36. bird.setRotation(bird.getRotation() + 45);
  37. x+=-0.4;
  38. y+=-0.4;
  39. timefall=0;
  40. if(x-width/2<=0)
  41. x=0+width/2;
  42. }
  43. void moveright(float window_width, float window_height)
  44. {
  45. bird.setScale(scale.x * 1, scale.y * 1);
  46. if(bird.getRotation()!=315)
  47. bird.setRotation(bird.getRotation() + 315);
  48. x+=0.4;
  49. y+=-0.4;
  50. timefall=0;
  51. if(x+width/2>=window_width)
  52. x=window_width-width/2;
  53. }
  54. void fall(float window_width, float window_height)
  55. {
  56. if(bird.getRotation()<=45&&bird.getRotation()>0)
  57. {
  58. bird.setRotation(bird.getRotation()-1);
  59. }
  60. if(bird.getRotation()>=315&&bird.getRotation()<360)
  61. {
  62. bird.setRotation(bird.getRotation()+1);
  63. }
  64. y+=timefall;
  65. timefall+=0.001;
  66. if(x+width/2==window_width)
  67. {
  68. bird.setScale(scale.x * -1, scale.y * 1);
  69. x+=-50;
  70. y+=-40;
  71. }
  72. if(x-width/2==0)
  73. {
  74. bird.setScale(scale.x * 1, scale.y * 1);
  75. x+=50;
  76. y+=-40;
  77. }
  78. }
  79.  
  80. };
  81.  
  82. struct Background
  83. {
  84. sf::Sprite background;
  85. sf::Texture texture;
  86. float x = 0, y = 0;
  87. float scale = 1.f;
  88. float width = 432*scale, height = 768*scale;
  89.  
  90. Background()
  91. {
  92. if (!texture.loadFromFile("background.png"))
  93. {
  94. cout << "Error" << endl;
  95. }
  96. background.setTexture(texture);
  97. //background.setOrigin(sf::Vector2f(f,f));
  98. background.setScale(sf::Vector2f(scale, scale));
  99. }
  100.  
  101. void update(float window_width, float window_height)
  102. {
  103.  
  104. }
  105.  
  106. void draw(sf::RenderWindow& window)
  107. {
  108. window.draw(background);
  109. }
  110. };
  111.  
  112. sf::Texture brickTexture;
  113.  
  114. struct Brickblock
  115. {
  116. sf::Sprite brick;
  117. float x = 200, y = 0;
  118. float scale = 0.8f;
  119. float width = 81*scale, height = 41*scale;
  120.  
  121. Brickblock()
  122. {
  123. if (!brickTexture.loadFromFile("brick.png"))
  124. {
  125. cout << "Error" << endl;
  126. }
  127. brick.setTexture(brickTexture);
  128. brick.setOrigin(sf::Vector2f(40.5f, 41.f));
  129. brick.setScale(sf::Vector2f(scale, scale));
  130. }
  131. void setX(float _x)
  132. {
  133. x=_x;
  134. }
  135. void draw(sf::RenderWindow& window)
  136. {
  137. window.draw(brick);
  138. }
  139. void update(float window_width, float window_height)
  140. {
  141. brick.setPosition(sf::Vector2f(x, y));
  142. y+=0.1;
  143. }
  144. bool collide(float birdx,float birdy,float birdwidth,float birdheight)
  145. {
  146. if(y-height<=birdy+birdheight/2 && y>=birdy-birdheight/2) //canh tren gach<=canh duoi chim va canh duoi gach>=canh tren chim
  147. {
  148. if(x+width/2>=birdx-birdwidth/2 && x-width/2<=birdx+birdwidth/2) //canh phai gach>=canh trai chim va canh trai gach<=canh phai chim
  149. {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. };
  156. struct Rowbricks
  157. {
  158. vector<Brickblock> row;
  159. int brick_number;
  160. float rand_number,xx;
  161. Rowbricks()
  162. {
  163. rand_number=rand();
  164. brick_number=(int)rand_number%3+1;
  165. if(brick_number==1)
  166. {
  167. rand_number=rand();
  168. xx=(int)rand()%432;
  169. Brickblock brick;
  170. brick.setX(xx);
  171. row.push_back(brick);
  172. }
  173. if(brick_number==2)
  174. {
  175. for(int i=0; i<2; i++)
  176. {
  177. if(i==0)
  178. {
  179. Brickblock brick;
  180. rand_number=rand();
  181. xx=(int)rand_number%190;
  182. brick.setX(xx);
  183. row.push_back(brick);
  184. }
  185. if(i==1)
  186. {
  187. Brickblock brick;
  188. rand_number=rand();
  189. xx=(int)rand_number%190+241;
  190. brick.setX(xx);
  191. row.push_back(brick);
  192. }
  193. }
  194. }
  195. if(brick_number==3)
  196. {
  197. for(int i=0; i<3; i++)
  198. {
  199. if(i==0)
  200. {
  201. Brickblock brick;
  202. rand_number=rand();
  203. xx=(int)rand_number%120;
  204. brick.setX(xx);
  205. row.push_back(brick);
  206. }
  207. if(i==1)
  208. {
  209. Brickblock brick;
  210. rand_number=rand();
  211. xx=(int)rand_number%99+165;
  212. brick.setX(xx);
  213. row.push_back(brick);
  214. }
  215. if(i==2)
  216. {
  217. Brickblock brick;
  218. rand_number=rand();
  219. xx=(int)rand_number%31+400;
  220. brick.setX(xx);
  221. row.push_back(brick);
  222. }
  223. }
  224. }
  225. }
  226. void draw(sf::RenderWindow& window)
  227. {
  228. for(int i=0; i<brick_number; i++)
  229. {
  230. row[i].draw(window);
  231. }
  232. }
  233. void update(float window_width, float window_height)
  234. {
  235. for(int i=0; i<brick_number; i++)
  236. {
  237. row[i].update(window_width,window_height);
  238. }
  239. }
  240. bool collide(float birdx,float birdy,float birdwidth,float birdheight)
  241. {
  242. for(int i=0; i<brick_number; i++)
  243. {
  244. if(row[i].collide(birdx,birdy,birdwidth,birdheight)==true)
  245. return true;
  246. }
  247. return false;
  248. }
  249. };
  250. struct Allbricks
  251. {
  252. vector<Rowbricks>all;
  253.  
  254. void draw(sf::RenderWindow& window)
  255. {
  256. for(unsigned int i=0; i<all.size(); i++)
  257. {
  258. all[i].draw(window);
  259. }
  260. }
  261. void update(long timeloop,float window_width, float window_height)
  262. {
  263. if(timeloop%2250==0)
  264. {
  265. Rowbricks row;
  266. all.push_back(row);
  267. cout<<"time loop"<<" "<<timeloop<<endl;
  268. cout<<"all.size"<<" "<<all.size()<<endl;
  269. }
  270. for(unsigned int i=0; i<all.size(); i++)
  271. {
  272. all[i].update(window_width,window_height);
  273. }
  274. }
  275. bool collide(float birdx,float birdy,float birdwidth,float birdheight)
  276. {
  277. for(unsigned int i=0; i<all.size(); i++)
  278. {
  279. if(all[i].collide(birdx,birdy,birdwidth,birdheight)==true)
  280. return true;
  281. }
  282. return false;
  283. }
  284. };
  285. struct Status
  286. {
  287. float window_width=432,window_height=768;
  288. Background background;
  289. Birdblock bird;
  290. //Brickblock brick;
  291. //Rowbricks row;
  292. Allbricks all;
  293. long timeloop=0;//225
  294. bool gameover=false;
  295. void displayStatus(sf::RenderWindow& window)
  296. {
  297. window.clear(sf::Color::Black);
  298. background.draw(window);
  299. //brick.draw(window);
  300. //row.draw(window);
  301. all.draw(window);
  302. bird.draw(window);
  303. window.display();
  304. }
  305.  
  306. void updateStatus()
  307. {
  308. if(gameover==false)
  309. {
  310. if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  311. bird.moveleft(window_width,window_height);
  312. else if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
  313. bird.moveright(window_width,window_height);
  314. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  315. bird.moveleft(window_width,window_height);
  316. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  317. bird.moveright(window_width,window_height);
  318. else
  319. bird.fall(window_width,window_height);
  320. }
  321. else
  322. {
  323. bird.y+=-0.5;
  324. bird.fall(window_width,window_height);
  325. }
  326. if(gameover==false)
  327. {
  328. //brick.update(window_width,window_height);
  329. //row.update(window_width,window_height);
  330. all.update(timeloop,window_width,window_height);
  331. }
  332. bird.update(window_width,window_height);
  333. if(all.collide(bird.x,bird.y,bird.width,bird.height)==true||bird.y>=window_height)
  334. {
  335. //cout<<"cham gach"<<endl;
  336. gameover=true;
  337. }
  338.  
  339. timeloop++;
  340. }
  341.  
  342. };
  343. int main()
  344. {
  345. srand(time(0));
  346. Status status;
  347. sf::RenderWindow window(sf::VideoMode(status.window_width,status.window_height), "Dump Jump");
  348. if (!brickTexture.loadFromFile("brick.png"))
  349. {
  350. cout << "Error" << endl;
  351. }
  352. while (window.isOpen())
  353. {
  354. sf::Event event;
  355. while (window.pollEvent(event))
  356. {
  357. if (event.type == sf::Event::Closed)
  358. window.close();
  359. }
  360.  
  361. status.updateStatus();
  362. status.displayStatus(window);
  363. }
  364.  
  365. return 0;
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement