C++ game shoot projectile help needed


  • Gesperrt

    I am trying to make a C++ shoot projectile game using Windows API. I need to place the triangle at the middle of bottom part of screen(window). It must be able to turn minus 90 to 90 degrees from basic upward direction and smoothly with the help of arrow keys. The space key must allow the triangle to shoot the projectile object that has lines and must keep shooting projectile continuously if space key is pressed and holded for a long time. I need to create object clocks(atleast two) that displays showing real time.

    Below is what is done so far. How to create a C++ projectile that can shoot out lines? Any help or some c++ gaming code samples would help here.

    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    bool off;
    
    int x,y;
    
    int height = 20;
    int width  = 20;
    
    enum Directon { STOP = 0, LEFT, RIGHT, UP, DOWN}; // direction
    Directon dir;
    
    void startgame ()
    {
     off = false;
     x = width / 2;
     y = height / 2;
         
    }
    
    
    void graphics () // shows the graphics on the screen of the game
    {
     system ("cls"); // clears the screen so that the map will not stack on top of each other
         
     for (int i = 0; i<width +2; i++) // top cover
     {
         cout << "1";
     }
     cout << endl;
    
     for ( int j = 0 ; j < height; j++)
     {
         cout <<  "2";
         
              for ( int k = 0; k < width ; k++)
              {
                  if (j == y && k == x)
                  {
                     cout << "O";
                  }
                     
                  else 
                  
              cout << " "; // fills in the middle
              }  
                 cout << "3";
                 cout << endl;
     }
      
      for (int i = 0; i<width +2; i++) // bottom cover
     {
         cout << "4";
     }
    
    }
     
    void Input() // how the program knows which key to input and what direction it will go
    {
        if (_kbhit())
        {
            switch (_getch())
            {
            case 'a':
                dir = LEFT;
                break;
                
            case 'd':
                dir = RIGHT;
                break;
                
            case 'w':
                dir = UP;
                break;
                
            case 's':
                dir = DOWN;
                break;
                
            case 'x':
                off = true;
                break;
            }
        }
    }
    
     
    void system ()
    {
         switch (dir)
        {
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case UP:
            y--;
            break;
        case DOWN:
            y++;
            break;
        default:
            break;
        }      
    }
    
    
    int main ()
    {
        startgame(); // in order to enable the while loops the "start" function must be called on
    
        while (!off) //constantly refreshes the program
              {
                   graphics ();
                   void system ();
                   void input ();
              }
    }
    
    
    


  • @tampere2021 sagte in C++ game shoot projectile help needed:

    How to create a C++ projectile that can shoot out lines?

    There are no projectiles in C++.


  • Gesperrt

    @Swordfish Is there any way to place cannon(triangle) to middle of the bottom part of window screen and then while user holds space key, the cannon must shoot a projectile which is a small object consists of lines. How can I create this in c++ windows api?



  • You can set the cursorposition with this function:

    #include <windows.h>
    void GotoXY( int x, int y)
    {
       COORD coord;
       coord.X = x;
       coord.Y = y;
       SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
    }
    

    Try to start to use a single character for your projectile.

    Try at next to draw a line with *-Characters. This means: Create a function, which looks like this

    void DrawLine(int x1, int y1, int x2, int y2)
    {
     ...
    }
    

    Try to move the projectile for you first step only in horizontal- or vertical-direction.



  • @XMAMan sagte in C++ game shoot projectile help needed:

    GotoXY

    Bei GotoXY muss ich immer schmunzeln, hatte einen Schulkollegen der das immer "Gotòcksi" ausgesprochen hat 🙂



  • @tampere2021 sagte in C++ game shoot projectile help needed:

    holded

    held 😉

    Below is what is done so far. How to create a C++ projectile that can shoot out lines? Any help or some c++ gaming code samples would help here.

    // (snip)
    
    void Input() // how the program knows which key to input and what direction it will go
    {
    // (snip)
    }
    
    // (snip)
    
    int main ()
    {
        startgame(); // in order to enable the while loops the "start" function must be called on
    
        while (!off) //constantly refreshes the program
              {
                   graphics ();
                   void system ();
                   void input ();
              }
    }
    
    

    There are a couple of very basic things wrong here.

    1. Input vs. input. C++ is case sensitive, so if you're trying to call the Input function from your main loop, then you have to spell it exactly as it's spelled where it's defined.
    2. The void system (); and void input (); lines in your main loop have no real effect. They are just forward declarations. They basically tell the compiler that there is a function with that name, arguments and return type which could be called. If you want to call the functions, you should just write system(); and input();. Or rather system(); and Input(); because your input function has a capital I.

    I am trying to make a C++ shoot projectile game using Windows API.

    You're not using the Windows API here, you're just printing text characters to stdout.

    In fact, using the Windows API would be better, because you can actually do graphics with the Windows API. If you want to do that, I suggest you google for some WinAPI graphics tutorials, there are tons of them out there.


  • Mod

    @hustbaer sagte in C++ game shoot projectile help needed:

    @XMAMan sagte in C++ game shoot projectile help needed:

    GotoXY

    Bei GotoXY muss ich immer schmunzeln, hatte einen Schulkollegen der das immer "Gotòcksi" ausgesprochen hat 🙂

    Es gibt Leute, die das nicht so aussprechen? Gotoeckswai oder gar Gotoicksüpsilon sind doch viel zu viele Silben.



  • Also ich sag mal er war nicht der hellste, die Vermutung war dass er nicht gecheckt hat was es bedeutet 🙂
    (Man hat das damals ja typischerweise nicht "GotoXY" sondern "gotoxy" oder "GOTOXY" geschrieben, wo es dann nicht so offensichtlich ist.)
    Was es dann lustig macht.
    Es absichtlich "gotòcksi" auszusprechen ist natürlich auch lustig - auf ne andere Art.

    Ich hatte auch einen Schulkollegen der immer "Dschiostick" statt "Dschoistick" gesagt hat. Vermutlich beim 1. mal falsch gelesen, und dann bleibt man schnell dabei hängen es immer falsch zu lesen. (Weiss ich aus Erfahrung, ich hab als Kind/Jugendlicher lange Zeit PC-Engine falsch ausgesprochen...)

    Aber zurück zu wie man Dinge ausspricht...
    Bei mir war es so, dass ich anfangs, als ich noch nicht so viel Erfahrung hatte, Dinge möglichst so ausgesprochen habe wie sie "gemeint" waren, also "essprinteff", "putess", "eibiemm" usw.
    Heute sag ich auch eher "sprinteff", "putz" und "ihb'm".
    Vermutlich weil ich als Neuling mehr Sorgen hatte jmd. könnte meinen ich hätte nicht gecheckt was es bedeutet. Heute dagegen sprech ich es eher so aus als wäre es ein Wort, einerseits weil es kürzer ist, aber hauptsächlich weil ich es lustig finde.

    Also ja, ich sage auch "gotocksi" 😃



  • Dieser Beitrag wurde gelöscht!

  • Gesperrt

    @hustbaer Ok will try using WinAPI here.


Anmelden zum Antworten