Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 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.  
  38. const RGBA color = PALETTE[4];
  39.  
  40. Bum(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
  41.  
  42. void Draw(Context &ctx) {
  43. DrawRectFill(ctx, x, y, width, height, color);
  44. live_time += GetDelta(ctx);
  45. }
  46.  
  47. Rectangle Rect() const {
  48. return {x,y,width,height};
  49. }
  50.  
  51.  
  52.  
  53. bool ShouldBum() {
  54. return live_time > 2;
  55. }
  56. };
  57.  
  58. class Bomb {
  59. public:
  60. float x;
  61. float y;
  62. float width;
  63. float height;
  64. float live_time;
  65. const RGBA color = PALETTE[11];
  66.  
  67. Bomb(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
  68.  
  69.  
  70. void Draw(Context &ctx) {
  71. DrawRectFill(ctx, x, y, width, height, color);
  72. live_time += GetDelta(ctx);
  73. }
  74.  
  75.  
  76.  
  77. Rectangle Rect() const {
  78. return {x,y,width,height};
  79. }
  80.  
  81. bool ShouldBum() {
  82. return live_time > 5;
  83. }
  84. };
  85.  
  86.  
  87.  
  88. class Man {
  89. public:
  90. float x;
  91. float y;
  92. float width;
  93. float height;
  94. float ms;
  95. float bomb_counter;
  96. float bomb_able;
  97. const std::vector<Bomb*> &bombs;
  98. const std::vector<Bum*> &bums;
  99. const RGBA color = PALETTE[6];
  100. bool isGameover = false;
  101. bool isGamestart = false;
  102. bool isSpace=false;
  103. float xset;
  104. float yset;
  105.  
  106. Man(float x, float y, float width, float height, float ms, float bomb_counter, float bomb_able,
  107. const std::vector<Bomb *> &bombs, const std::vector<Bum *> &bums) : x(x), y(y), width(width), height(height),
  108. ms(ms), bomb_counter(bomb_counter),
  109. bomb_able(bomb_able), bombs(bombs),
  110. bums(bums) {}
  111.  
  112.  
  113. void Draw(Context &ctx) {
  114. DrawRectFill(ctx, x, y, width, height, color);
  115. }
  116.  
  117. bool isGameOver() const {
  118. return isGameover;
  119. }
  120.  
  121. bool isGameStart() const {
  122. return isGamestart;
  123. }
  124.  
  125. void ProcessInput(Context &ctx) {
  126. if (IsPressed(ctx, Button::KEY_W)&&((int)y>0)) {
  127. y-=ms;
  128. }
  129. if (IsPressed(ctx, Button::KEY_S)&&((int)y<132)) {
  130. y+=ms;
  131. }
  132. if (IsPressed(ctx, Button::KEY_D)&&((int)x<244)) {
  133. x+=ms;
  134. }
  135. if (IsPressed(ctx, Button::KEY_A)&&((int)x>0)) {
  136. x-=ms;
  137. }
  138. Rectangle br = {x,y,width,height};
  139. for (auto& bombi:bombs)
  140. {
  141. auto wall_rect = bombi->Rect();
  142. if (br.IntersectsWith(wall_rect))
  143. {
  144. }
  145. }
  146. for (auto& bumi:bums)
  147. {
  148. auto wall_rect = bumi->Rect();
  149. if (br.IntersectsWith(wall_rect))
  150. {
  151. isGameover = true;
  152. }
  153. }
  154. }
  155. void printSpace(Context &ctx) {
  156. if (IsClicked(ctx,Button::KEY_SPACE)) {
  157. isSpace=true;
  158. } else {
  159. isSpace=false;
  160. }
  161. }
  162. };
  163.  
  164.  
  165.  
  166.  
  167.  
  168. class Bomberman : public ApplicationListener {
  169. std::vector<Bomb*> bombs;
  170. std::vector<Bum*> bums;
  171. Man *man = nullptr;
  172. public :
  173. void OnCreate(Context &ctx)override {
  174. man= new Man(10, 10, 12, 12,1,0,1,bombs,bums);
  175. // bombs.push_back(new Bomb(50, 50, 12, 12));
  176. }
  177. void OnRender(Context &ctx) override {
  178. if (!man->isGameStart()) {
  179. DrawClearScreen(ctx, PALETTE[0]);
  180. if (IsClicked(ctx, Button::KEY_0)) {
  181.  
  182. man->isGamestart = true;
  183. }
  184. }
  185. else{
  186. if(man->isGameover){
  187. delete (man);
  188. man= new Man(10, 10, 12, 12,1,0,1,bombs,bums);
  189. bums.clear();
  190. bombs.clear();
  191. man->isGamestart=false;
  192. man->isGameover=false;
  193. }
  194. DrawClearScreen(ctx, PALETTE[6]);
  195. DrawRectFill(ctx, 0, 0, 256, 144, PALETTE[1]);
  196. man->Draw(ctx);
  197. man->ProcessInput(ctx);
  198. for (auto & bombi: bombs){
  199. bombi->Draw(ctx);
  200. }
  201. for (auto & bumi: bums) {
  202. bumi->Draw(ctx);
  203. }
  204. // man->printSpace(ctx);
  205. if (IsClicked(ctx,Button::KEY_SPACE)&&((int)man->bomb_able>(int)man->bomb_counter)) {
  206. bombs.push_back(new Bomb(man->x, man->y, 12, 12));
  207. man->bomb_counter+=1;
  208. man->xset = man->x;
  209. man->yset = man->y;
  210. man->isSpace = false;
  211. }
  212.  
  213. for (size_t i = 0; i < bombs.size(); i++) {
  214. if (bombs[i] == nullptr) continue;
  215. if (bombs[i]->ShouldBum()) {
  216. delete bombs[i];
  217. bombs[i] = nullptr;
  218. man->bomb_counter-=1;
  219. bums.push_back(new Bum(man->xset, man->yset, 12, 12));
  220. }
  221. }
  222. for (size_t i = 0; i < bums.size(); i++) {
  223. if (bums[i] == nullptr) continue;
  224. if (bums[i]->ShouldBum()) {
  225. delete bums[i];
  226. bums[i] = nullptr;
  227. }
  228. }
  229.  
  230.  
  231.  
  232. auto to_delete = std::remove_if(bombs.begin(), bombs.end(), [](Bomb* b){
  233. return b == nullptr;
  234. });
  235. bombs.erase(to_delete, bombs.end());
  236. auto to_delete1 = std::remove_if(bums.begin(), bums.end(), [](Bum* d){
  237. return d == nullptr;
  238. });
  239. bums.erase(to_delete1, bums.end());
  240. }
  241.  
  242. }
  243. };
  244.  
  245.  
  246.  
  247. int main(int argc, char *argv[]){
  248. RunApp<Bomberman>(ApplicationConfig(256, 144, "Bomberman", false));
  249. return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement