2d命令行小游戏源码
2024-08-23 05:38:41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace _2d
{
class Dit
{
public int x, y;
}
class Map
{
public void mp()
{
const int buy = 20;
for (int i = 0; i < buy; i++)
{
for (int j = 0; j < buy; j++)
{
if (i == 0 || i == buy - 1 || j == 0 || j == buy - 1)
Console.Write("█");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
const char b='@';
const char d = ' ';
const char e = '*';
public int fenshu=0;
public void set(int x, int y, char b)
{
int buy = 20;
Console.SetCursorPosition(2 * y, x);
Console.Write(b);
Console.SetCursorPosition(0, buy);
}
public void put()
{
int speed = 160;
int buy=20;
Random where = new Random();
Random _where=new Random();
Dit renwu = new Dit();
renwu.x = _where.Next(1, buy - 2);
renwu.y = _where.Next(1, buy - 2);
set(renwu.x, renwu.y, b);
bool food = false;
Dit _food=new Dit();
_food.x=where.Next(1,buy-2);
_food.y=where.Next(1,buy-2);
set(_food.y,_food.x,e);
char c;
for (; ; )
{
c = Console.ReadKey(true).KeyChar;
switch (c)
{
case 'w': set(renwu.x, renwu.y, d); set(renwu.x - 1, renwu.y, b); renwu.x--; Thread.Sleep(speed); break;
case 'a': set(renwu.x, renwu.y, d); set(renwu.x, renwu.y - 1, b); renwu.y--; Thread.Sleep(speed); break;
case 's': set(renwu.x, renwu.y, d); set(renwu.x + 1, renwu.y, b); renwu.x++; Thread.Sleep(speed); break;
case 'd': set(renwu.x, renwu.y, d); set(renwu.x, renwu.y + 1, b); renwu.y++; Thread.Sleep(speed); break;
}
if (renwu.x == 0 || renwu.y == 0 || renwu.x == buy - 1 || renwu.y == buy - 1)
{
Console.Clear();
Console.WriteLine("GameOver!\n您的得分是{0}",fenshu);
}
if(renwu.x==_food.y&&renwu.y==_food.x)
{
food=true;
_food.x = where.Next(1, buy - 2);
_food.y = where.Next(1, buy - 2);
set(_food.y, _food.x, e);
fenshu++;
}
}
}
}
class Menu
{
public void paly()
{
Console.WriteLine("2d命令行小游戏");
Console.WriteLine("输入1开始游戏\n请将输入法调至英文状态");
}
public void start()
{
int a = Convert.ToInt32(Console.ReadLine());
if (a == 1)
{
Console.Clear();
Map _map = new Map();
_map.mp();
_map.put();
}
}
}
class Program
{
static void Main(string[] args)
{
Menu _menu = new Menu();
Console.CursorVisible = false;
_menu.paly();
_menu.start();
Console.ReadLine();
}
}
}
最新文章
- flexbox布局神器
- ACM/ICPC 之 最小割转网络流(POJ3469)
- C#中的Lambda表达式的演化过程
- Spring集成MyBatis完整示例
- jmeter随笔(1)-在csv中数据为json格式的数据不完整
- 46.谈谈SDRAM的作用
- ip_conntrack 实现
- 阿里云配置php环境 ubuntu12.04 32 nginx+php5+mysql
- JS学习笔记Day14
- Centos6.8-hadoop-2.7.2 64 bit源码编译(伪分布-5大守护进程在本机上)
- 基于zepto的移动端轻量级日期插件
- 一句话说清楚cache和buffer
- JS 中 if / if...else...替换方式
- MySQL中整型和字符串类型指定长度的含义
- python 菜鸟入门
- 【纯代码】Swift-自定义PickerView单选(可修改分割线颜色、修改字体大小、修改字体颜色。)(可根据需要自己扩展)
- 7个常见Javascript框架介绍
- $_SERVER[&#39;SCRIPT_NAME&#39;]、$_SERVER[&#39;SCRIPT_FILENAME&#39;]及__FILE__不同
- 解决IE6的PNG透明
- 关于android R.java文件无法创建的问题