Moving objects

Demmler77

Member
Joined
Apr 15, 2020
Messages
5
Programming Experience
3-5
Hallo,
currently I'm working on a 'small' Console Application. Its something like a Simulation with moving objects and i already have the Main code and i just need Help by additional Methods to move Them in specific ways, or to create danger for existin objects. Thanks for your help
 
Of course we can help but your description is way too vague. Please provide a FULL and CLEAR explanation of exactly what you're trying to achieve, exactly how you're trying to achieve it and exactly where you're stuck.
 
Basically its just a console app about a ant simulation. i think about 4-5 base classes. One is called Object in general with coordinates and in class Ant it gets more specific. I have got everything important and the main code. But Im trying to put in some additonal methods to get catastrophes for the objects. It works to move them home and etc. But when i try to manipulate the instantiated objects im confused how. Methods with paramters to give them the existing objects for manipulating,destroying or changing them? It possible for me to put methods for creating dangerous objects, but not how to effect them objectoriented correctly and not only objects which randomly hits the other without really destroying and just "covering". And at the End i wanted to have a random method or something like this, which choses a catastrophe method of 3 or something. When i put switch statement in, for example they get infected and it jumps to the nexxt method but doesnt end it. It gets mixed up without ending. But it should end after starting one of the catastrophe methods
 
Last edited:
Hi, and welcome to the forums.

Please post only the relevant code you have a problem with and we can get a better idea of what it is you're doing and trying to do.

Please remember to post your code in code tags, just as it's demonstrated below :

codetags-gif.884
 
C#:
using System;

namespace AmeisenSimulation
{
class MainProgram
    {
        public static void BringAntIntoDanger(objects[] Givenants)
        {
            Random rnd;
            Antdanger[] DangerObjects= new Dangerobject[15]; //dangerous objects

            for (int i = 0; i <DangerObjekte.Length; i++)
            {
                Dangerobject animal= new Antdanger(); //from antdanger class
             
                for (int k = 0; k < DangerObjects.Length; i++)
                {
                    DangerObjects[k].Aktualisieren();
                 
                    if ((objects)DangerObjects[k] == Givenants[rnd.Next(0, Givenants.Length)]) //if one object of the dangour randomly hits a ant to kill it for example it wont work but it possible ?
                    {
                        tmpAnt= GivenAnts[rnd.Next(0, Givenants.Length)];
                        Console.WriteLine("Hit,AntsFalls");
                        tmpAnt.Delete();
                    }
                }
            }
        }
     
        static void Main(string[] args)
        {
            Console.WriteLine("LOL");
            System.Console.SetCursorPosition(10, 0);
            System.Console.Write("AntSim_008_Vererbung_und_Polymorphie");

            int days= 20;
            int ants= 10;
            int queens= 1;
            object[] Dangerarray= new object[5];

            System.Console.CursorVisible = false;

            object[] myAnt= new object[ants+ queens];
         
            for (int a = 0; a < ants; a++)
                myants[a] = new Ant();

            myants[ants] = new Queen();

            for (int t = 0; t < days; t++)
            {
                for (int a = 0; a < (ants + queens); a++)
                {
                    myAnts[a].update();
                    bringAntintoDanger(meineAmeise);
                }

                //Here i wanted to start maybe a method to randomly run after normal ant simulation a catastrophe in it
                System.Threading.Thread.Sleep(200);
            }

            System.Console.SetCursorPosition(10, 24);

            System.Console.ReadKey();
        }
    }
    class GeneralObject
    {
        public int posX;
        public int posY;
        public char symbol;

        public GeneralObject()
        {
            posX = 40;
            posY = 12;
            symbol = 'A';
        }

        public void delete()
        {
            System.Console.SetCursorPosition(posX, posY);
            System.Console.Write(" ");
        }

        public void Draw()
        {
            System.Console.SetCursorPosition(posX, posY);
            System.Console.Write(symbol);
        }

        public virtual void Update()
        {
            Loeschen();
            Malen();
        }
    }

 

    class classAnt: Generalobject
    {
        protected int HPLive?;
        protected System.Random rnd;

        public ClassAnt()
            : base()
        {
            System.Threading.Thread.Sleep(20);
            rnd = new System.Random();
        }

        public override void update()
        {
            Loeschen();
            Bewegen();
            Malen();
        }


        public void Move()
        {
            Loeschen();

            posX = posX + rnd.Next(-1, 2);
            posY = posY + rnd.Next(-1, 2);

            if (posX < 0)
                posX = 0;
            if (posX > 79)
                posX = 79;

            if (posY < 0)
                posY = 0;
            if (posY > 24)
                posY = 24;

            Malen();
        }
    }

 

   
}
 
Last edited:
I don't think I've ever seen anyone do a worse job of posting code than that. Three sets of code tags with no code in them and a huge wad of code unformatted, and that after someone provided an animation showing exactly how to do it. You also seem to have completely ignored the bit about posting the relevant code and ONLY the relevant code. You need to do a better job of isolating the specific issue and provide just the information relevant to that.

Apart from anything else, there's no way that you should be naming a class Object, given that that is the ultimate base type of every .NET type, whether it be part of the Framework or your own types. You also should be using "Class" anywhere in the names of your classes. You really ought to stick to a specific naming convention too and there's no reason for that not to be what Microsoft recommends, which means names of types, methods and properties all start with an upper-case letter. Don't use Pascal casing sometimes and camel casing other times. Camel casing is for local variables, parameters and private fields.
 
For naming purposes I agree, it's not the right name for a class. Whether it be :
C#:
    public class Object
    {

    }
Or :
C#:
    public class object
    {

    }
The difference being. The IDE nor the compiler will kick up a fuss about the first one. And the second one is a naming rule violation by using the lower case o in object, and not only that, but it's also syntactically incorrect because as pointed out above, object is the base type of all objects in C#. You should nearly never need to write code like this (Example only) :
C#:
        internal static void Main(string[] args)
        {
            new Thread(() => Request_Response())
            {
                Name = "Executor"
            }.Start();
        }
        internal static void Request_Response()
        {
            Console.WriteLine(@object.@string);
        }
    public static class @object
    {
        public static string @string = "foo";
    }
As you can see, you can use these names but I don't agree with why you should or would. Console.WriteLine(@object.@string); => You can see how the code calls the class object and string by its verbatim string literal name. And this works fine, but It's not how you should be writing code, as this actually is considered a violation of one of the conventions for naming purposes. If you need to name an object by name as a type, simply call it object obj instead. There are naming convention rules in C# which you should be following as pointed out by @jmcilhinney. In regards to the rest of your code, you need to explain explicitly where in the block of code above resides your problem. That is why I asked for only the relevant code, and not your whole game.

As an aside; you might also want to breeze over this article too C# Coding Conventions - C# Programming Guide
 
Last edited:
Im really sorry for confusing you all! I will update it to the most important and the Method named object was formerly in my mother language , i'm sorry i thought it will be easier if u know that this is a obligatory object class for the ants... but i confused just more... I searched a lot of things and as a trainee i need good structured ideas to learn how to Program right and efficient with objects. I would be glad if u have some similar easy codings for moving objects and etc to learn...
 
I took a lot out and it starts with the Main class.
I wrote this code to create a Object which infects all other Objects. But is this way "OK" if the real ant object with its properties doesnt get effected and only from other methods things are just covert to make it look like?:D And i would be interested if there is a way to let maybe rain some objects and to give chars own colors without using foreground maybe if they get a hit.... And my main is problem about using a switch method to call one catastrophe after ants moving for a while. I have one function: switch (Movementmodels) {

case : 0 Randomlyrunning();break;
case : 1 ConfrontAntwithvirus();break();
and potentially more but it wont work without stopping after one of the methods is called... it doesnt get a error message but the console window moves insane up and down because not only one of the methods is running, if understand me .... I would be glad about every help
I want to add my Switch case function to switch between cases of catastrophe.
C#:
protected void ConfrontAntwithvirus()
{
    int VirusposX==56;
    int Virusposy==20;

    Generalobject infectedObjectInfrontofFoodstock = new Generalobject();
    infectedObjectInfrontofFoodstock .symbol="@";
    Console.SetCusorPosition(VirusposX,VirusposY);

    Console.WriteLine(infectedObjectInfrontofFoodstock .symbol);

    if(posX==56)
    {
        Console.ForegroundColor=ConsolCOlor.Red;
        Runhome();
    }

    if (posX > ViruspositionX)
        posX -= 1;
    if (posX < ViruspositionX)
        posX +=1;
    if (posY > ViruspositionY)
        posY -= 1;
    if (posY < ViruspositionY)
        posY +=1;

}
 
Last edited:
Back
Top Bottom