首页 > 解决方案 > 问:C# 线程命令导致方法名称预期错误(CS0149)

问题描述

我正在 Visual Studio 的控制台中制作游戏。我在它的中间,有一个问题。

Thread th1 = new Thread(new ThreadStart(Blocks(ana, e, obj, wall)));                                                        
th1.Start();

由于需要方法名称,我无法启动它。这就是《守则》的全部内容。

using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace Konsol
{
    
    class Program
    {
        string space1 = " ";
        //Alt + 2
        string player1 = "☻";
        //Alt + 219
        string wall1 = "█";
        //Alt + 4
        string coin1 = "♦";

        int yer = 76;

        int loop = 0;
        static void Main(string[] args)
        {
            Program space = new Program();
            Program player = new Program();
            Program wall = new Program();
            Program coin = new Program();

            Program[] obj = new Program[9] {space, space, space, space, space, space, space, space, space };

           
                         


            string[] e = { space.space1, player.player1, wall.wall1, coin.coin1 };

            ///////            0     1     2     3     4     5     6     7     8                       //////////
            string[] ana = { e[2], e[2], e[2], e[2], e[2], e[2], e[2], e[2], e[2], 

            ///////            9    1 0   1 1   1 2   1 3   1 4   1 5   1 6   1 7                    //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2],

            ///////           1 8   1 9   2 0   2 1   2 2   2 3   2 4   2 5   2 6                  //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2],

            ///////           2 7   2 8   2 9   3 0   3 1   3 2   3 3   3 4   3 5                //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2],

            ///////           3 6   3 7   3 8   3 9   4 0   4 1   4 2   4 3   4 4              //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2],

            ///////           4 5   4 6   4 7   4 8   4 9   5 0   5 1   5 2   5 3            //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2],

            ///////           5 4   5 5   5 6   5 7   5 8   5 9   6 0   6 1   6 2          //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2],

            ///////           6 3   6 4   6 5   6 6   6 7   6 8   6 9   7 0   7 1        //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2],

            ///////           7 2   7 3   7 4   7 5   7 6   7 7   7 8   7 9   8 0      //////////
                             e[2], e[0], e[0], e[0], e[1], e[0], e[0], e[0], e[2],

            ///////           8 1   8 2   8 3   8 4   8 5   8 6   8 7   8 8   8 9    //////////
                             e[2], e[0], e[0], e[0], e[0], e[0], e[0], e[0], e[2], };

            

        çıktı:

            Console.Clear();

            int y = 0;

            foreach (string s in ana)
            {
                Console.Write(s);
                y++;
                if (y % 9 == 0)
                {
                    Console.WriteLine();
                }
            }

            Console.WriteLine(player.yer);

            ConsoleKeyInfo move = new ConsoleKeyInfo();
            move = Console.ReadKey(true);

            if (move.Key == ConsoleKey.A && ana[player.yer - 1] != e[2])
            {
                if (player.yer > 73) 
                {
                    player.yer--;
                }
            }

            else if (move.Key == ConsoleKey.D && ana[player.yer + 1] != e[2])
            {
                if (player.yer < 79 )
                {
                    player.yer++;
                }
            }
            else
            {

            }

            if (ana[player.yer - 1] == e[1] && ana[player.yer - 1] != e[2])
            {
                ana[player.yer - 1] = e[0];
                
                ana[player.yer] = e[1];
                
            }
            
            else if (ana[player.yer + 1] == e[1] && ana[player.yer + 1] != e[2])
            {
                ana[player.yer + 1] = e[0];

                ana[player.yer] = e[1];

            }

            Thread th1 = new Thread(new ThreadStart(Blocks(ana, e, obj, wall)));
                                                          

            th1.Start();

            goto çıktı;       
        }

        public static void Blocks(string[] ana, string[] e, Program[] obj, Program wall, Program coin)
        {
            

            Random Generator1 = new Random();
            int gen1 = Generator1.Next(1, 11);

            Random Generator2 = new Random();
            int gen2 = Generator2.Next(1, 10);

            int x = 0;

            switch (gen2)
            {
                case 1:
                    x = 0;
                    break;
                case 2:
                    x = 1;
                    break;
                case 3:
                    x = 2;
                    break;
                case 4:
                    x = 3;
                    break;
                case 5:
                    x = 4;
                    break;
                case 6:
                    x = 5;
                    break;
                case 7:
                    x = 6;
                    break;
                case 8:
                    x = 7;
                    break;
                case 9:
                    x = 8;
                    break;

            }
            
            

            switch (gen1)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    break;

                case 7:
                case 8:
                case 9:
                case 10:
                    ana[x + 9] = e[2];
                    if (obj[x] != wall)
                    {
                        obj[x] = wall;
                        break;
                    }
                    else
                    {
                        break;
                    }
                    
            }

            obj[x].loop++;

            switch (obj[x].loop)
            {
                case 1:
                    ana[x + 9] = e[0];
                    ana[x + 18] = e[2];
                    break;
            }

            
           
        }
    }
}

我以为是因为空虚。我尝试了 int、object 等,但没有奏效。我说我在代码中间缺少一些东西。

错误在那里

Blocks(ana, e, obj, wall)

它可以在没有Thread命令的情况下工作,但是当我使用 Thread 命令时会导致错误。

我试图做一个游戏会有一张脸,用A和D移动。然后每隔几秒钟就会掉块。但我不知道该怎么做。我需要帮助。

标签: c#

解决方案


在您的应用程序中,最好使用任务而不是线程(实际上我认为使用任务总是比线程更可取)。
在最简单的形式中,您可以使用以下内容:

Task.Run(() => Blocks(ana, e, obj, wall));

推荐阅读