Reputation: 1
I am making a game works in console of Visual Studio. I am in middle of it and there is a problem.
Thread th1 = new Thread(new ThreadStart(Blocks(ana, e, obj, wall)));
th1.Start();
I can't start it because of Method Name Expected. This is all of the Code.
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;
}
}
}
}
I thought it was because of void. I tried int, object etc. but didn't work. I said I am in the middle of the code somethings are missing.
Error is in there
Blocks(ana, e, obj, wall)
it is work without Thread commands but it will cause error when I use Thread commands.
I trying to do a game there will be a face, move with A and D. Then blocks will fall in every few seconds. But I don't know how to do that. I need help.
Upvotes: 0
Views: 2114
Reputation: 3285
In your application, it would be better to use tasks instead of threads (actually I think using a Task would always be preferable to Thread).
In the simplest form you could use something like:
Task.Run(() => Blocks(ana, e, obj, wall));
Upvotes: 1