首页 > 代码库 > 2D命令行小游戏Beta1.0_代码
2D命令行小游戏Beta1.0_代码
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(); } } } }
2D命令行小游戏Beta1.0_代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。