C# C控制台应用程序-gameloop/多对象

C# C控制台应用程序-gameloop/多对象,c#,console-application,game-loop,C#,Console Application,Game Loop,正如标题所说,我目前正在用c语言开发一款游戏机。问题是,每次我都无法制作一个有效的游戏循环。。。另外,我在生成多个对象时遇到了一个问题,比如在本例中子弹或敌人我对c和stakoverflow还是新手;P 我希望你能帮我让它工作!!: using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threadin

正如标题所说,我目前正在用c语言开发一款游戏机。问题是,每次我都无法制作一个有效的游戏循环。。。另外,我在生成多个对象时遇到了一个问题,比如在本例中子弹或敌人我对c和stakoverflow还是新手;P

我希望你能帮我让它工作!!:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace Try
{
class Program
{
    public static bool gameloop = true;
    public static int counter = 3;
    public static Timer travel = new Timer();
    public static string playermodel = GetConfig();

    static void Main(string[] args)
    {

        GameLoop();
        Console.Read();

    }

    private static string GetConfig()
    {
        string player = "";

        while (player.Length != 7)
        {
            Console.Write("Enter 7 chars (in 1 line) for your Playermodell:");
            player = Console.ReadLine();
        }

        return player;
    }

    private static void GameLoop()
    {

        Player player = new Player();

        travel.Interval = 1;

        travel.Elapsed += OnTimedEventTravel;



        player.SetPlayer(Program.playermodel);

        while (gameloop == true)
        {
            travel.Start();
        }


    }

    private static void OnTimedEventTravel(Object source, System.Timers.ElapsedEventArgs e)
    {
        Player player = new Player();
        Bullet bullet = new Bullet();
        Enemy enemy = new Enemy();
        PowerUP powerUP = new PowerUP();

        player.Move();
        player.Shoot();

        if (Bullet.isFlying == true)
        {
            bullet.Travle();
        }


    }
}

class Player

{
    public static int PlayerPosX = 0;
    public static int PlayerPosY = 0;
    public static int PlayerSize = 0;

    public Player()
    {
        PlayerSize = Program.playermodel.Length;
    }

    public void SetPlayer(string player)
    {
        Console.SetCursorPosition(Console.WindowWidth / 2 - PlayerSize / 2, Console.WindowHeight - 3);

        PlayerPosX = Console.WindowWidth / 2 - PlayerSize / 2;
        PlayerPosY = Console.WindowHeight - 3;

        Console.WriteLine(player);
    }

    public virtual void Move()
    {
        ConsoleKeyInfo key;

        if (Console.KeyAvailable)
        {
            key = Console.ReadKey();

            if (key.Key == ConsoleKey.LeftArrow)
            {
                if (PlayerPosX > Console.WindowLeft)
                {
                    Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX - 1, PlayerPosY);
                    PlayerPosX -= 1;                      
                }
            }

            if (key.Key == ConsoleKey.RightArrow)
            {
                if (PlayerPosX + PlayerSize < Console.WindowWidth)
                {
                    Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX + 1, PlayerPosY);
                    PlayerPosX += 1;
                }
            }

            if (key.Key == ConsoleKey.UpArrow)
            {
                if (PlayerPosY > Console.WindowTop)
                {
                    Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX, PlayerPosY - 1);
                    PlayerPosY -= 1;
                }
            }

            if (key.Key == ConsoleKey.DownArrow)
            {
                if (PlayerPosY < Console.WindowHeight)
                {
                    Console.MoveBufferArea(PlayerPosX, PlayerPosY, PlayerSize, 1, PlayerPosX, PlayerPosY + 1);
                    PlayerPosY += 1;
                }
            }
        }
    }

    public virtual void Shoot()
    {
        Bullet bullet = new Bullet();

        ConsoleKeyInfo key;

        key = Console.ReadKey();

        if (key.Key == ConsoleKey.Spacebar)
        {
            Bullet.Spawn();
        }
    }

    public virtual void Collide()
    {

    }
}

class Bullet
{

    private static int SpawnPosX;
    private static int SpawnPosY;
    public static int CurrentPosX;
    public static int CurrentPosY;
    public static bool isFlying = false;
    public static int speed = 1;

    public static void Spawn()
    {
        isFlying = true;
        SpawnPosX = Player.PlayerPosX + 3;
        SpawnPosY = Player.PlayerPosY - 1;

        Console.SetCursorPosition(SpawnPosX, SpawnPosY);

        CurrentPosX = SpawnPosX;
        CurrentPosY = SpawnPosY;

        Console.WriteLine("'");


    }

    public void Travle()
    {

        if (Bullet.CurrentPosX > Console.WindowTop)
        {
            Console.MoveBufferArea(Bullet.CurrentPosX, Bullet.CurrentPosY, 1, 1, Bullet.CurrentPosX, Bullet.CurrentPosY - 1);
            Bullet.CurrentPosY -= 1;
        }
        else
        {
            //TODO Delete bullet
            Bullet.isFlying = false;

        }


    }



    private static void Hit()
    {


    }
}

class Enemy
{
    public int EnemyPosX;
    public int EnemyPosY;

    public Enemy()
    {

    }

    public static void Spawn()
    {

    }

    public void Move()
    {

    }

    public void Shoot()
    {

    }

    public void Collide()
    {

    }
}

class PowerUP
{
    public int random;

    public string WhichUP()
    {


        Random rnd = new Random();

        string whichUP = "";

        random = rnd.Next(0, 4);

        switch (random)
        {
            case 0:
                whichUP = ":";
                break;

            case 1:
                whichUP = "O";
                break;

            case 2:
                whichUP = "^";
                break;

            case 3:
                whichUP = "~";
                break;
        }

        return whichUP;
    }
  }
}

对于gameloop,您可以创建类似Unity3D的东西,function start可以创建任何东西,您可以在其中执行循环所需的操作,然后创建更新函数!结束更新

这个

   bool end  = false;
   static void Main(string[] args)
{
  //Things before the Loop
  //Instantiate player etc
  while(!end) Update();


}

   void Update(){//Loop
  // When the game is finished End = true

 }

寻求调试帮助的问题此代码为什么不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请看:只是一点帮助,这里有一个改进的通电类:类通电{private Random rnd=new Random;public string WhichUP=>:O^ ~.Substringrnd.Next4,1;}。你能澄清一下吗?正如我说的,我是新来的,我现在跟你有问题。我到底该怎么办?