• 12
• 9
• 9
• 13
• 10

Getting a trail of spaceships instead of just one. Any suggestions?

This topic is 4464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm working on a console spaceship shooter using just text. I started with an example program from "Windows Game Programming For Dummies" and have been adding and modifying it to make a full fledged game. However, I've come upon a problem. The "X" coordinates work just fine, but for some reason, the player's spaceship is being drawn on every "Y" coordinate in the line containing the "X" coordinate. If you need a visual to understand what I mean to this, I've posted a screenshot that can be viewed at http://geocities.com/xfiruath/space.jpg To draw the spaceship (and the background), on the screen, I use this function:
inline void Draw_String(int x, int y, char *string)
{
// Draws string at the given x,y
COORD cursor_pos; // used to pass coords
// set printing position
cursor_pos.X = x;
cursor_pos.Y = y;
SetConsoleCursorPosition(hconsole,cursor_pos);
// print the string in current color
printf("%s",string);
}  /* --> end Draw_String  */

The corresponding function call to draw the spaceship is this :
Draw_String(player_x,player_y,"<-*->");

I've tried clearing the space immediately behind the spaceship by using this function :
inline void Clean_Player(int y, char *string)
{
// Ensure there is not a string of spaceships on the y axis as ship moves
Set_Color(15,0);
COORD cursor_pos;
cursor_pos.Y = y;
y = y - 1;
SetConsoleCursorPosition(hconsole,cursor_pos);
printf("%s",string);
}

With the corresponding function call (immediately after painting the spaceship):
Clean_Player(player_y, "");

What am I doing wrong here, and how do I fix this so the spaceship stays in one X,Y coordinate, instead of 1 X coordinate and all Y coordinates?

Share on other sites
I never use the Console to make a Game.. but for the code everything seem ok

So, i dont know how the console is working, but its sure u need a way to clear the screen (or you can redraw it all, or clear the player like u did)

For your problem, are you calling Draw_String(player_x,player_y,"<-*->") in a loop of some sort ?

This is the only place i can think there can be an error, in the code i dont see ;)

Feel free to post a little more
Jonathan

Share on other sites
Yes, I'm using a while loop. Here are the pertinent contents of main :
void main(void){	char playerinput;   // Players Keyboard Input	int player_x = 40;  // Players x position	int player_y = 10;  // Players y position	Create_Graphics();	Clear_Screen();	// Game Loop - Get Input, Clean Screen, Redraw	while(game_running)	{		if (kbhit())		{			// Get User Input			playerinput = toupper(getch());			// Quit Game			if (playerinput=='Q' || playerinput==27)				game_running = 0;			// Move Left			if (playerinput=='F')				player_x--;			// Move Right			if (playerinput=='S')				player_x++;			// Move Down			if (playerinput=='C')				player_y++;			// Move UP			if (playerinput=='E')				player_y--;		}  		// Ensure ship doesn't leave screen		if (++player_x > MAX_X)			player_x = MAX_X;		if (--player_x < 0)			player_x = 0;		if (++player_y > MAX_Y)			player_y = MAX_Y;		if (--player_y < 0)			player_y = 0;		// Draw stars at random positions to scroll by		Set_Color(15,0);		Draw_String(rand()%88, SCROLL_POS,".\n");		// Draw player		Set_Color(rand()%15,0);		Draw_String(player_x,player_y,"<-*->");		Draw_String(0,0,"");		Clean_Player(player_y, "");		// Set up frame rate using the sleep function		Sleep(40);	} /* --> End While  */	// Clear Screen, display game over message	Clear_Screen();	printf("\nG A M E O V E R\n\n");} /* --> End Main    */

It's supposed to draw the stars, draw the character, clear the screen. It seems to be doing that for everything except the Y axis. I'm kind of new to C/C++, so I'm not really sure what I'm doing incorrectly.

Share on other sites
Hummm it compile and work on my side with some modification ;)

What happen if you Comment thoses line out

// Draw stars at random positions to scroll by
Set_Color(15,0);
Draw_String(rand()%88, SCROLL_POS,".\n");

And those lines too...

Draw_String(0,0,"");
Clean_Player(player_y, "");

And add a Clear_Screen(); After the if (kbhit()) clause

if(bhhit())
{
...
}

Clear_Screen();

And keep me post
Jonathan

if (++player_x > MAX_X)	player_x = MAX_X;if (--player_x < 0)	player_x = 0;if (++player_y > MAX_Y)	player_y = MAX_Y;if (--player_y < 0)	player_y = 0;