Advertisement
Guest User

error help

a guest
Jul 2nd, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. void gotoxy(int x, int y);
  14. void setColour(int ink);
  15. void setConsoleSize(int rows, int columns);
  16.  
  17. int main()
  18. {
  19.  
  20.     int BULLET_SPEED  = 10;
  21. int ENEMY_SPEED  = 50;
  22.  
  23. int cyan = 0x0B;
  24. int white = 0x0F;
  25.  int green = 0x0A;
  26.  int red = 0x0C;
  27.  
  28.  
  29.  int MAX_BULLETS = 30;
  30.  
  31.  
  32.     int bulletX = 0;
  33.     int bulletY = 0;
  34.     int timer = BULLET_SPEED;
  35.     bool Bulletalive = false;
  36.     int numBullets = MAX_BULLETS;
  37.  
  38.  
  39.     int enemyX = 3;
  40.     int enemyY = 3;
  41.     int enemyTimer = ENEMY_SPEED;
  42.     int enemyAlive = true;
  43.     char dirHorizontal = 'r';
  44.     int hits = 0;
  45.      char b = 232;
  46.      char st = 153;
  47.      char bt = 234;
  48.      char slt = 2;
  49.     char gm = 15;
  50.     char em = 31;
  51.     char el = 17;
  52.     char er = 16;
  53.     char ps  = 24;
  54.     char pm = 6;
  55.     char pmj = 194;
  56.     char q, ch1;
  57.     char a = 221, bi=219;
  58.     char bu = 15;
  59.     char ms;
  60.     const int MenuState = 0;
  61.     const int MenuPlay = 1;
  62.    const int MenuHelp = 2;
  63. const int MenuExit = 3;
  64.  
  65.     bool playing = true;
  66.     char ch;
  67.  
  68.     int shipXPos = 39, shipYPos = 23;
  69.  
  70.     setConsoleSize(30, 89);
  71.  
  72.     system("title MAD SHOT");
  73.  
  74.       Beep(261,100);
  75.   Beep(293,100);
  76.    Beep(329,100);
  77.   Beep(349,100);
  78.   // this is just for the design, ignore it
  79.  
  80. cout<<"       ***********           ***********      999999999999999         000000000000\n";
  81. Sleep(100);
  82.    cout<<"       **** *******        *******  ****      999999999999999         00         00\n";
  83.    Sleep(100);
  84.    cout<<"       ****  ********      ******   ****      999         999         00          00\n";
  85.    Sleep(100);
  86.    cout<<"       ****    *******    *******   ****      999         999         00           00\n";
  87.    Sleep(100);
  88.    cout<<"       ****     *******  *******    ****      999         999         00            00\n";
  89.    Sleep(100);
  90.    cout<<"       ****      **************     ****      999         999         00             00\n";
  91.    Sleep(100);
  92.    cout<<"       ****         ********        ****      999         999         00             00\n";
  93.    Sleep(100);
  94.    cout<<"       ****          ******         ****      999         999         00             00\n";
  95.    Sleep(100);
  96.    cout<<"       ****                         ****      999         999         00             00\n";
  97.    Sleep(100);
  98.    cout<<"       ****                         ****      999         999         00             00\n";
  99.    Sleep(100);
  100.    cout<<"       ****                         ****      999         999         00             00\n";
  101.    Sleep(100);
  102.    cout<<"       ****                         ****      999         999         00             00\n";
  103.    Sleep(100);
  104.    cout<<"       ****                         ****      999         999         00            00\n";
  105.    Sleep(100);
  106.    cout<<"       ****                         ****      999         999         00           00\n";
  107.    Sleep(100);
  108.    cout<<"       ****                         ****      999         999         00          00\n";
  109.    Sleep(100);
  110.    cout<<"       ****                         ****      999         999         00         00\n";
  111.    Sleep(100);
  112.    cout<<"       ****                         ****      999999999999999         00        00\n";
  113.    Sleep(100);
  114.    cout<<"       ****                         ****      999999999999999         00000000000\n";
  115.  
  116.    cout<<"\n\n                                         PRESS ENTER TO PLAY";
  117.  
  118.  
  119.    cin.ignore();
  120.          for(q = 0; q<89; q++){
  121.             cout<<a;
  122.             Sleep(60);
  123.          }
  124.          system("CLS");
  125.  
  126.          // error starts from here
  127.  
  128.          MenuState ms=MenuPlay;
  129. char ch7=0;
  130. while (ch!='\r')
  131. {
  132.     switch (ms)
  133.     {
  134.         case MenuPlay:
  135.             cout<<"--> PLAY\n    HELP\n    EXIT\n";
  136.             break;
  137.         case MenuHelp:
  138.             cout<<"    PLAY\n--> HELP\n    EXIT\n";
  139.             break;
  140.         case MenuExit:
  141.             cout<<"    PLAY\n    HELP\n--> EXIT\n";
  142.             break;
  143.         default:
  144.             break;
  145.             cout<<"ERROR: Unknown state!\n";
  146.     }
  147.     ch=getch();
  148.     switch (ch)
  149.     {
  150.         case 'w':
  151.         case 'W':
  152.         ms=MenuPlay;
  153.         break;
  154.         case 'e':
  155.         case 'E':
  156.         ms=MenuHelp;
  157.         break;
  158.         case 'r':
  159.         case 'R':
  160.         ms=MenuExit;
  161.         break;
  162.         default:
  163.             break;
  164.     }
  165. }
  166. switch (ms)
  167. {
  168.     case MenuPlay:
  169.  
  170.             do
  171.     {
  172.  
  173.  
  174.         setColour(white);
  175.         gotoxy(24, 0);
  176.         cout << "Bullets Left: " << numBullets << "   Ship Hits: " << hits;
  177.  
  178.  
  179.  
  180.         setColour(green);
  181.         gotoxy(0,1);
  182.         cout<<"_________________________________________________________________________________________";
  183.  
  184.         setColour(green);
  185.         gotoxy(0,25);
  186.         cout<<"_________________________________________________________________________________________";
  187.  
  188.  
  189.  
  190.  
  191.  
  192.         if (_kbhit())
  193.         {
  194.  
  195.             ch = _getch();
  196.  
  197.             switch (ch)
  198.             {
  199.             case 'w':
  200.                 shipXPos--;
  201.                 if (shipXPos < 0) shipXPos = 0;
  202.                 break;
  203.             case 'e':
  204.                 shipXPos++;
  205.                 if (shipXPos > 81) shipXPos = 81;
  206.                 break;
  207.             case ' ':
  208.                 if (!Bulletalive)
  209.                 {
  210.  
  211.                     bulletX = shipXPos + 1;
  212.                     bulletY = shipYPos;
  213.                     Bulletalive = true;
  214.                     numBullets--;
  215.                 }
  216.                 break;
  217.             }
  218.  
  219.             system("cls");
  220.         }
  221.  
  222.  
  223.         if (enemyAlive)
  224.         {
  225.             enemyTimer--;
  226.             if (enemyTimer == 0)
  227.             {
  228.  
  229.                 gotoxy(enemyX, enemyY);
  230.                 cout << "   ";
  231.  
  232.                 switch (dirHorizontal)
  233.                 {
  234.                 case 'l':
  235.                     enemyX--;
  236.                     if (enemyX == 3)
  237.                         dirHorizontal = 'r';
  238.                     break;
  239.                 case 'r':
  240.                     enemyX++;
  241.                     if (enemyX == 81)
  242.                         dirHorizontal = 'l';
  243.                     break;
  244.                 }
  245.  
  246.                 setColour(cyan);
  247.                 gotoxy(enemyX, enemyY);
  248.                 cout << el<<em<<er;
  249.                 enemyTimer = ENEMY_SPEED;
  250.             }
  251.         }
  252.  
  253.  
  254.  
  255.         if (Bulletalive)
  256.         {
  257.             timer--;
  258.  
  259.             if (timer == 0) {
  260.                 gotoxy(bulletX, bulletY);
  261.                 cout << " ";
  262.                 bulletY--;
  263.                 if (bulletY < 0)
  264.                     Bulletalive = false;
  265.                 else
  266.                 {
  267.  
  268.                     setColour(red);
  269.                     gotoxy(bulletX, bulletY);
  270.                     cout <<bu;
  271.                 }
  272.                 timer = BULLET_SPEED;
  273.             }
  274.         }
  275.  
  276.  
  277.         setColour(red);
  278.         gotoxy(shipXPos, shipYPos);
  279.         cout <<" "<<pm;
  280.         gotoxy(shipXPos, shipYPos + 1);
  281.         cout << ps<<pmj<<ps;
  282.  
  283.  
  284.         if (bulletX == enemyX && bulletY == enemyY && Bulletalive ||
  285.             bulletX == enemyX + 1 && bulletY == enemyY && Bulletalive ||
  286.             bulletX == enemyX + 2 && bulletY == enemyY && Bulletalive)
  287.         {
  288.             hits++;
  289.             Bulletalive = false;
  290.         }
  291.  
  292.  
  293.         if (numBullets == 0)
  294.             playing = false;
  295.  
  296.     } while (playing);
  297.  
  298.     if(hits <= 5){
  299.         setColour(red);
  300.     gotoxy(25, 10);
  301.     cout << "        Game Over"<<endl;
  302.     cout<<"                                 SHIPS HIT:"<< hits<<endl;
  303.     cout<<"                            NO MEDAL";
  304.     _getch();
  305.     _getch();
  306.  
  307.     }
  308.     else if(hits < 5){
  309.         setColour(green);
  310.     gotoxy(25, 10);
  311.     cout << "        Game Over"<<endl;
  312.     cout<<"                                 SHIPS HIT:"<< hits<<endl;
  313.     cout<<"                               WOODEN MEDAL "<<b;
  314.     _getch();
  315.     _getch();
  316.  
  317.     }
  318.  
  319.     else if(hits <10){
  320.         setColour(green);
  321.     gotoxy(25, 10);
  322.     cout << "        Game Over"<<endl;
  323.     cout<<"                                 SHIPS HIT:"<< hits<<endl;
  324.     cout<<"                               STONE MEDAL "<<st;
  325.     _getch();
  326.     _getch();
  327.  
  328.     }
  329.     else if(hits < 15){
  330.         setColour(green);
  331.     gotoxy(25, 10);
  332.     cout << "        Game Over"<<endl;
  333.     cout<<"                                 SHIPS HIT:"<< hits<<endl;
  334.     cout<<"                               BRONZE MEDAL "<<bt;
  335.     _getch();
  336.     _getch();
  337.  
  338.     }
  339.     else if(hits >= 20){
  340.         setColour(green);
  341.     gotoxy(25, 10);
  342.     cout << "        Game Over"<<endl;
  343.     cout<<"                                 SHIPS HIT:"<< hits<<endl;
  344.     cout<<"                               SILVER MEDAL "<<slt;
  345.     _getch();
  346.     _getch();
  347.  
  348.     }
  349.     else if(hits >= 25){
  350.         setColour(green);
  351.     gotoxy(25, 10);
  352.     cout << "        Game Over"<<endl;
  353.     cout<<"                                 SHIPS HIT:"<< hits<<endl;
  354.     cout<<"                               GOLD MEDAL "<<gm;
  355.     _getch();
  356.     _getch();
  357.  
  358.     }
  359.  
  360.         break;
  361.     case MenuHelp:
  362.  
  363.         break;
  364.     case MenuExit:
  365.  
  366.         break;
  367. }
  368.  
  369.  
  370.  
  371.     return 0;
  372.  
  373. }
  374.  
  375.  
  376. void gotoxy(int x, int y)
  377. {
  378.     COORD coord;
  379.     coord.X = x;
  380.     coord.Y = y;
  381.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  382. }
  383.  
  384.  
  385. void setColour(int ink)
  386. {
  387.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ink);
  388. }
  389.  
  390.  
  391. void setConsoleSize(int rows, int columns)
  392. {
  393.     _SMALL_RECT rect;
  394.     COORD coord;
  395.     coord.X = columns;
  396.     coord.Y = rows;
  397.     rect.Top = 0;
  398.     rect.Left = 0;
  399.     rect.Bottom = rows - 1;
  400.     rect.Right = columns - 1;
  401.     SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  402.     SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &rect);
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement