用C#写一个飞机小游戏
时间: 2023-08-10 07:02:48 浏览: 55
当然!以下是一个简单的 C# 飞机小游戏示例代码:
```csharp
using System;
using System.Threading;
class Program
{
static int playerX = 0;
static int playerY = 0;
static bool isGameOver = false;
static void Main(string[] args)
{
Console.CursorVisible = false;
Console.SetWindowSize(40, 20);
Thread inputThread = new Thread(InputThread);
inputThread.Start();
while (!isGameOver)
{
Update();
Draw();
Thread.Sleep(50);
}
inputThread.Join();
}
static void InputThread()
{
while (!isGameOver)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.LeftArrow)
{
playerX--;
}
else if (keyInfo.Key == ConsoleKey.RightArrow)
{
playerX++;
}
else if (keyInfo.Key == ConsoleKey.UpArrow)
{
playerY--;
}
else if (keyInfo.Key == ConsoleKey.DownArrow)
{
playerY++;
}
else if (keyInfo.Key == ConsoleKey.Escape)
{
isGameOver = true;
}
}
}
static void Update()
{
// 游戏逻辑更新
}
static void Draw()
{
Console.Clear();
Console.SetCursorPosition(playerX, playerY);
Console.Write("▲");
}
}
```
这个示例中,我们使用了一个简单的 ASCII 图形来表示飞机,通过键盘的箭头键来控制飞机的移动。游戏的主循环中,不断地更新游戏状态、绘制画面,并通过一个单独的线程来处理用户输入。
您可以根据自己的需求扩展游戏逻辑,比如添加敌机、子弹、碰撞检测等功能。希望这个示例能帮助到您,如果有任何问题,请随时提问!