贪吃蛇
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ; namespace SnakeGame
{ public class Game { private const int Width = 40 ; private const int Height = 20 ; private const char SnakeChar = '■' ; private const char FoodChar = '★' ; private const char WallChar = '█' ; private List< Position> snake; private Position food; private Direction direction; private bool gameOver; private int score; private Random random; public Game ( ) { snake = new List< Position> ( ) ; random = new Random ( ) ; InitializeGame ( ) ; } private void InitializeGame ( ) { snake. Clear ( ) ; snake. Add ( new Position ( Width / 2 , Height / 2 ) ) ; snake. Add ( new Position ( Width / 2 - 1 , Height / 2 ) ) ; snake. Add ( new Position ( Width / 2 - 2 , Height / 2 ) ) ; direction = Direction. Right; gameOver = false ; score = 0 ; GenerateFood ( ) ; } public void Start ( ) { Console. CursorVisible = false ; Console. Clear ( ) ; while ( ! gameOver) { if ( Console. KeyAvailable) { HandleInput ( ) ; } MoveSnake ( ) ; CheckCollision ( ) ; Draw ( ) ; Thread. Sleep ( 150 ) ; } GameOver ( ) ; } private void HandleInput ( ) { var key = Console. ReadKey ( true ) . Key; switch ( key) { case ConsoleKey. UpArrow: if ( direction != Direction. Down) direction = Direction. Up; break ; case ConsoleKey. DownArrow: if ( direction != Direction. Up) direction = Direction. Down; break ; case ConsoleKey. LeftArrow: if ( direction != Direction. Right) direction = Direction. Left; break ; case ConsoleKey. RightArrow: if ( direction != Direction. Left) direction = Direction. Right; break ; case ConsoleKey. Escape: gameOver = true ; break ; } } private void MoveSnake ( ) { Position head = snake. First ( ) ; Position newHead = new Position ( head. X, head. Y) ; switch ( direction) { case Direction. Up: newHead. Y-- ; break ; case Direction. Down: newHead. Y++ ; break ; case Direction. Left: newHead. X-- ; break ; case Direction. Right: newHead. X++ ; break ; } snake. Insert ( 0 , newHead) ; if ( newHead. X == food. X && newHead. Y == food. Y) { score += 10 ; GenerateFood ( ) ; } else { snake. RemoveAt ( snake. Count - 1 ) ; } } private void CheckCollision ( ) { Position head = snake. First ( ) ; if ( head. X < 0 || head. X >= Width || head. Y < 0 || head. Y >= Height) { gameOver = true ; return ; } for ( int i = 1 ; i < snake. Count; i++ ) { if ( head. X == snake[ i] . X && head. Y == snake[ i] . Y) { gameOver = true ; return ; } } } private void GenerateFood ( ) { do { food = new Position ( random. Next ( 0 , Width) , random. Next ( 0 , Height) ) ; } while ( snake. Any ( p => p. X == food. X && p. Y == food. Y) ) ; } private void Draw ( ) { Console. SetCursorPosition ( 0 , 0 ) ; Console. WriteLine ( new string ( WallChar, Width + 2 ) ) ; for ( int y = 0 ; y < Height; y++ ) { Console. Write ( WallChar) ; for ( int x = 0 ; x < Width; x++ ) { if ( snake. Any ( p => p. X == x && p. Y == y) ) { Console. Write ( SnakeChar) ; } else if ( food. X == x && food. Y == y) { Console. Write ( FoodChar) ; } else { Console. Write ( ' ' ) ; } } Console. WriteLine ( WallChar) ; } Console. WriteLine ( new string ( WallChar, Width + 2 ) ) ; Console. WriteLine ( $"分数: { score } " ) ; Console. WriteLine ( $"蛇的长度: { snake. Count } " ) ; Console. WriteLine ( "按ESC键退出游戏" ) ; } private void GameOver ( ) { Console. Clear ( ) ; Console. WriteLine ( "游戏结束!" ) ; Console. WriteLine ( $"最终分数: { score } " ) ; Console. WriteLine ( $"蛇的最终长度: { snake. Count } " ) ; Console. WriteLine ( "按任意键退出..." ) ; Console. ReadKey ( ) ; } }
}
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ; namespace SnakeGame
{ public class Position { public int X { get ; set ; } public int Y { get ; set ; } public Position ( int x, int y) { X = x; Y = y; } } public enum Direction { Up, Down, Left, Right} }
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Threading ; namespace SnakeGame
{ class Program { static void Main ( string [ ] args) { Console. WriteLine ( "欢迎来到贪吃蛇游戏!" ) ; Console. WriteLine ( "使用方向键控制蛇的移动" ) ; Console. WriteLine ( "按任意键开始游戏..." ) ; Console. ReadKey ( ) ; Game game = new Game ( ) ; game. Start ( ) ; } }
}
< Project Sdk= "Microsoft.NET.Sdk" > < PropertyGroup> < OutputType> Exe< / OutputType> < TargetFramework> net8. 0 < / TargetFramework> < ImplicitUsings> enable< / ImplicitUsings> < Nullable> enable< / Nullable> < Platforms> AnyCPU; x86< / Platforms> < / PropertyGroup> < / Project>
# 贪吃蛇游戏 (Snake Game) 一个用C#编写的控制台版贪吃蛇游戏。## 功能特点 - 完整的贪吃蛇游戏逻辑
- 使用方向键控制蛇的移动
- 实时分数显示
- 碰撞检测(墙壁和自身)
- 食物随机生成
- 游戏结束统计## 游戏控制 - * * 方向键* * : 控制蛇的移动方向
- * * ESC键* * : 退出游戏## 如何运行 ### 方法1: 使用 .NET CLI 1 . 确保您已安装 . NET 6.0 或更高版本
2 . 在项目目录中打开命令行
3 . 运行以下命令:```bash
# 编译项目
dotnet build# 运行游戏
dotnet run
### 方法2: 使用 Visual Studio 1 . 打开 `SnakeGame. csproj` 文件
2 . 按 F5 运行游戏## 游戏规则 1 . 使用方向键控制蛇的移动
2 . 吃到食物(★)可以增加分数和蛇的长度
3 . 避免撞到墙壁或蛇的身体
4 . 游戏会一直进行直到撞墙或撞到自己## 系统要求 - . NET 6.0 或更高版本
- Windows/ Linux/ macOS 操作系统
- 支持控制台应用程序的终端## 游戏截图 游戏界面包含:
- 边界墙(█)
- 蛇身(■)
- 食物(★)
- 实时分数显示
- 蛇的长度显示享受游戏吧!