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

Posted

技术标签:

【中文标题】C# 控制台应用程序 - gameloop / 多个对象【英文标题】:C# console App - gameloop / multiple object 【发布时间】:2018-06-22 07:17:29 【问题描述】:

正如标题所说,我目前正在使用 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;
    
  

【问题讨论】:

寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。见:minimal reproducible example 只是一点点帮助,这里有一个改进的PowerUp 类:class PowerUP private Random rnd = new Random(); public string WhichUP() =&gt; ":O^~".Substring(rnd.Next(4), 1); 【参考方案1】:

对于游戏循环,您可以创建 Unity3D 之类的东西,创建所有内容的函数启动(您可以在其中执行循环所需的操作),然后是更新函数“while(!end) Update();”

【讨论】:

您能否再澄清一下,因为我说我是新手,我目前在关注您时遇到问题。我到底应该怎么做?【参考方案2】:

这个

   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

 

【讨论】:

以上是关于C# 控制台应用程序 - gameloop / 多个对象的主要内容,如果未能解决你的问题,请参考以下文章

如何暂停和恢复 GameLoop?

gameloop和deltatime插值c ++

Android Gameloop:结束游戏并开始活动

在 Windows 窗体中实现 gameloop [关闭]

如何在 C# 控制台应用程序的多线程中同时实现 FileSystemWatcher 和 Timer?

使用 Console.Writeline() 或 Console.Write() 时,多线程 C# 控制台应用程序中不经常挂起