using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _2D命令行小游戏Beta1_0 { class Game { public Game(int scale) { _scale = scale; peox = 1; peoy = 1; map = new int[_scale, _scale]; for (int i = 0; i < _scale; i++) { map[0, i] = 1; map[_scale - 1, i] = 1; map[i, 0] = 1; map[i, _scale - 1] = 1; } _score = 0; foodx = peox; foody = peoy; spawnFood(); direction = 0; } public void play() { disMap(); while (true) { var k = Console.ReadKey();//System.ConsoleKeyInfo switch (k.KeyChar) { case ‘w‘: case ‘W‘: direction = 1; break; case ‘s‘: case ‘S‘: direction = 2; break; case ‘a‘: case ‘A‘: direction = 3; break; case ‘d‘: case ‘D‘: direction = 4; break; case ‘e‘: case ‘E‘: return; } switch (direction) { case 1: if (peox > 1) peox--; disMap(); break; case 2: if (peox < _scale - 2) peox++; disMap(); break; case 3: if (peoy > 1) peoy--; disMap(); break; case 4: if (peoy < _scale - 2) peoy++; disMap(); break; } } } private void disMap() { Console.Clear(); for (int i = 0; i < _scale; i++) { for (int j = 0; j < _scale; j++) { if (peox == foodx && peoy == foody) { _score += 100; spawnFood(); } if (i == peox && j == peoy) Console.Write("◎"); else if (i == foodx && j == foody) Console.Write("※"); else if (map[i, j] == 0) Console.Write(" "); else if (map[i, j] == 1) Console.Write("█"); } Console.WriteLine(); } Console.WriteLine("WSAD:move E:exit"); Console.WriteLine("Your score: "+_score.ToString()); } private void spawnFood() { Random rd = new Random(); while (foodx == peox && foody == peoy) { foodx = rd.Next(1, _scale - 1); foody = rd.Next(1, _scale - 1); } } private int peox, peoy; private int[,] map;// 0 empty 1 wall private int _scale; private long _score; private int foodx,foody; private int direction; } class Program { static void Main(string[] args) { Console.Write("please input scale(2<=n<=25):"); int sca = Convert.ToInt32(Console.ReadLine()); if(sca>=2 && sca<=25) { Game game = new Game(sca+2); game.play(); } } } }
时间: 2024-10-22 11:16:35