hutch90
hutch90

Reputation: 351

Remove single character from console

I am trying to make a snake game for the windows console without needing to use "system("cls")" to redraw the game board, food, and snake every time the snake moves. Instead I would like to add a head and remove the tail at certain intervals. I'm using "cout" to print a '@' for the new snake head and that's working just fine, but I don't know how to remove a single character (or just make it blank) from the console and leave the rest of the console as is. Any suggestions?

Upvotes: 1

Views: 125

Answers (1)

TLJ
TLJ

Reputation: 4945

Add following function

#include "windows.h"

void gotoxy(int x, int y) 
{ 
  HANDLE hConsoleOutput; 
  COORD dwCursorPosition;

  cout.flush(); 
  dwCursorPosition.X = x; 
  dwCursorPosition.Y = y; 
  hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); 
  SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition); 
}

And all you need to do is to use gotoxy(your_snake_tail_x,your_snake_tail_y); then print space there.

Upvotes: 2

Related Questions