Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <whycpp/application_listener.h>
  3. #include <whycpp/whycpp.h>
  4. #include <whycpp/drawing.h>
  5. #include <whycpp/time.h>
  6. #include <cmath>
  7. #include <whycpp/color.h>
  8. #include <whycpp/buttons.h>
  9. #include <whycpp/input.h>
  10. #include <whycpp/palette.h>
  11. #include <whycpp/lifecycle.h>
  12. #include <vector>
  13. #include <algorithm>
  14.  
  15. struct Rectangle
  16. {
  17. float x;
  18. float y;
  19. float width;
  20. float height;
  21.  
  22. Rectangle(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
  23.  
  24. bool IntersectsWith(Rectangle rect)
  25. {
  26. return (rect.x < this->x + this->width) && (this->x < (rect.x + rect.width)) && (rect.y < this->y + this->height) && (this->y < rect.y + rect.height);
  27. }
  28. };
  29.  
  30. class Bum {
  31. public:
  32. float x;
  33. float y;
  34. float width;
  35. float height;
  36. float live_time=0;
  37. const RGBA color = PALETTE[4];
  38.  
  39. Bum(float x, float y, float width, float height ) : x(x), y(y), width(width), height(height) {}
  40.  
  41. void Draw(Context &ctx) {
  42. DrawRectFill(ctx, x, y, width, height, color);
  43. live_time += GetDelta(ctx);
  44. }
  45.  
  46. Rectangle Rect() const {
  47. return {x,y,width,height};
  48. }
  49.  
  50.  
  51.  
  52. bool ShouldBum() {
  53. return live_time > 2;
  54. }
  55. };
  56.  
  57. class Bomb {
  58. public:
  59. float x;
  60. float y;
  61. float width;
  62. float height;
  63. float live_time=0;
  64. const RGBA color = PALETTE[11];
  65.  
  66. Bomb(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
  67.  
  68.  
  69. void Draw(Context &ctx) {
  70. DrawRectFill(ctx, x, y, width, height, color);
  71. live_time += GetDelta(ctx);
  72. }
  73.  
  74.  
  75.  
  76. Rectangle Rect() const {
  77. return {x,y,width,height};
  78. }
  79.  
  80. bool ShouldBum() {
  81. return live_time > 5;
  82. }
  83. };
  84.  
  85.  
  86.  
  87. class Man {
  88. public:
  89. float x;
  90. float y;
  91. float width;
  92. float height;
  93. float ms;
  94. float bomb_counter;
  95. float bomb_able;
  96. const std::vector<Bomb*> &bombs;
  97. const std::vector<Bum*> &bums;
  98. const RGBA color = PALETTE[6];
  99. bool isGameover = false;
  100. bool isGamestart = false;
  101. bool isSpace=false;
  102. float xset;
  103. float yset;
  104. int bumsize=-1;
  105. int bumnumber=2;
  106.  
  107. Man(float x, float y, float width, float height, float ms, float bomb_counter, float bomb_able,
  108. const std::vector<Bomb *> &bombs, const std::vector<Bum *> &bums) : x(x), y(y), width(width), height(height),
  109. ms(ms), bomb_counter(bomb_counter),
  110. bomb_able(bomb_able), bombs(bombs),
  111. bums(bums) {}
  112.  
  113.  
  114. void Draw(Context &ctx) {
  115. DrawRectFill(ctx, x, y, width, height, color);
  116. }
  117.  
  118. bool isGameOver() const {
  119. return isGameover;
  120. }
  121.  
  122. bool isGameStart() const {
  123. return isGamestart;
  124. }
  125.  
  126. void ProcessInput(Context &ctx) {
  127. if (IsPressed(ctx, Button::KEY_W)&&((int)y>0)) {
  128. y-=ms;
  129. }
  130. if (IsPressed(ctx, Button::KEY_S)&&((int)y<128)) {
  131. y+=ms;
  132. }
  133. if (IsPressed(ctx, Button::KEY_D)&&((int)x<240)) {
  134. x+=ms;
  135. }
  136. if (IsPressed(ctx, Button::KEY_A)&&((int)x>0)) {
  137. x-=ms;
  138. }
  139. Rectangle br = {x,y,width,height};
  140. for (auto& bombi:bombs)
  141. {
  142. auto wall_rect = bombi->Rect();
  143. if (br.IntersectsWith(wall_rect))
  144. {
  145. }
  146. }
  147. for (auto& bumi:bums)
  148. {
  149. auto wall_rect = bumi->Rect();
  150. if (br.IntersectsWith(wall_rect))
  151. {
  152. isGameover = true;
  153. }
  154. }
  155. }
  156. // void printSpace(Context &ctx) {
  157. // if (IsClicked(ctx,Button::KEY_SPACE)) {
  158. // isSpace=true;
  159. // } else {
  160. // isSpace=false;
  161. // }
  162. // }
  163. };
  164.  
  165.  
  166.  
  167.  
  168.  
  169. class Bomberman : public ApplicationListener {
  170. std::vector<Bomb*> bombs;
  171. std::vector<Bum*> bums;
  172. Man *man = nullptr;
  173. public :
  174. void OnCreate(Context &ctx)override {
  175. man= new Man(240, 128, 16, 16,1,0,1,bombs,bums);
  176. // bombs.push_back(new Bomb(50, 50, 12, 12));
  177. }
  178. void OnRender(Context &ctx) override {
  179. if (!man->isGameStart()) {
  180. DrawClearScreen(ctx, PALETTE[0]);
  181. if (IsClicked(ctx, Button::KEY_0)) {
  182.  
  183. man->isGamestart = true;
  184. }
  185. }
  186. else{
  187. if(man->isGameover){
  188. delete (man);
  189. man= new Man(0, 0, 16, 16,1,0,1,bombs,bums);
  190. bums.clear();
  191. bombs.clear();
  192. man->isGamestart=false;
  193. man->isGameover=false;
  194. }
  195. DrawClearScreen(ctx, PALETTE[6]);
  196. DrawRectFill(ctx, 0, 0, 256, 144, PALETTE[1]);
  197. man->Draw(ctx);
  198. man->ProcessInput(ctx);
  199. for (auto & bombi: bombs){
  200. bombi->Draw(ctx);
  201. }
  202. for (auto & bumi: bums) {
  203. bumi->Draw(ctx);
  204. }
  205. // man->printSpace(ctx);
  206. if (IsClicked(ctx,Button::KEY_SPACE)&&((int)man->bomb_able>(int)man->bomb_counter)) {
  207. int trouble = (int) man->x % 16;
  208. if (trouble < 8 ) {
  209. man->xset = (int) man->x - trouble;
  210. } else {
  211. man->xset = (int) man->x + (16-trouble);
  212. }
  213. trouble = (int) man->y % 16;
  214. if (trouble < 8 ) {
  215. man->yset = (int) man->y - trouble;
  216. } else {
  217. man->yset = (int) man->y + (16 - trouble);
  218. }
  219. bombs.push_back(new Bomb(man->xset, man->yset, 16, 16));
  220. man->bomb_counter+=1;
  221. trouble= (int) man->x;
  222. // man->xset = man->x;
  223. // man->yset = man->y;
  224. man->isSpace = false;
  225. }
  226.  
  227. for (size_t i = 0; i < bombs.size(); i++) {
  228. if (bombs[i] == nullptr) continue;
  229. if (bombs[i]->ShouldBum()) {
  230. delete bombs[i];
  231. bombs[i] = nullptr;
  232. man->bomb_counter-=1;
  233. // for (int j = 0 ; j<=3 ; j ++ ) {
  234. // }
  235. // for (int i = 0 ; i<=3 ; i ++ ) {
  236. // bums.push_back(new Bum ((man->x + 24 + 12*i), man->yset, 12 , 12));
  237. // }
  238. // bums.push_back(new Bum (man->xset, man->yset, (man->y + 24 + 12*j) , (12)));
  239. // bums.push_back(new Bum (man->xset, man->yset, (12) , (12)));
  240. // for (bum->bumsize=-1 ; bum->bumsize <=bum->number; bum->bumsize ++ ) {
  241. // bums.push_back(new Bum((man->xset + bum->bumsize*12), man->yset, 12, 12));
  242. // }
  243. // for (bum->bumsize=-2 ; bum->bumsize <= bum->number + 1; bum->bumsize ++ ) {
  244. // bums.push_back(new Bum (man->xset , (man->yset + bum->bumsize*12), 12, 12));
  245. // }
  246. std::cout << bums.size() <<std::endl;
  247. for (int counter = man->bumsize ; counter < man->bumnumber; counter ++ ) {
  248. bums.push_back(new Bum((man->xset) , (man->yset-counter*16), 16, 16));
  249. }
  250. for (int counter = man->bumsize - 1; counter < man->bumnumber + 1 ; counter ++ ) {
  251. if (counter!=0) {
  252. bums.push_back(new Bum((man->xset-counter*16) , (man->yset), 16, 16));
  253.  
  254. }
  255.  
  256. }
  257. std::cout << bums.size() <<std::endl;
  258. }
  259. }
  260. for (size_t i = 0; i < bums.size(); i++) {
  261. if (bums[i] == nullptr) continue;
  262. if (bums[i]->ShouldBum()) {
  263. delete bums[i];
  264. bums[i] = nullptr;
  265. }
  266. }
  267.  
  268.  
  269.  
  270. auto to_delete = std::remove_if(bombs.begin(), bombs.end(), [](Bomb* b){
  271. return b == nullptr;
  272. });
  273. bombs.erase(to_delete, bombs.end());
  274. auto to_delete1 = std::remove_if(bums.begin(), bums.end(), [](Bum* d){
  275. return d == nullptr;
  276. });
  277. bums.erase(to_delete1, bums.end());
  278. }
  279.  
  280. }
  281. };
  282.  
  283.  
  284.  
  285. int main(int argc, char *argv[]){
  286. RunApp<Bomberman>(ApplicationConfig{256, 144, "Bomberman", false});
  287. return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement