Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 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;
  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;
  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. float bumsize=-1;
  105. float 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<132)) {
  131. y+=ms;
  132. }
  133. if (IsPressed(ctx, Button::KEY_D)&&((int)x<244)) {
  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(10, 10, 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(10, 10, 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. bombs.push_back(new Bomb(man->x, man->y, 16, 16));
  208. man->bomb_counter+=1;
  209. man->xset = man->x;
  210. man->yset = man->y;
  211. man->isSpace = false;
  212. }
  213.  
  214. for (size_t i = 0; i < bombs.size(); i++) {
  215. if (bombs[i] == nullptr) continue;
  216. if (bombs[i]->ShouldBum()) {
  217. delete bombs[i];
  218. bombs[i] = nullptr;
  219. man->bomb_counter-=1;
  220. // for (int j = 0 ; j<=3 ; j ++ ) {
  221. // }
  222. // for (int i = 0 ; i<=3 ; i ++ ) {
  223. // bums.push_back(new Bum ((man->x + 24 + 12*i), man->yset, 12 , 12));
  224. // }
  225. // bums.push_back(new Bum (man->xset, man->yset, (man->y + 24 + 12*j) , (12)));
  226. // bums.push_back(new Bum (man->xset, man->yset, (12) , (12)));
  227. // for (bum->bumsize=-1 ; bum->bumsize <=bum->number; bum->bumsize ++ ) {
  228. // bums.push_back(new Bum((man->xset + bum->bumsize*12), man->yset, 12, 12));
  229. // }
  230. // for (bum->bumsize=-2 ; bum->bumsize <= bum->number + 1; bum->bumsize ++ ) {
  231. // bums.push_back(new Bum (man->xset , (man->yset + bum->bumsize*12), 12, 12));
  232. // }
  233. for (int counter = (int)man->bumsize ; counter < (int)man->bumnumber ; counter ++ ) {
  234. bums.push_back(new Bum(man->xset , (man->yset+counter*16), 16, 16));
  235. }
  236. for (int counter = (int)man->bumsize - 1; counter < (int)man->bumnumber + 1 ; counter ++ ) {
  237. bums.push_back(new Bum((man->xset+counter*16) , man->yset, 16, 16));
  238. }
  239.  
  240. }
  241. }
  242. for (size_t i = 0; i < bums.size(); i++) {
  243. if (bums[i] == nullptr) continue;
  244. if (bums[i]->ShouldBum()) {
  245. delete bums[i];
  246. bums[i] = nullptr;
  247. }
  248. }
  249.  
  250.  
  251.  
  252. auto to_delete = std::remove_if(bombs.begin(), bombs.end(), [](Bomb* b){
  253. return b == nullptr;
  254. });
  255. bombs.erase(to_delete, bombs.end());
  256. auto to_delete1 = std::remove_if(bums.begin(), bums.end(), [](Bum* d){
  257. return d == nullptr;
  258. });
  259. bums.erase(to_delete1, bums.end());
  260. }
  261.  
  262. }
  263. };
  264.  
  265.  
  266.  
  267. int main(int argc, char *argv[]){
  268. RunApp<Bomberman>(ApplicationConfig{256, 144, "Bomberman", false});
  269. return 0;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement