6 Replies - 418 Views - Last Post: 05 November 2017 - 05:44 AM Rate Topic: -----

#1 Adriel16  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 21-May 13

Move a worm from left to right in C++

Posted 05 November 2017 - 12:08 AM

First of all, I think I'm in the correct section of the site to post this question. I have an assignment where I have to make a worm move from left to right of the console screen. I have no idea of how to do that and the code I have right now, below, isn't getting me anywhere fast. Please point me in the right direction? Is there some reading for research?
NOTE: I have <windows.h> because I read that I would have to use coordinates, hence the existing code.
NOTE:I'm using VS 2017 and it automatically adds "stdafx.h"

Below is the code that I have so far.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <Windows.h>
#include <conio.h>

using namespace std;
class Worm {
private:
	int x;
	int y;
	int z;
public:
	Worm();
	void GotoCoord();
	void GetCursor();
};

Worm::Worm() {
	x = 0;
	y = 0;
	z = 0;
}
void Worm::GotoCoord() {
	//Initialize the coordinates
	COORD coord = { x,y };
	//Set the position
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
	return;
}
void Worm::GetCursor() {
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
		x = csbi.dwCursorPosition.X;
		y = csbi.dwCursorPosition.Y;
		cout << "Find the cursor test 1";
		
		cout << endl;
		cout << "cursor was at: (" << x << ", " << y << ")" << endl;
		//x++; 
		//y++;
		//cout << x << y;
	}
}
int main() {
	Worm slither;
	slither.GotoCoord();
	slither.GetCursor();


}


This post has been edited by Adriel16: 05 November 2017 - 12:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Move a worm from left to right in C++

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Move a worm from left to right in C++

Posted 05 November 2017 - 01:00 AM

Hmm,

You need a function to set the console cursor not to get the console cursor...


void setCursorTo(int x, int y)
{
    HANDLE handle;
    COORD position;
    handle = GetStdHandle(STD_OUTPUT_HANDLE);
    position.X = x;
    position.Y = y;
    SetConsoleCursorPosition(handle, position);
}



play around with this function it should be obvious what to do with it

you can use a cout statement to draw the worm as long as there is no std::endl ie

setCursorTo(8,15);
cout << "###@";



to move the worm easiest way is to clear the screen and redraw everything every time you move the worm there are more complicated ways which don't result in screen flashing but for now just clear the screen and redraw position of the worm.
Was This Post Helpful? 1
  • +
  • -

#3 Adriel16  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 21-May 13

Re: Move a worm from left to right in C++

Posted 05 November 2017 - 02:43 AM

Thank you for you help! This is the first time that I am using the Windows.h library and the first time I see "HANDLE handle", that's from the <windows.h> library, correct?

This post has been edited by ndc85430: 05 November 2017 - 02:50 AM
Reason for edit:: Removed quote of previous post. Just press "Reply".

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Move a worm from left to right in C++

Posted 05 November 2017 - 04:06 AM

Yes

Do you know how to clear the screen ?
Was This Post Helpful? 0
  • +
  • -

#5 Adriel16  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 21-May 13

Re: Move a worm from left to right in C++

Posted 05 November 2017 - 04:51 AM

View Postsnoopy11, on 05 November 2017 - 04:06 AM, said:

Yes

Do you know how to clear the screen ?

That I do know.
system("CLS");


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5333
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Move a worm from left to right in C++

Posted 05 November 2017 - 05:19 AM

I think he meant, do you know how to clear the screen using the Windows API?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Move a worm from left to right in C++

Posted 05 November 2017 - 05:44 AM

Yes,

This article outlines why not to use any system() calls in your code

http://www.cplusplus...articles/11153/

It covers most of the reasons but the main one from your point of view is that if you use system in your programs
there is a good chance that anti-virus programs will say to the user of your program that your program is malicious
this will make the user not want to use any of your programs, period.

Even if all it does is move a worm around the screen.

Example 2 of this article shows you how to do it safely inside windows.

https://docs.microso...ring-the-screen

and finally how to hide the cursor so it doesnt flash..

HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(consoleHandle, &info);


This post has been edited by snoopy11: 05 November 2017 - 05:47 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1