Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <whycpp/application_listener.h>
- #include <whycpp/whycpp.h>
- #include <whycpp/drawing.h>
- #include <whycpp/time.h>
- #include <cmath>
- #include <whycpp/color.h>
- #include <whycpp/buttons.h>
- #include <whycpp/input.h>
- #include <whycpp/palette.h>
- #include <whycpp/lifecycle.h>
- #include <vector>
- #include <algorithm>
- struct Rectangle
- {
- float x;
- float y;
- float width;
- float height;
- Rectangle(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
- bool IntersectsWith(Rectangle rect)
- {
- 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);
- }
- };
- class Bum {
- public:
- float x;
- float y;
- float width;
- float height;
- float live_time;
- const RGBA color = PALETTE[4];
- Bum(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
- void Draw(Context &ctx) {
- DrawRectFill(ctx, x, y, width, height, color);
- live_time += GetDelta(ctx);
- }
- Rectangle Rect() const {
- return {x,y,width,height};
- }
- bool ShouldBum() {
- return live_time > 2;
- }
- };
- class Bomb {
- public:
- float x;
- float y;
- float width;
- float height;
- float live_time;
- const RGBA color = PALETTE[11];
- Bomb(float x, float y, float width, float height) : x(x), y(y), width(width), height(height) {}
- void Draw(Context &ctx) {
- DrawRectFill(ctx, x, y, width, height, color);
- live_time += GetDelta(ctx);
- }
- Rectangle Rect() const {
- return {x,y,width,height};
- }
- bool ShouldBum() {
- return live_time > 5;
- }
- };
- class Man {
- public:
- float x;
- float y;
- float width;
- float height;
- float ms;
- float bomb_counter;
- float bomb_able;
- const std::vector<Bomb*> &bombs;
- const std::vector<Bum*> &bums;
- const RGBA color = PALETTE[6];
- bool isGameover = false;
- bool isGamestart = false;
- bool isSpace=false;
- float xset;
- float yset;
- Man(float x, float y, float width, float height, float ms, float bomb_counter, float bomb_able,
- const std::vector<Bomb *> &bombs, const std::vector<Bum *> &bums) : x(x), y(y), width(width), height(height),
- ms(ms), bomb_counter(bomb_counter),
- bomb_able(bomb_able), bombs(bombs),
- bums(bums) {}
- void Draw(Context &ctx) {
- DrawRectFill(ctx, x, y, width, height, color);
- }
- bool isGameOver() const {
- return isGameover;
- }
- bool isGameStart() const {
- return isGamestart;
- }
- void ProcessInput(Context &ctx) {
- if (IsPressed(ctx, Button::KEY_W)&&((int)y>0)) {
- y-=ms;
- }
- if (IsPressed(ctx, Button::KEY_S)&&((int)y<132)) {
- y+=ms;
- }
- if (IsPressed(ctx, Button::KEY_D)&&((int)x<244)) {
- x+=ms;
- }
- if (IsPressed(ctx, Button::KEY_A)&&((int)x>0)) {
- x-=ms;
- }
- Rectangle br = {x,y,width,height};
- for (auto& bombi:bombs)
- {
- auto wall_rect = bombi->Rect();
- if (br.IntersectsWith(wall_rect))
- {
- }
- }
- for (auto& bumi:bums)
- {
- auto wall_rect = bumi->Rect();
- if (br.IntersectsWith(wall_rect))
- {
- isGameover = true;
- }
- }
- }
- void printSpace(Context &ctx) {
- if (IsClicked(ctx,Button::KEY_SPACE)) {
- isSpace=true;
- } else {
- isSpace=false;
- }
- }
- };
- class Bomberman : public ApplicationListener {
- std::vector<Bomb*> bombs;
- std::vector<Bum*> bums;
- Man *man = nullptr;
- public :
- void OnCreate(Context &ctx)override {
- man= new Man(10, 10, 12, 12,1,0,1,bombs,bums);
- // bombs.push_back(new Bomb(50, 50, 12, 12));
- }
- void OnRender(Context &ctx) override {
- if (!man->isGameStart()) {
- DrawClearScreen(ctx, PALETTE[0]);
- if (IsClicked(ctx, Button::KEY_0)) {
- man->isGamestart = true;
- }
- }
- else{
- if(man->isGameover){
- delete (man);
- man= new Man(10, 10, 12, 12,1,0,1,bombs,bums);
- bums.clear();
- bombs.clear();
- man->isGamestart=false;
- man->isGameover=false;
- }
- DrawClearScreen(ctx, PALETTE[6]);
- DrawRectFill(ctx, 0, 0, 256, 144, PALETTE[1]);
- man->Draw(ctx);
- man->ProcessInput(ctx);
- for (auto & bombi: bombs){
- bombi->Draw(ctx);
- }
- for (auto & bumi: bums) {
- bumi->Draw(ctx);
- }
- // man->printSpace(ctx);
- if (IsClicked(ctx,Button::KEY_SPACE)&&((int)man->bomb_able>(int)man->bomb_counter)) {
- bombs.push_back(new Bomb(man->x, man->y, 12, 12));
- man->bomb_counter+=1;
- man->xset = man->x;
- man->yset = man->y;
- man->isSpace = false;
- }
- for (size_t i = 0; i < bombs.size(); i++) {
- if (bombs[i] == nullptr) continue;
- if (bombs[i]->ShouldBum()) {
- delete bombs[i];
- bombs[i] = nullptr;
- man->bomb_counter-=1;
- bums.push_back(new Bum(man->xset, man->yset, 12, 12));
- }
- }
- for (size_t i = 0; i < bums.size(); i++) {
- if (bums[i] == nullptr) continue;
- if (bums[i]->ShouldBum()) {
- delete bums[i];
- bums[i] = nullptr;
- }
- }
- auto to_delete = std::remove_if(bombs.begin(), bombs.end(), [](Bomb* b){
- return b == nullptr;
- });
- bombs.erase(to_delete, bombs.end());
- auto to_delete1 = std::remove_if(bums.begin(), bums.end(), [](Bum* d){
- return d == nullptr;
- });
- bums.erase(to_delete1, bums.end());
- }
- }
- };
- int main(int argc, char *argv[]){
- RunApp<Bomberman>(ApplicationConfig(256, 144, "Bomberman", false));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement