Click here to Skip to main content
15,868,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My homework is to move a new ufo with key arrows. We already done the main program in class. I have no clue how to do it. I would be obliged if someone could help!
(I am a beginner)

I am Hungarian so some help might come handy:

mozgat - move the ufo
torol - delete
szin - color
rajzol - show the ufo on the screen
kep - the ufo's look

What I have tried:

    class Program
    {


       
        

        static void Main(string[] args)
        {
            Random r = new Random();

   /*ufo1*/         Ufotip ufo1 = new Ufotip(40, 12, "\\-O-/", ConsoleColor.Red);
   /*ufo2*/         Ufotip ufoLe = new Ufotip(20, 20, "|-O-|", ConsoleColor.Green);
   /*ufo3*/         Ufotip ufoE = new Ufotip(30, 20, ">-----------/", 
            ConsoleColor.Yellow);           
   /*ufo4*/         Ufotip ufoFV = new Ufotip(10, 10, "<-|O|->", ConsoleColor.Blue);
   /*ufo5*/         Ufotip ufoV = new Ufotip(60, 10, "_/\\_", ConsoleColor.White);
   /*ufo6*/         Ufotip ufoEn = new Ufotip(10, 15, "/--O--\\" /*look of it*/, ConsoleColor.Magenta);
   /*ufo7*/         Ufotip ufoseven = new Ufotip(5, 5, "U-U", ConsoleColor.Cyan);
                                            //ufo-starting-position
            while (true)
            {
//show ufo on the screen
                ufo1.rajzol();
                ufoLe.rajzol();
                ufoE.rajzol();                
                ufoFV.rajzol();
                ufoV.rajzol();
                ufoEn.rajzol();
//wait
                Thread.Sleep(100);
//delete the ufo
                ufo1.torol();
                ufoLe.torol();
                ufoFV.torol();
                ufoV.torol();
                ufoEn.torol();
//move the ufo
                ufo1.mozgat(1, 0);
                ufoLe.mozgat(0, 1);
                ufoFV.mozgat(0, r.Next(-1, 1 + 1));
                ufoV.mozgat(r.Next(-1, 2), r.Next(-1, 2));



            }

        }


    }
    class Ufotip
    {
        int x, y;
        string kep;
        ConsoleColor szin;

        public Ufotip(int x, int y, string kep, ConsoleColor szin)
        {
            this.x = x;
            this.y = y;
            this.kep = kep; //ufo look
            this.szin = szin; //the color of it
        }
        public void rajzol() //show the ufo
        {                  /* just borders the ufos moving area */
            if (x < 0) { x = 79; } 
            if (x > 79) { x = 0; }
            if (y < 0) { y = 23; }
            if (y > 23) { y = 0; }
            Console.SetCursorPosition(x, y);
            Console.ForegroundColor = szin;
            Console.WriteLine(kep);
            Console.ResetColor();
        }

        public void torol() //clear the ufo
        {
            Console.SetCursorPosition(x, y);
            for (int i = 0; i < kep.Length; i++)
            {
                Console.Write(" ");
            }
            Console.WriteLine();
        }

        public void mozgat(int dx, int dy) //move the ufo
        {
            x += dx;

            y += dy;
        }
    }
Posted
Updated 4-Jan-22 11:48am
v4
Comments
0x01AA 4-Jan-22 14:02pm    
Thumbs up!
Thumbs up, that you give a translation in English of your methods and also that you formated your posted code correct.

Now please explain also the problem:
a.) What are you doing
b.) What result you get vs. what result you expect
Teveli Kristóf 4-Jan-22 14:11pm    
this is the material from the lesson. (basic program that moves the ufos) this does not contain any tries sadly:( i dont even know how to start
0x01AA 4-Jan-22 14:15pm    
But you already coded something. So the question remains: What is wrong with your code? What you get and what you expect?
Teveli Kristóf 4-Jan-22 14:24pm    
so this code is correct. i have to add ufo 7 which i can add it under ufo 6 its okay til. then i have to make it move with arrows, unlike how other ufos move. i gave them a position where to go and they only move on that line. ufo7 has to move with arrow keys
0x01AA 4-Jan-22 14:30pm    
Uuups, sorry ufo 7 allready exists Step1: Create ufo7
Do you have an idea how to create ufo7? I mean you have an example how ufo1 to ufo6 have been created so far. So now how do you think ufo7 can be created?
--- And no, I don't write how to do it. That will not help you to learn ;)

1 solution

Quote:


while (true)
{
   var ch = Console.ReadKey(false).Key;
   switch(ch)
   {
       case ConsoleKey.Escape:
          ShutdownRobot();
          return;
       case ConsoleKey.UpArrow:
          MoveRobotUp();
          break;
       case ConsoleKey.DownArrow:
          MoveRobotDown();
          break;
   }
}

C# arrow key input for a console app - Stack Overflow[^]

Console.ReadKey Method (System) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900