Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MazeGame
- {
- class Program
- {
- static int ROW = 7;
- static char[] map =
- {
- '#', '#', '#', '#', '#', '#','#',
- '#', ' ', ' ', ' ', ' ', ' ','#',
- '#', ' ', '#', '#', '#', ' ','#',
- '#', ' ', '#', 'G', '#', ' ','#',
- '#', ' ', '#', ' ', '#', ' ','#',
- '#', ' ', '#', ' ', '#', ' ','#',
- '#', ' ', '#', ' ', '#', ' ','#',
- '#', ' ', '#', ' ', '#', ' ','#',
- '#', 'P', '#', ' ', ' ', ' ','#',
- '#', '#', '#', '#', '#', '#','#',
- };
- static void DrawMap()
- {
- Console.Clear();
- for (int i = 0; i < map.Length; i++)
- {
- Console.Write(map[i]);
- if ((i + 1) % ROW == 0)
- {
- Console.WriteLine();
- }
- }
- }
- static void MovePlayer(ConsoleKeyInfo key)
- {
- int playerPos = Array.IndexOf(map, 'P');
- int playerNextPos = 0;
- switch (key.KeyChar)
- {
- case 'w':
- playerNextPos = playerPos - ROW;
- break;
- case 'a':
- playerNextPos = playerPos - 1;
- break;
- case 's':
- playerNextPos = playerPos + ROW;
- break;
- case 'd':
- playerNextPos = playerPos + 1;
- break;
- }
- if (map[playerNextPos] != '#')
- {
- map[playerNextPos] = 'P';
- map[playerPos] = ' ';
- playerPos = playerNextPos;
- }
- }
- static void MovePlayer(string key)
- {
- int playerPos = Array.IndexOf(map, 'P');
- int playerNextPos = 0;
- if (key == "a")
- {
- playerNextPos = playerPos - 1;
- }
- else if (key == "d")
- {
- playerNextPos = playerPos + 1;
- }
- else if (key == "w")
- {
- playerNextPos = playerPos - 5;
- }
- else if (key == "s")
- {
- playerNextPos = playerPos + 5;
- }
- else
- {
- return;
- }
- if (map[playerNextPos] != '#')
- {
- map[playerNextPos] = 'P';
- map[playerPos] = ' ';
- playerPos = playerNextPos;
- }
- }
- static bool CheckGoal()
- {
- if (Array.IndexOf(map, 'G') < 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static void Main(string[] args)
- {
- while (true)
- {
- DrawMap();
- // string key = Console.ReadLine();
- var key = Console.ReadKey(true);
- MovePlayer(key);
- if (CheckGoal())
- {
- break;
- }
- }
- DrawMap();
- Console.WriteLine("ゲームクリア");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement