基于C#制作一个贪吃蛇小游戏
发布人:shili8
发布时间:2023-11-23 22:23
阅读次数:73
贪吃蛇小游戏是一个经典的游戏,玩家通过控制蛇的移动来吃食物,随着吃食物的数量增加,蛇的身体也会变长,直到撞到墙壁或者自己的身体为止。在这篇文章中,我们将使用C#语言来制作一个简单的贪吃蛇小游戏。
首先,我们需要创建一个新的C#控制台应用程序项目。然后,我们可以开始编写游戏的代码。
csharp
using System;
using System.Collections.Generic;
using System.Threading;
class Program
{
static void Main()
{
Console.WindowHeight = 16;
Console.WindowWidth = 32;
int screenwidth = Console.WindowWidth;
int screenheight = Console.WindowHeight;
Random randomnummer = new Random();
int score = 5;
int gameover = 0;
pixel hoofd = new pixel();
hoofd.xpos = screenwidth / 2;
hoofd.ypos = screenheight / 2;
hoofd.schermkleur = ConsoleColor.Red;
string movement = RIGHT;
List<int> xposlijf = new List<int>();
List<int> yposlijf = new List<int>();
int berryx = randomnummer.Next(0 screenwidth);
int berryy = randomnummer.Next(0 screenheight);
DateTime tijd = DateTime.Now;
DateTime tijd2 = DateTime.Now;
string buttonpressed = no;
while (true)
{
Console.Clear();
if (hoofd.xpos == screenwidth - 1 || hoofd.xpos == 0 || hoofd.ypos == screenheight - 1 || hoofd.ypos == 0)
{
gameover = 1;
}
for (int i = 0; i < screenwidth; i++)
{
Console.SetCursorPosition(i 0);
Console.Write(■);
}
for (int i = 0; i < screenwidth; i++)
{
Console.SetCursorPosition(i screenheight - 1);
Console.Write(■);
}
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(0 i);
Console.Write(■);
}
for (int i = 0; i < screenheight; i++)
{
Console.SetCursorPosition(screenwidth - 1 i);
Console.Write(■);
}
Console.ForegroundColor = ConsoleColor.Green;
if (berryx == hoofd.xpos && berryy == hoofd.ypos)
{
score++;
berryx = randomnummer.Next(1 screenwidth - 2);
berryy = randomnummer.Next(1 screenheight - 2);
}
for (int i = 0; i < xposlijf.Count; i++)
{
Console.SetCursorPosition(xposlijf[i] yposlijf[i]);
Console.Write(■);
if (xposlijf[i] == hoofd.xpos && yposlijf[i] == hoofd.ypos)
{
gameover = 1;
}
}
if (gameover == 1)
{
break;
}
Console.SetCursorPosition(hoofd.xpos hoofd.ypos);
Console.ForegroundColor = hoofd.schermkleur;
Console.Write(■);
Console.SetCursorPosition(berryx berryy);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write(■);
tijd = DateTime.Now;
buttonpressed = no;
while (true)
{
tijd2 = DateTime.Now;
if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
if (Console.KeyAvailable)
{
ConsoleKeyInfo toets = Console.ReadKey(true);
//Console.WriteLine(toets.Key.ToString());
//Console.ReadKey(true);
switch (toets.Key.ToString())
{
case UpArrow:
buttonpressed = up;
break;
case DownArrow:
buttonpressed = down;
break;
case LeftArrow:
buttonpressed = left;
break;
case RightArrow:
buttonpressed = right;
break;
}
}
}
if (buttonpressed != no)
{
movement = buttonpressed;
}
xposlijf.Add(hoofd.xpos);
yposlijf.Add(hoofd.ypos);
switch (movement)
{
case up:
hoofd.ypos--;
break;
case down:
hoofd.ypos++;
break;
case left:
hoofd.xpos--;
break;
case right:
hoofd.xpos++;
break;
}
if (xposlijf.Count > score)
{
xposlijf.RemoveAt(0);
yposlijf.RemoveAt(0);
}
}
Console.SetCursorPosition(screenwidth / 5 screenheight / 2);
Console.WriteLine(Game over Score: + score);
Console.SetCursorPosition(screenwidth / 5 screenheight / 2 + 1);
}
}
class pixel
{
public int xpos { get; set; }
public int ypos { get; set; }
public ConsoleColor schermkleur { get; set; }
}
在上面的代码中,我们首先定义了一个`pixel`类,用来表示游戏中的一个像素点,包括其x和y坐标以及颜色。然后在`Main`方法中,我们初始化了游戏的一些参数,比如窗口的大小、蛇的初始位置和颜色、食物的位置等。接着进入了游戏的主循环,不断地更新蛇的位置、检测是否吃到了食物、检测游戏是否结束等。
通过这段代码,我们可以看到C#语言的一些基本语法和控制台应用程序的开发方法。当然,这只是一个简单的贪吃蛇小游戏的实现,还有很多地方可以进行优化和扩展,比如增加难度、增加关卡、增加特殊道具等。希望这篇文章能够帮助你更好地理解C#语言和控制台应用程序的开发。

