Hello, so i am making a shooter game, and i want the asteroids or '@' to spawn randomly along the y axis and fall down (- y axis per second)

compiler:- codeblocks
os:- win 7

thanks.

Do you really mean along the x axis, and changing their y position some amount per second?

We don't solve problems here, we help you with your solution. Write some code, ask some questions.

oh sorry, i meant spawn randomly in the x axis at the top, and fall along the y axis. I wanted to edit it but it did not show the option to do so

+vmanes i have a code for the player, the code is how to control our player but i am confused on how to do for the asteroids.

here is the code for controlling our player:-

#include <conio.h>

#include <windows.h>

#include <stdio.h>

/* Declare Goto Function */

void gotoxy(int, int);

/* Init X and Y plus the Variables which hold

 previous X and Y. */

int x, y, lastx, lasty;

/* Make a CHAR variable to get Key Input */

char a;

/* Main Function */

int main()
{
 /* Give a value to all integer variables we declared */

 x = 20; y = 30;

 lastx = x; lasty = y;

 /* start an Infinite Loop */

 while(1 == 1)

 {

 /* Head to LastX and LastY and clear old O*/

 gotoxy(lastx, lasty);

 printf(" ");

 /* Draw New O */

 gotoxy(x,y);

 printf("O");

 /* Get Input */

 a = getch();

 /* Remember Old X and Y */

 lastx = x; lasty = y;

/* If Right Arrow is Pressed, X is increased */

 if (a == 77) x = x + 1;

/* If Left Arrow is Pressed, X is decreased */

 if (a == 75) x = x - 1;

 }

}

/* Make GOTOXY function */

void gotoxy(int eex, int eey)

{
  COORD coord;
  coord.X = eex;
  coord.Y = eey;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

now what i need is i want the asteroid to generate at y axis = 1 and x axis = random, and every second the y axis = -1, and 5 asteroids should be spawned every second.
I googled but found nothing related.
I am tring but unable to

//is it correct ?


#include <conio.h>
#include <windows.h>
#include <stdio.h>

void gotoxy(int, int);

int x, y, astx, asty;


int main()
{

 x = rand() % max_x_coord ; y = 1;

 astx = x; asty = y;


 while(1 == 1)

 {


 gotoxy(astx, asty);

 printf(" ");

 gotoxy(x,y);

 printf("@");
    }
}

// for declaring goto

void gotoxy(int eex, int eey)

{
  COORD coord;
  coord.X = eex;
  coord.Y = eey;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

i tried running my code but it keeps spawning at one point, it does not spawn at random areas

No it's not correct.

(1) The "conio.h" header is old and often not even supported at all, and it is certainly not a standard header. Also, you are not using any functions from that header, so you should just remove that include.

(2) The "stdio.h" is not a standard C++ header, it's a C header. To include the C++ equivalent of that header, you need to do #include <cstdio>.

(3) If you are going to use standard functions (like printf() or rand()) with pre-pending them with std::, then you need to add using namespace std; at the beginning (after the include statements).

(4) You need to care more about indentation than that. Please indent your code correctly, for everyone's sake.

(5) The rand() function is from the cstdlib header, so you need to add #include <cstdlib>.

(6) The rand() function needs to be seeded to produce actual random numbers, otherwise, it will always produce the same sequence of numbers every time you run the program. You can seed it with the srand function, as in the reference example.

(7) The variable max_x_coord is undeclared. This should be either some number or constant that is the maximum value that you want x to have, because rand() % max_x_coord will generate a random number between 0 and max_x_coord, whatever you put there.

Your generation of the random x value is outside the loop, so only one x is ever created, you keep reusing the same value.

Your loop statement at line 21 - do you mean to create an infinite loop? 1 will always equal 1. I think a clearer way to express that would be while( true)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.