Jump to content
  • Advertisement
Sign in to follow this  
xFiruath

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

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;



Try not altering the actual coordinates with the prefix operations. Instead just do this style to check it: ( player_y - 1 < 0 ). See if that fixes the problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!