Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/Audio.hpp>
  4. #include<vector>
  5. #include<string>
  6. #include<ctime>
  7. using namespace std;
  8. struct Birdblock
  9. {
  10. sf::Sprite bird;
  11. sf::Texture texture;
  12. float x=250,y=384,timefall=0;
  13. float scale_ratio=0.1588f;
  14. sf::Vector2f scale;
  15. float width=272*scale_ratio,height=225*scale_ratio;
  16. Birdblock()
  17. {
  18. if(!texture.loadFromFile("redbird.png"))
  19. cout<<"Error";
  20. bird.setTexture(texture);
  21. bird.setOrigin(sf::Vector2f(136.f,112.5f));
  22. bird.setScale(sf::Vector2f(scale_ratio,scale_ratio));
  23. scale = bird.getScale();
  24. bird.setPosition(x,y);
  25. }
  26. void update(float window_width,float window_height)
  27. {
  28. bird.setPosition(sf::Vector2f(x,y));
  29. }
  30. void draw(sf::RenderWindow& window)
  31. {
  32. window.draw(bird);
  33. }
  34. void moveleft(float window_width, float window_height)
  35. {
  36. bird.setScale(scale.x * -1, scale.y * 1);
  37. if(bird.getRotation()!=45)
  38. bird.setRotation(bird.getRotation() + 45);
  39. x+=-0.4;
  40. y+=-0.4;
  41. timefall=0;
  42. if(x-width/2<=0)
  43. x=0+width/2;
  44. }
  45. void moveright(float window_width, float window_height)
  46. {
  47. bird.setScale(scale.x * 1, scale.y * 1);
  48. if(bird.getRotation()!=315)
  49. bird.setRotation(bird.getRotation() + 315);
  50. x+=0.4;
  51. y+=-0.4;
  52. timefall=0;
  53. if(x+width/2>=window_width)
  54. x=window_width-width/2;
  55. }
  56. void fall(float window_width, float window_height)
  57. {
  58. if(bird.getRotation()<=45&&bird.getRotation()>0)
  59. {
  60. bird.setRotation(bird.getRotation()-5);
  61. }
  62. if(bird.getRotation()>=315&&bird.getRotation()<360)
  63. {
  64. bird.setRotation(bird.getRotation()+5);
  65. }
  66. y+=timefall;
  67. timefall+=0.001;
  68. if(x+width/2==window_width)
  69. {
  70. bird.setScale(scale.x * -1, scale.y * 1);
  71. x+=-50;
  72. y+=-40;
  73. }
  74. if(x-width/2==0)
  75. {
  76. bird.setScale(scale.x * 1, scale.y * 1);
  77. x+=50;
  78. y+=-40;
  79. }
  80. }
  81. void falldown(float window_width, float window_height)
  82. {
  83. y+=timefall;
  84. timefall+=0.002;
  85. bird.setRotation(bird.getRotation()+1);
  86. }
  87.  
  88. };
  89.  
  90. struct Background
  91. {
  92. sf::Sprite background;
  93. sf::Texture texture;
  94. float x = 0, y = 0;
  95. float scale = 1.f;
  96. float width = 432*scale, height = 768*scale;
  97.  
  98. Background()
  99. {
  100. if (!texture.loadFromFile("background.png"))
  101. {
  102. cout << "Error" << endl;
  103. }
  104. background.setTexture(texture);
  105. background.setScale(sf::Vector2f(scale, scale));
  106. }
  107.  
  108. void update(float window_width, float window_height)
  109. {
  110.  
  111. }
  112.  
  113. void draw(sf::RenderWindow& window)
  114. {
  115. window.draw(background);
  116. }
  117. };
  118. struct Endgame
  119. {
  120. sf::Sprite endgame;
  121. sf::Texture texture;
  122. float x = 0, y = 0;
  123. float scale = 1.f;
  124. float width = 432*scale, height = 768*scale;
  125.  
  126. Endgame()
  127. {
  128. if (!texture.loadFromFile("endgame.png"))
  129. {
  130. cout << "Error" << endl;
  131. }
  132. endgame.setTexture(texture);
  133. endgame.setScale(sf::Vector2f(scale, scale));
  134. }
  135. void draw(sf::RenderWindow& window)
  136. {
  137. window.draw(endgame);
  138. }
  139. };
  140. struct Startgame
  141. {
  142. sf::Sprite startgame;
  143. sf::Texture texture;
  144. float x = 0, y = 0;
  145. float scale = 1.f;
  146. float width = 432*scale, height = 768*scale;
  147.  
  148. Startgame()
  149. {
  150. if (!texture.loadFromFile("startgame.png"))
  151. {
  152. cout << "Error" << endl;
  153. }
  154. startgame.setTexture(texture);
  155. startgame.setScale(sf::Vector2f(scale, scale));
  156. }
  157. void draw(sf::RenderWindow& window)
  158. {
  159. window.draw(startgame);
  160. }
  161. };
  162.  
  163. sf::Texture brickTexture;
  164. sf::Font fontscore;
  165. sf::Text textscore;
  166. sf::Color color(0,142,173);
  167.  
  168. struct Brickblock
  169. {
  170. sf::Sprite brick;
  171. float x = 200, y = 0;
  172. float scale = 0.8f;
  173. float width = 81*scale, height = 41*scale;
  174.  
  175. Brickblock()
  176. {
  177. if (!brickTexture.loadFromFile("brick.png"))
  178. {
  179. cout << "Error" << endl;
  180. }
  181. brick.setTexture(brickTexture);
  182. brick.setOrigin(sf::Vector2f(40.5f, 41.f));
  183. brick.setScale(sf::Vector2f(scale, scale));
  184. }
  185. void setX(float _x)
  186. {
  187. x=_x;
  188. }
  189. void draw(sf::RenderWindow& window)
  190. {
  191. window.draw(brick);
  192. }
  193. void update(float window_width, float window_height)
  194. {
  195. brick.setPosition(sf::Vector2f(x, y));
  196. y+=0.1;
  197. }
  198. bool collide(float birdx,float birdy,float birdwidth,float birdheight)
  199. {
  200. if(y-height<=birdy+birdheight/2 && y>=birdy-birdheight/2) //canh tren gach<=canh duoi chim va canh duoi gach>=canh tren chim
  201. {
  202. 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
  203. {
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. };
  210. struct Rowbricks
  211. {
  212. vector<Brickblock> row;
  213. int brick_number;
  214. float rand_number,xx;
  215. Rowbricks()
  216. {
  217. rand_number=rand();
  218. brick_number=(int)rand_number%3+1;
  219. if(brick_number==1)
  220. {
  221. rand_number=rand();
  222. xx=(int)rand()%432;
  223. Brickblock brick;
  224. brick.setX(xx);
  225. row.push_back(brick);
  226. }
  227. if(brick_number==2)
  228. {
  229. for(int i=0; i<2; i++)
  230. {
  231. if(i==0)
  232. {
  233. Brickblock brick;
  234. rand_number=rand();
  235. xx=(int)rand_number%190;
  236. brick.setX(xx);
  237. row.push_back(brick);
  238. }
  239. if(i==1)
  240. {
  241. Brickblock brick;
  242. rand_number=rand();
  243. xx=(int)rand_number%190+241;
  244. brick.setX(xx);
  245. row.push_back(brick);
  246. }
  247. }
  248. }
  249. if(brick_number==3)
  250. {
  251. for(int i=0; i<3; i++)
  252. {
  253. if(i==0)
  254. {
  255. Brickblock brick;
  256. rand_number=rand();
  257. xx=(int)rand_number%120;
  258. brick.setX(xx);
  259. row.push_back(brick);
  260. }
  261. if(i==1)
  262. {
  263. Brickblock brick;
  264. rand_number=rand();
  265. xx=(int)rand_number%99+165;
  266. brick.setX(xx);
  267. row.push_back(brick);
  268. }
  269. if(i==2)
  270. {
  271. Brickblock brick;
  272. rand_number=rand();
  273. xx=(int)rand_number%31+400;
  274. brick.setX(xx);
  275. row.push_back(brick);
  276. }
  277. }
  278. }
  279. }
  280. void draw(sf::RenderWindow& window)
  281. {
  282. for(int i=0; i<brick_number; i++)
  283. {
  284. row[i].draw(window);
  285. }
  286. }
  287. void update(float window_width, float window_height)
  288. {
  289. for(int i=0; i<brick_number; i++)
  290. {
  291. row[i].update(window_width,window_height);
  292. }
  293. }
  294. bool collide(float birdx,float birdy,float birdwidth,float birdheight)
  295. {
  296. for(int i=0; i<brick_number; i++)
  297. {
  298. if(row[i].collide(birdx,birdy,birdwidth,birdheight)==true)
  299. return true;
  300. }
  301. return false;
  302. }
  303. };
  304. struct Allbricks
  305. {
  306. vector<Rowbricks>all;
  307. string yourscore;
  308.  
  309. void draw(sf::RenderWindow& window)
  310. {
  311. for(unsigned int i=0; i<all.size(); i++)
  312. {
  313. all[i].draw(window);
  314. }
  315. }
  316. void update(long timeloop,float window_width, float window_height)
  317. {
  318. if(timeloop%2250==0)
  319. {
  320. Rowbricks row;
  321. all.push_back(row);
  322. yourscore=to_string(all.size());
  323. //cout<<"time loop"<<" "<<timeloop<<endl;
  324. //cout<<"all.size"<<" "<<all.size()<<endl;
  325. }
  326. for(unsigned int i=0; i<all.size(); i++)
  327. {
  328. all[i].update(window_width,window_height);
  329. }
  330. }
  331. bool collide(float birdx,float birdy,float birdwidth,float birdheight)
  332. {
  333. for(unsigned int i=0; i<all.size(); i++)
  334. {
  335. if(all[i].collide(birdx,birdy,birdwidth,birdheight)==true)
  336. return true;
  337. }
  338. return false;
  339. }
  340. };
  341. /*struct Soundhit
  342. {
  343. sf::SoundBuffer buffer;
  344. sf::Sound sound;
  345. Soundhit()
  346. {
  347. if(!buffer.loadFromFile("Hit.wav"))
  348. cout<<"Error"<<endl;
  349. sound.setBuffer(buffer);
  350. }
  351. void playSoundhit()
  352. {
  353. sound.play();
  354. }
  355. };*/
  356. struct Status
  357. {
  358.  
  359. float window_width=432,window_height=768;
  360. Startgame startgame;
  361. Background background;
  362. Endgame endgame;
  363. Birdblock bird;
  364. //Brickblock brick;
  365. //Rowbricks row;
  366. Allbricks all;
  367. //Soundhit soundhit;
  368. string score;
  369. long timeloop=0;//225
  370. bool start=true;;
  371. bool gameover=false;
  372. void displayStatus(sf::RenderWindow& window)
  373. {
  374. if(start==true)
  375. {
  376. window.clear(sf::Color::Green);
  377. startgame.draw(window);
  378. }
  379. else
  380. {
  381. window.clear(sf::Color::Black);
  382. background.draw(window);
  383. //brick.draw(window);
  384. //row.draw(window);
  385. all.draw(window);
  386. bird.draw(window);
  387.  
  388. //draw score
  389. score=all.yourscore;
  390. textscore.setFont(fontscore);
  391. textscore.setString(score);
  392. textscore.setCharacterSize(30);
  393. textscore.setFillColor(color);
  394. textscore.setStyle(sf::Text::Bold);
  395. textscore.setPosition(sf::Vector2f(0,0));
  396. window.draw(textscore);
  397.  
  398. if(bird.y>=window_height)
  399. {
  400. window.clear(sf::Color::Black);
  401. endgame.draw(window);
  402. textscore.setCharacterSize(30);
  403. textscore.setFillColor(sf::Color::White);
  404. textscore.setStyle(sf::Text::Bold);
  405. textscore.setPosition(sf::Vector2f(210,370));
  406. window.draw(textscore);
  407. }
  408. }
  409.  
  410. window.display();
  411. }
  412.  
  413. void updateStatus()
  414. {
  415. if(start==true)
  416. {
  417. if(sf::Mouse::isButtonPressed(sf::Mouse::Left)||sf::Mouse::isButtonPressed(sf::Mouse::Right))
  418. start=false;
  419. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)||sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  420. start=false;
  421. }
  422. else
  423. {
  424. if(gameover==false)
  425. {
  426. if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
  427. bird.moveleft(window_width,window_height);
  428. else if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
  429. bird.moveright(window_width,window_height);
  430. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  431. bird.moveleft(window_width,window_height);
  432. else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  433. bird.moveright(window_width,window_height);
  434. else
  435. bird.fall(window_width,window_height);
  436. }
  437. else
  438. {
  439. bird.y+=-0.5;
  440. bird.falldown(window_width,window_height);
  441. }
  442. if(gameover==false)
  443. {
  444. //brick.update(window_width,window_height);
  445. //row.update(window_width,window_height);
  446. all.update(timeloop,window_width,window_height);
  447. }
  448. bird.update(window_width,window_height);
  449.  
  450. if(all.collide(bird.x,bird.y,bird.width,bird.height)==true||bird.y>=window_height)
  451. {
  452. //cout<<"cham gach"<<endl;
  453. //soundhit.playSoundhit();
  454. gameover=true;
  455. }
  456.  
  457. timeloop++;
  458. }
  459. }
  460.  
  461. };
  462. int main()
  463. {
  464. REPLAY:
  465. srand(time(0));
  466. Status status;
  467. sf::RenderWindow window(sf::VideoMode(status.window_width,status.window_height), "Dump Jump");
  468. if (!brickTexture.loadFromFile("brick.png"))
  469. {
  470. cout << "Error" << endl;
  471. }
  472. if (!fontscore.loadFromFile(".VnBahamasB.ttf"))
  473. {
  474. cout << "Error loading .VnBahamasB.ttf" << endl;
  475. }
  476. while (window.isOpen())
  477. {
  478. sf::Event event;
  479. while (window.pollEvent(event))
  480. {
  481. if (event.type == sf::Event::Closed)
  482. window.close();
  483. }
  484.  
  485. status.updateStatus();
  486. status.displayStatus(window);
  487. if(status.gameover==true)
  488. {
  489. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  490. {
  491. goto REPLAY;
  492. }
  493. }
  494. }
  495.  
  496. return 0;
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement