Thread: Screensaver problem

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    4

    Screensaver problem

    Hey,i'm using visual studio 2015. The task is to get an "n" number of "X" characters on the console in random positions after that the "X" characters have to move up, down,left or right one space randomly on their own, if they get to the edge they have to come out on the opposite side and continue, it has to exit by pressing q. I'm having trouble getting them to move randomly so i would appreciate the help.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <Windows.h>
    #include <conio.h>
    #include <ctime>
    #include <stdio.h>
    
    
    
    
    using namespace std;
    
    
    void gotoxy(int x, int y)
    {
        COORD c = { x, y };
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
    }
    
    
    
    
    int main()
    {
        srand(time(0));
        int n,x,y;
        char ch = 'X';
        char cha;
        int i = 0;
    
    
        
        cin >> n;
    
    
        do {
        
    
    
                while (i<n)
            {
                    i++;
                    x = rand() % 150;
                    y = rand() % 20;
                    gotoxy(x, y);
                    cout << ch;
    
    
                    
            }
                
        
            cha = _getch();
        } while (cha != char('q') && cha!=char('Q'));
    
    
    
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You want them to move randomly.

    Which means you need to know where they are.

    So perhaps things along the line of
    Code:
    const int SCREEN_X = 150;
    const int SCREEN_Y = 50;
    const char SYMBOL = 'X';
    const char BLANK = ' ';
    class Dots {
      private:
        int x,y;
      public:
        Dots() {
            x = rand() % SCREEN_X;
            y = rand() % SCREEN_Y;
        }
        void draw() {
            gotoxy(x, y);
            cout << SYMBOL;
        }
        void undraw() {
            gotoxy(x, y);
            cout << BLANK;
        }
        void move() {
            undraw();
            x += rand() % 3 - 1;
            y += rand() % 3 - 1;
            // you fix the edge wrapping
            draw();
        }
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    4
    Thanks for the help but i just started c++ not too long ango and we haven't even gotten to classes, so i don't know why this is the homework. Could you tell me how to do it with what you wrote cause i honestly have no idea and i'm just getting errors, would really appreciate the help.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Have you covered arrays?
    Have you covered structures (struct)?
    Edit: Have you covered user defined functions?

    Does "n" have a max value that you have to support?

    Tim S.
    Last edited by stahta01; 11-27-2018 at 07:20 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Nov 2018
    Posts
    4
    Yes we covered arrays and just barely begun structures. The task just says "n" has to be an integer.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So create an array of
    Code:
    struct Dot {
      int x;
      int y;
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2018
    Posts
    4
    I managed to do it with the class you sent at least in part. How would i make all of the "X"-s move, only one of them moves cause the gotoxy function is tied to the cursorposition i think at least.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <Windows.h>
    #include <conio.h>
    #include <ctime>
    #include <stdio.h>
    
    
    
    
    
    
    
    
    using namespace std;
    
    
    
    
    void gotoxy(int x, int y)
    {
    	COORD c = { x, y };														
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
    }
    
    
    
    
    const int screen_x = 100;
    const int screen_y = 30;													
    const char symbol = 'X';
    const char blank = ' ';
    class Dots {																
    private:
    	int x, y;
    public:
    	void Random() {															
    		x = rand() % screen_x;
    		y = rand() % screen_y;
    	}
    	void draw() {															
    		gotoxy(x, y);
    		cout << symbol;
    	}
    	void undraw() {															
    		gotoxy(x, y);
    		cout << blank;
    	}
    	void move() {																
    		undraw();
    		x = x + rand() % 3 - 1;
    		y = y + rand() % 3 - 1;
    		draw();
    	}
    };
    
    
    int main()
    {
    
    
    	int n;
    	Dots Pontok;
    	char cha;
    	
    	cin >> n;
    
    
    	for (int i = 0; i < n; i++)												
    	{
    		Pontok.Random();
    		Pontok.draw();	
    	}
    
    
    do {
    	while (!_kbhit())
    	{
    		Pontok.move();
    		Sleep(500);
    	}				 																
    		cha = _getch();
    	} while (cha != char('q') && cha != char('Q'));
    	
    
    
    
    
    
    
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a screensaver, and need help
    By Finchie_88 in forum Windows Programming
    Replies: 0
    Last Post: 10-25-2004, 12:24 PM
  2. Screensaver Problem. Help!
    By SmashBro in forum Windows Programming
    Replies: 1
    Last Post: 04-01-2003, 01:32 PM
  3. redhat screensaver
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-10-2002, 08:21 PM
  4. Screensaver
    By Eber Kain in forum Windows Programming
    Replies: 4
    Last Post: 05-06-2002, 09:27 AM
  5. ScreenSaver
    By wayko in forum C++ Programming
    Replies: 0
    Last Post: 10-05-2001, 05:41 AM

Tags for this Thread