Advertisement
Guest User

Bomberman?

a guest
May 29th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.06 KB | None | 0 0
  1. // bomberman.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <windows.h>
  6. #include <gl/gl.h>
  7. #include "glut.h"
  8. #include <math.h>
  9. #include <iostream>
  10. #include <time.h>
  11. #define SIZE 19
  12. using namespace std;
  13. //Player
  14. struct point {
  15.     int x;
  16.     int y;
  17. }position;
  18.  
  19. struct bomb {
  20.     int x;
  21.     int y;
  22. }posb;
  23.  
  24.  
  25.  
  26. typedef float point2[2];
  27.  
  28. int field[SIZE][SIZE];
  29. int condition;
  30. int TimeA;
  31. int TimeB=(TimeA+10000);
  32.  
  33.  
  34.  
  35. //Bomb Right
  36. int BR() {
  37.  
  38.     int Bx;
  39.     int By;
  40.     TimeB = TimeA + 10000;
  41.     Bx = (position.x + 1);
  42.     By = position.y;
  43.  
  44.     field[Bx][By] = 5;
  45.     if (TimeA < TimeB)
  46.     if(TimeA=TimeB){
  47.         TimeA++;
  48.         if (TimeA = TimeB) {
  49.  
  50.         if (field[Bx + 1][By] != 2)
  51.             field[Bx + 1][By] = 0;
  52.  
  53.         if (field[Bx][By - 1] != 2)
  54.             field[Bx][By - 1] = 0;
  55.  
  56.         if (field[Bx][By + 1] != 2)
  57.             field[Bx][By + 1] = 0;
  58.  
  59.         if (field[Bx - 1][By] != 2)
  60.             field[Bx - 1][By] = 0;
  61.  
  62.         field[Bx][By] = 0;
  63.  
  64.     }
  65.     }
  66.     return 0;
  67. }
  68. //Bomb Left
  69. int BL() {
  70.     int Bx;
  71.     int By;
  72.     TimeB = TimeA + 10000;
  73.     Bx = (position.x - 1);
  74.     By = (position.y);
  75.  
  76.     field[Bx][By] = 5;
  77.  
  78.     if (TimeA < TimeB) {
  79.         TimeA++;
  80.         if(TimeA=TimeB)
  81.         {
  82.  
  83.             if (field[Bx + 1][By] != 2)
  84.                 field[Bx + 1][By] = 0;
  85.  
  86.             if (field[Bx][By - 1] != 2)
  87.                 field[Bx][By - 1] = 0;
  88.  
  89.             if (field[Bx][By + 1] != 2)
  90.                 field[Bx][By + 1] = 0;
  91.  
  92.             if (field[Bx - 1][By] != 2)
  93.                 field[Bx - 1][By] = 0;
  94.  
  95.             field[Bx][By] = 0;
  96.  
  97.         }
  98.     }
  99.     return 0;
  100. }
  101.  
  102. //Bomb Up
  103. int BUP() {
  104.     int Bx;
  105.     int By;
  106.     TimeB = TimeA + 10000;
  107.  
  108.     Bx = position.x;
  109.     By = (position.y - 1);
  110.  
  111.     field[Bx][By] = 5;
  112.  
  113.     if (TimeA <= TimeB) {
  114.         TimeA++;
  115.         if (TimeA = TimeB) {
  116.  
  117.             if (field[Bx + 1][By] != 2)
  118.                 field[Bx + 1][By] = 0;
  119.  
  120.             if (field[Bx][By - 1] != 2)
  121.                 field[Bx][By - 1] = 0;
  122.  
  123.             if (field[Bx][By + 1] != 2)
  124.                 field[Bx][By + 1] = 0;
  125.  
  126.             if (field[Bx - 1][By] != 2)
  127.                 field[Bx - 1][By] = 0;
  128.  
  129.             field[Bx][By] = 0;
  130.  
  131.         }
  132.     }
  133.     return 0;
  134. }
  135.  
  136. int BDWN() {
  137.     int Bx;
  138.     int By;
  139.     TimeB = (TimeA + 10000);
  140.  
  141.     Bx = position.x;
  142.     By = (position.y + 1);
  143.  
  144.     field[Bx][By] = 5;
  145.  
  146.     if (TimeA <= TimeB) {
  147.         TimeA++;
  148.         if (TimeA = TimeB) {
  149.  
  150.             if (field[Bx][By + 1] != 2)
  151.                 field[Bx][By + 1] = 0;
  152.  
  153.             if (field[Bx + 1][By] != 2)
  154.                 field[Bx + 1][By] = 0;
  155.  
  156.             if (field[Bx - 1][By] != 2)
  157.                 field[Bx - 1][By] = 0;
  158.  
  159.             if (field[Bx][By - 1] != 2)
  160.                 field[Bx][By - 1] = 0;
  161.  
  162.             field[Bx][By] = 0;
  163.  
  164.         }
  165.     }
  166.     return 0;
  167. }
  168.  
  169.  
  170.  
  171. void MyInit(void){
  172. {
  173.         //The void color(filling/background)
  174.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  175.         //Spawning the character
  176.     position.x = 1;
  177.     position.y = 1;
  178.         //Condition/Movement
  179.     condition = 10;
  180.    
  181.     TimeA = 0;
  182.     TimeA++;
  183.  
  184.     }
  185.  
  186.  
  187.     //Setting the map
  188.     for (int i = 0; i < SIZE; ++i)
  189.     {
  190.         for (int j = 0; j < SIZE; ++j)
  191.         {
  192.             if (i == position.x && j == position.y)
  193.             {
  194.                 field[i][j] = 1;
  195.             }
  196.             else if (i == 0 || i == SIZE - 1 || j == 0 || j == SIZE - 1)
  197.             {
  198.                 field[i][j] = 2;
  199.             }
  200.             else
  201.             {
  202.                 field[i][j] = 0;
  203.             }
  204.             if (i == posb.x && j == posb.y)
  205.             {
  206.                 field[i][j] = 5;
  207.             }
  208.         }
  209.     }
  210.  
  211.     for (int i = 0; i < SIZE; i += 2)
  212.     {
  213.         for (int j = 0; j < SIZE; j += 2)
  214.         {
  215.             if (i % 2 == 0)
  216.             {
  217.                 field[i][j] = 2;
  218.             }
  219.         }
  220.     }
  221.  
  222.     for (int i = 0; i < SIZE; ++i)
  223.     {
  224.         for (int j = 0; j < SIZE; ++j)
  225.         {
  226.             if (field[i][j] == 0)
  227.             {
  228.                 if (rand() > RAND_MAX / 100)
  229.                 {
  230.                     field[i][j] = 3;
  231.                 }
  232.             }
  233.         }
  234.     }
  235.  
  236.     field[1][2] = 4; //Starting area(Right block of start-area)
  237.     field[2][1] = 4; //Starting area(Down block of start-area)
  238. }
  239.  
  240. void tbomb(){
  241.         for (TimeA = 0; TimeA < 100; TimeA++) {
  242.             if (field[posb.x - 1][posb.y] != 2) { field[posb.x - 1][posb.y] = 0; }
  243.             if (field[posb.x + 1][posb.y] != 2) { field[posb.x + 1][posb.y] = 0; }
  244.             if (field[posb.x][posb.y - 1] != 2) { field[posb.x][posb.y - 1] = 0; }
  245.             if (field[posb.x][posb.y + 1] != 2) { field[posb.x][posb.y + 1] = 0; }
  246.             if (field[posb.x][posb.y] != 2) { field[posb.x][posb.y] = 0; }
  247.         }
  248. }
  249.  
  250. void Run()
  251. {
  252.     TimeA++;
  253.  
  254.  
  255.     //Movement
  256.     switch (condition)
  257.     {
  258.     case 0:
  259.         if (position.x != 0) {
  260.             if (field[position.x + 1][position.y] != 2
  261.                 && field[position.x + 1][position.y] != 3
  262.                 && field[position.x + 1][position.y] != 5
  263.                 && field[position.x + 1][position.y] != field[posb.x][posb.y])
  264.                 {
  265.                 if (field[position.x][position.y] != field[posb.x][posb.y]) {
  266.                     field[position.x][position.y] = 0;
  267.                 }
  268.                 position.x++;
  269.                 field[position.x][position.y] = 1;
  270.             }
  271.         }
  272.  
  273.         condition = 10; // To stop the player
  274.  
  275.         break;
  276.  
  277.     case 1:
  278.         if (position.y != 0) {
  279.             if (field[position.x][position.y + 1] != 2
  280.                 && field[position.x][position.y + 1] != 3
  281.                 && field[position.x][position.y + 1] != 5
  282.                 && field[position.x][position.y + 1] != field[posb.x][posb.y])
  283.                 {
  284.                 if (field[position.x][position.y] != field[posb.x][posb.y]) {
  285.                     field[position.x][position.y] = 0;
  286.                 }
  287.                 position.y++;
  288.                 field[position.x][position.y] = 1;
  289.                 }
  290.         }
  291.  
  292.         condition = 10;
  293.         break;
  294.  
  295.     case 2:
  296.         if (position.x != 0) {
  297.             if (field[position.x - 1][position.y] != 2
  298.                 && field[position.x - 1][position.y] != 3
  299.                 && field[position.x - 1][position.y] != 5
  300.                 && field[position.x - 1][position.y] != field[posb.x][posb.y])
  301.                 {
  302.                 if (field[position.x][position.y] != field[posb.x][posb.y]) {
  303.                     field[position.x][position.y] = 0;
  304.                 }
  305.                 position.x--;
  306.                 field[position.x][position.y] = 1;
  307.             }
  308.         }
  309.  
  310.         condition = 10;
  311.         break;
  312.  
  313.     case 3:
  314.         if (position.y != 0) {
  315.             if (field[position.x][position.y - 1] != 2
  316.                 && field[position.x][position.y - 1] != 3
  317.                 && field[position.x][position.y - 1] != 5
  318.                 && field[position.x][position.y - 1] != field[posb.x][posb.y]) {
  319.  
  320.                 if (field[position.x][position.y] != field[posb.x][posb.y]) {
  321.                     field[position.x][position.y] = 0;
  322.                 }
  323.                 position.y--;
  324.                 field[position.x][position.y] = 1;
  325.             }
  326.         }
  327.  
  328.         condition = 10;
  329.         break;
  330.  
  331.         //Bomb initiation on 'k' key
  332.     case 4:
  333.         field[position.x][position.y] = field[posb.x][posb.y];
  334.         tbomb();
  335.        
  336.  
  337.         break;
  338.  
  339.     case 5:
  340.         if (field[position.x + 1][position.y] != 2 && field[position.x + 1][position.y] != 3 && field[position.x + 1][position.y] != 5)
  341.             BR();
  342.         break;
  343.  
  344.     case 6:
  345.         if (field[position.x - 1][position.y] != 2 && field[position.x - 1][position.y] != 3 && field[position.x - 1][position.y] != 5)
  346.             BL();
  347.         break;
  348.  
  349.     case 7:
  350.         if (field[position.x][position.y - 1] != 2 && field[position.x][position.y - 1] != 3 && field[position.x][position.y - 1] != 5)
  351.             BUP();
  352.         break;
  353.  
  354.     case 8:
  355.         if (field[position.x][position.y + 1] != 2 && field[position.x][position.y + 1] != 3 && field[position.x][position.y + 1] != 5)
  356.             BDWN();
  357.         break;
  358.  
  359.     case 9:
  360.         field[position.x][position.y] = 0;
  361.         break;
  362.  
  363.     default:
  364.         break;
  365.         }
  366.  
  367.  
  368.  
  369.         Sleep(1);
  370.         glutPostRedisplay();
  371.         }
  372.             // The squares
  373.         void square(point location, float cr, float cg, float cb)
  374.         {
  375.             point2 a = { -1.0f + 0.1f * location.x, 1.0f - 0.1f * location.y };
  376.             point2 b = { -1.0f + 0.1f * location.x + 0.1f, 1.0f - 0.1f * location.y };
  377.             point2 c = { -1.0f + 0.1f * location.x + 0.1f, 1.0f - 0.1f * location.y - 0.1f };
  378.             point2 d = { -1.0f + 0.1f * location.x, 1.0f - 0.1f * location.y - 0.1f };
  379.             glBegin(GL_QUADS);
  380.             glColor3f(cr, cg, cb);
  381.             glVertex2fv(a);
  382.             glColor3f(cr, cg, cb);
  383.             glVertex2fv(b);
  384.             glColor3f(cr, cg, cb);
  385.             glVertex2fv(c);
  386.             glColor3f(cr, cg, cb);
  387.             glVertex2fv(d);
  388.             glEnd();
  389.         }
  390.  
  391.  
  392.             // The player's point
  393.         void smallsquare(point location)
  394.         {
  395.             point2 a = { -1.0f + 0.1f * location.x + 0.05f, 1.0f - 0.2f * location.y - 0.05f };
  396.             point2 b = { -1.0f + 0.1f * location.x + 0.1f - 0.05f, 1.0f - 0.1f * location.y - 0.05f };
  397.             point2 c = { -1.0f + 0.1f * location.x + 0.1f - 0.05f, 1.0f - 0.1f * location.y - 0.1f + 0.05f };
  398.             point2 d = { -1.0f + 0.1f * location.x + 0.05f, 1.0f - 0.1f * location.y - 0.1f + 0.05f };
  399.             glColor3f(1.0f, 0.0f, 0.0f);
  400.             glBegin(GL_QUADS);
  401.             glVertex2fv(a);
  402.             glVertex2fv(b);
  403.             glVertex2fv(c);
  404.             glVertex2fv(d);
  405.             glEnd();
  406.         }
  407.  
  408.         void RenderScene(void)
  409.         {
  410.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  411.             glLoadIdentity();
  412.             point tmp;
  413.             //Drawing the map
  414.             for (int i = 0; i < SIZE; i++) {
  415.                 for (int j = 0; j < SIZE; j++) {
  416.                     tmp.x = i;
  417.                     tmp.y = j;
  418.                     if (field[i][j] == 0)
  419.                         square(tmp, 1.0, 1.0, 1.0);
  420.                     if (field[i][j] == 1)
  421.                         square(tmp, 1.0, 0.0, 0.0);
  422.                     if (field[i][j] == 2)
  423.                         square(tmp, 0.0, 0.0, 1.0);
  424.                     if (field[i][j] == 3)
  425.                         square(tmp, 0.0, 1.0, 1.0);
  426.                     if (field[i][j] == 4)
  427.                         square(tmp, 1.0, 1.0, 1.0);
  428.                     if (field[i][j] == 5)
  429.                         square(tmp, 1.0, 1.0, 1.0);
  430.                 }
  431.             }
  432.             //Drawing the player
  433.             smallsquare(position);
  434.  
  435.             glutSwapBuffers();
  436.  
  437.         }
  438.  
  439.             //Inputkeys
  440.         void keys(unsigned char key, int x, int y)
  441.         {
  442.             //Input movement keys
  443.             if (key == 'w') condition = 3;
  444.             if (key == 'a') condition = 2;
  445.             if (key == 's') condition = 1;
  446.             if (key == 'd') condition = 0;
  447.  
  448.             //Input keys creating the bomb
  449.  
  450.             if (key == 'k') condition = 4;//Bombonme
  451.             if (key == 'l') condition = 5;//Bombonright
  452.             if (key == 'j') condition = 6;//Bombonleft
  453.             if (key == 'i') condition = 7;//Bombonup
  454.             if (key == ',') condition = 8;//Bombondown
  455.             if (key == 'x') condition = 9;//Cleanmespace
  456.  
  457.  
  458.             RenderScene();
  459.         }
  460.  
  461.         int main(void)
  462.         {
  463.             srand(time(0));
  464.             glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  465.             glutInitWindowSize(800, 800);
  466.             // Initializing the Window size
  467.             glutCreateWindow("Bomberman");
  468.             // Naming the Window
  469.             glutDisplayFunc(RenderScene);
  470.             // RenderScene as a return function
  471.             // (callback function).  It'll be ommited everytime when redrawing the window will be needed
  472.             glutKeyboardFunc(keys);
  473.             // Return function detecting the state of the keyboard
  474.             glutIdleFunc(Run);
  475.             MyInit();
  476.             // Function MyInit() (defined above) executes every pre-settings before rendering
  477.             glEnable(GL_DEPTH_TEST);
  478.             // Enabling the machine to clean the invisible elements of the scene
  479.             glutMainLoop();
  480.             // Function initializes the core of the GLUT library
  481.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement