這兩天通過視頻,瞭解一下C#的基本用法,C#看起來語法和java差不多,還沒有仔細研究,這是跟着視頻做的第一個DEMO
using System;
namespace myfristgame
{
class MainClass
{
public static void Main (string[] args)
{
//遊戲啓動時候初始化值
//常量只能給常量,能給變量或者表達式 否則會報錯
//const int MapViewX = Convert.ToInt32 ("20");
const int MapViewX = 16;
const int MapViewY = 46;
bool isStart = true;
const int EmptyLine = 0;
const int TBLine = 1;
const int LRLine = 2;
const int Player = 3;
int playerPosX = 1;
int playerPosY = 1;
int playerOldPosX = 1;
int playerOldPosY = 1;
//地圖數據
int[,]mapData = new int[MapViewX,MapViewY];
//初化地圖數據
for(int i = 0;i<MapViewX;i++){
for(int j = 0;j<MapViewY;j++){
if (i == 0 || i == MapViewX - 1) {
mapData [i,j] = TBLine;
} else if (j == 0 || j == MapViewY - 1) {
mapData [i,j] = LRLine;
} else if(playerPosX==i&&j==playerPosY){
mapData [i,j] = Player;
} else{
mapData [i,j] = EmptyLine;
}
}
}
//根據數據初始化地圖
String[,]map = new String[MapViewX,MapViewY];
//根據地圖數據初始化地圖
for(int i=0;i<MapViewX;i++){
String str = "";
for(int j=0;j<MapViewY;j++){
int data = mapData[i,j];
switch (data) {
case EmptyLine:
str += " ";
break;
case TBLine:
str += "-";
break;
case LRLine:
str += "|";
break;
case Player:
str += "P";
break;
}
}
Console.WriteLine (str);
}
//遊戲啓動進行時操作
String[,]mapVies = new string[MapViewX,MapViewY];
while(isStart){
Console.WriteLine ("Game is start!");
Console.ReadLine();
String inputKey = Console.ReadKey().KeyChar.ToString();
if (inputKey.Equals ("q")|| inputKey.Equals ("Q")) {
isStart = false;
break;
} else {
playerOldPosX = playerPosX;
playerOldPosY = playerPosY;
if("w".Equals(inputKey)||"W".Equals(inputKey)){
if(playerPosX>1){
playerPosX--;
}
}
if("s".Equals(inputKey)||"S".Equals(inputKey)){
if(playerPosX<MapViewX-2){
playerPosX++;
}
}
if("d".Equals(inputKey)||"D".Equals(inputKey)){
if(playerPosY<MapViewY-2){
playerPosY++;
}
}
if("a".Equals(inputKey)||"A".Equals(inputKey)){
if(playerPosY>1){
playerPosY--;
}
}
mapData [playerOldPosX,playerOldPosY] = EmptyLine;
mapData [playerPosX,playerPosY] = Player;
//根據地圖數據初始化地圖
for(int i=0;i<MapViewX;i++){
String str = "";
for(int j=0;j<MapViewY;j++){
int data = mapData[i,j];
switch (data) {
case EmptyLine:
str += " ";
break;
case TBLine:
str += "-";
break;
case LRLine:
str += "|";
break;
case Player:
str += "P";
break;
}
}
Console.WriteLine (str);
}
}
}
//遊戲停止時的操作
Console.WriteLine ("Game is Over! Come agin");
}
}
}