【 星 辰 · 第 二 条 约 定 】
1、关系图
![img](file:///C:UserschenxinAppDataRoamingTencentUsers121379820QQWinTempRichOleEGZFZOJ$YNAH_H1@C8{KIKT.png)
2、游戏设计
输入s建正式开始游戏,一开始只有一个敌人,当你按下w或s时,它会在随机位置生成敌人,你的任务是吃掉敌人。当地图上一个敌人都没有的时候,你就赢了。
3、问题
一开始想实现敌人随机移动,还有随机生成的五个食物,碰到敌人则游戏就输了。但是敌人随机移动的代码查了很久也想了很久没搞出来。
而后想实现倒计时的功能,即游戏开始瞬间开始计时,当你赢得时候输出用时,这个有去尝试但是貌似没有成功。
现在的代码还存在的问题是无法检测地图中是否还存在有敌人,也就是说无法判断输赢。
using one_part;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace one_part
{
class Map
{
public const int mapsize = 30;//地图尺寸
public string[,] map = new string[mapsize, mapsize];
private int row;
private int col;
public void Chushi()//初始化地图
{
for (row = 0; row < 29; row++)
{
for (col = 0; col < 30; col++)
{
if (row == 0 || row == 28 || col == 0 || col == 29)
map[row, col] = "█";
else map[row, col] = " ";
}
}
}
public void Draw()//绘画地图
{
for (row = 0; row < mapsize; row++)
{
for (col = 0; col < mapsize; col++)
{
if (col != mapsize - 1)
{
Console.Write(map[row, col]);
}
else
{
Console.WriteLine(map[row, col]);
}
}
}
}
}
class Position//设置人物位置
{
public int row;
public int col;
public void set(int Row, int Col)
{
row = Row;
col = Col;
}
}
class Move
{
public Position pos = new Position();//初始化人物位置
public char mode;//人物动作
public void movement(char mode, Map world)//人物移动方法和地点
{
switch (mode)
{
case 'w':
{
if (world.map[pos.row - 1, pos.col] != "█")
{
world.map[pos.row, pos.col] = " ";
Console.SetCursorPosition(2 * pos.col, pos.row);//重设光标位置
Console.Write(" ");
pos.set(pos.row - 1, pos.col);
world.map[pos.row, pos.col] = Player.icon;
Console.SetCursorPosition(2 * pos.col, pos.row);
Console.Write(world.map[pos.row, pos.col]);
}
break;
}
case 's':
{
if (world.map[pos.row + 1, pos.col] != "█")
{
world.map[pos.row, pos.col] = " ";
Console.SetCursorPosition(2 * pos.col, pos.row);
Console.Write(" ");
pos.set(pos.row + 1, pos.col);
world.map[pos.row, pos.col] = Player.icon;
Console.SetCursorPosition(2 * pos.col, pos.row);
Console.Write(world.map[pos.row, pos.col]);
}
break;
}
case 'a':
{
if (world.map[pos.row, pos.col - 1] != "█")
{
world.map[pos.row, pos.col] = " ";
Console.SetCursorPosition(2 * pos.col, pos.row);
Console.Write(" ");
pos.set(pos.row, pos.col - 1);
world.map[pos.row, pos.col] = Player.icon;
Console.SetCursorPosition(2 * pos.col, pos.row);
Console.Write(world.map[pos.row, pos.col]);
}
break;
}
case 'd':
{
if (world.map[pos.row, pos.col + 1] != "█")
{
world.map[pos.row, pos.col] = " ";
Console.SetCursorPosition(2 * pos.col, pos.row);
Console.Write(" ");
pos.set(pos.row, pos.col + 1);
world.map[pos.row, pos.col] = Player.icon;
Console.SetCursorPosition(2 * pos.col, pos.row);
Console.Write(world.map[pos.row, pos.col]);
}
break;
}
}
}
}
class Player : Move
{
public const int row0 = 10;//出生点行数
public const int col0 = 10;//出生点列数
public int hp = 3;//生命数
public const string icon = "★";//人物图标
public void chushi()//初始化人物属性
{
pos.set(row0, col0);
}
}
class EnemyPos
{
public int row1 = 3;
public int col1 = 3;
public void set(int Row1, int Col1)
{
row1 = Row1;
col1 = Col1;
}
}
class EnemyMove : Move
{
static public Random ran = new Random();
EnemyPos enemy = new EnemyPos();
public void movement(char mode, Map world)
{
if (mode == 'w' || mode == 'd')
{
world.map[enemy.row1, enemy.col1] = " ";
enemy.row1 = ran.Next(2, 28);
enemy.col1 = ran.Next(2, 28);
Console.SetCursorPosition(2 * enemy.col1, enemy.row1);
Console.Write(" ");
enemy.set(enemy.row1, enemy.col1);
world.map[enemy.row1, enemy.col1] = Enemy.icon;
Console.SetCursorPosition(2 * enemy.col1, enemy.row1);
Console.Write(world.map[enemy.row1, enemy.col1]);
}
}
}
class Enemy : EnemyMove
{
public EnemyPos enemypos = new EnemyPos();
public const int row1 = 3;//出生点行数
public const int col1 = 3;//出生点列数
public const string icon = "* ";
public void chushi()//初始化人物属性
{
enemypos.set(row1, col1);
}
}
//class EatPos
//{
// static public Random ran = new Random();
// public int row2 = ran.Next(2, 29);
// public int col2 = ran.Next(2, 29);
//}
//class Eat
//{
// public EatPos eatpos = new EatPos();
// public const string icon = "$ ";
// public void chushi(Map world)
// {
// if (world.map[eatpos.row2, eatpos.col2] != "█")
// {
// //world.map[eatpos.row2, eatpos.col2] = " ";
// //Console.SetCursorPosition(2 * eatpos.col2, eatpos.row2);
// //Console.Write(" ");
// world.map[eatpos.row2, eatpos.col2] = Eat.icon;
// Console.SetCursorPosition(2 * eatpos.col2, eatpos.row2);
// Console.Write(world.map[eatpos.row2, eatpos.col2]);
// }
// }
//}
class GameController
{
public Map world = new Map();
public Player player = new Player();
public EnemyMove move = new EnemyMove();
public Enemy enemy = new Enemy();
//public Eat eat1 = new Eat();
//public Eat eat2 = new Eat();
//public Eat eat3 = new Eat();
public ConsoleKeyInfo input;//接受键盘输入的值
public void chushi()
{
player.chushi();
world.Chushi();
//eat1.chushi(world);
//eat2.chushi(world);
//eat3.chushi(world);
world.map[enemy.enemypos.row1, enemy.enemypos.col1] = Enemy.icon;
world.map[player.pos.row, player.pos.col] = Player.icon;//初始化玩的图标
// world.map[eat1.eatpos.row2, eat1.eatpos.col2] = Eat.icon;
// world.map[eat2.eatpos.row2, eat2.eatpos.col2] = Eat.icon;
// world.map[eat3.eatpos.row2, eat3.eatpos.col2] = Eat.icon;
//}
}
}
class Program
{
public static bool iswin = true;
//int x = 60; // 看你要是计时多少秒
//public static void Writechar()
//{
// x--;
// Console.WriteLine(x.toString());
//}
static void Main(string[] args)
{
// System.Threading.Timer myTimer = new Timer(1000);
// myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
// myTimer.Start();
Console.WriteLine("The game is begin.
If you press 'd',it will creat '*'.
When there is not '*',you are win.
");
Console.WriteLine("Please press 's' to continue.");
ConsoleKeyInfo R = Console.ReadKey(false);
if (R.KeyChar == 's')//按s键时继续
Console.Clear();
GameController gamecontroller = new GameController();//实例化GameController类为gameobject对象
gamecontroller.chushi();//游戏初始化
gamecontroller.world.Draw();//绘制地图
while (true)
{
gamecontroller.input = Console.ReadKey(false);//获取键盘输入
gamecontroller.player.mode = gamecontroller.input.KeyChar;
gamecontroller.move.movement(gamecontroller.player.mode, gamecontroller.world);
gamecontroller.player.movement(gamecontroller.player.mode, gamecontroller.world);
Console.SetCursorPosition(0, 30); //重置光标位置
Console.Write(' ');
Console.SetCursorPosition(0, 30);
for (int i = 2; i < 29; i++)
for (int j = 2; j < 29; j++)
{
if (gamecontroller.world.map[i, j] == "* ")
{ iswin = false;break; }
}
if (iswin)
{
Console.Clear();
Console.WriteLine("Congragulation!
You are so excellent!
You are win!");
}//这部分没有完善
}
}
}
}