1.요구 사항
-콘솔 뱀게임 만들기
2.결과 화면 및 샘플
3.소스 코드
//Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using System.Collections; namespace Snake { class Program { public static void Main() { Map map = new Map(); SnakeBody snake = new SnakeBody(); Control control = new Control(); Food food = new Food(); ConsoleKeyInfo command; Console.SetWindowSize(110, 60); bool loop = true; do { while (Console.KeyAvailable == false) { Console.Clear(); if (control.CookTime()) food.MakeFood(snake,map); control.PrintMap(snake,map); if (control.MoveSnake(snake, map)) { loop = false; break; } Thread.Sleep(150); } // Loop until input is entered. if (!loop) break; command = Console.ReadKey(true); control.WhereGo(command); } while (command.Key != ConsoleKey.Escape); Console.Clear(); Console.Write("주금"); Console.ReadKey(); } } } | cs |
//snake body
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; namespace Snake { public class SnakeBody { int[] head = new int[3] { 3, 3, 7 }; int tailNum = 6; public int[] last = new int[2]; public ArrayList bodyList = new ArrayList(); public SnakeBody() { bodyList.Add(head[0]); bodyList.Add(head[1]); bodyList.Add(head[2]); last[0] = 3; last[1] = 3; } public void SetBody() { bodyList.Add(last[0]); bodyList.Add(last[1]); bodyList.Add(tailNum); } } } | cs |
//control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; namespace Snake { class Control { public string whereGo = "+x"; int cook = 0; public static void ColoredConsoleWrite(ConsoleColor color, string text) { ConsoleColor originalColor = Console.ForegroundColor; Console.ForegroundColor = color; Console.Write(text); Console.ForegroundColor = originalColor; } public Control() { } public bool CookTime() { bool Makeit = false; cook++; if(cook == 10) { cook = 0; Makeit = true; } return Makeit; } public void PrintMap(SnakeBody Snake,Map map) { for(int i = 0; i < map.mapInfo01.GetLength(0);i++) { for(int j = 0; j< map.mapInfo01.GetLength(1); j++) { if (map.mapInfo01[i, j] == 1) ColoredConsoleWrite(ConsoleColor.DarkGray, map.map01[i, j]); else if (map.mapInfo01[i, j] == 4) ColoredConsoleWrite(ConsoleColor.Yellow,"♨"); else if (CheckHead(i, j, Snake)) ColoredConsoleWrite(ConsoleColor.Cyan, "●"); else if (CheckTail(i, j, Snake)) ColoredConsoleWrite(ConsoleColor.Cyan, "▣"); else ColoredConsoleWrite(ConsoleColor.White, " "); } Console.WriteLine(); } } public bool MoveSnake(SnakeBody snake,Map map) { bool dieCheck = false; int x=0; int y=0; int info = 0; for(int roop =0; roop < (snake.bodyList.Count / 3); roop++) { y = int.Parse( snake.bodyList[roop * 3].ToString()); x = int.Parse(snake.bodyList[roop * 3+1].ToString()); info = int.Parse(snake.bodyList[roop * 3+2].ToString()); } if (whereGo == "+x") { snake.last[0] = int.Parse(snake.bodyList[0].ToString()); snake.last[1] = int.Parse(snake.bodyList[1].ToString()); y = int.Parse(snake.bodyList[0].ToString()); x = int.Parse(snake.bodyList[1].ToString()) + 1; if (map.mapInfo01.GetLength(1) < x) x--; if (map.mapInfo01[y, x] == 1) dieCheck = true; else if (map.mapInfo01[y, x] == 4) MakeTail(y, x, snake, map); snake.bodyList[0] = y; snake.bodyList[1] = x; MoveTail(snake); if (EatTail(y, x, snake)) dieCheck = true; } else if (whereGo == "-x") { snake.last[0] = int.Parse(snake.bodyList[0].ToString()); snake.last[1] = int.Parse(snake.bodyList[1].ToString()); y = int.Parse(snake.bodyList[0].ToString()); x = int.Parse(snake.bodyList[1].ToString()) - 1; if (0 > x) x++; if (map.mapInfo01[y, x] == 1) dieCheck = true; else if (map.mapInfo01[y, x] == 4) MakeTail(y, x, snake, map); snake.bodyList[0] = y; snake.bodyList[1] = x; MoveTail(snake); if (EatTail(y, x, snake)) dieCheck = true; } else if (whereGo == "+y") { snake.last[0] = int.Parse(snake.bodyList[0].ToString()); snake.last[1] = int.Parse(snake.bodyList[1].ToString()); y = int.Parse(snake.bodyList[0].ToString()) + 1; x = int.Parse(snake.bodyList[1].ToString()); if (map.mapInfo01.GetLength(0) < y) y--; if (map.mapInfo01[y, x] == 1) dieCheck = true; else if (map.mapInfo01[y, x] == 4) MakeTail(y, x, snake, map); snake.bodyList[0] = y; snake.bodyList[1] = x; MoveTail(snake); if (EatTail(y, x, snake)) dieCheck = true; } else if (whereGo == "-y") { snake.last[0] = int.Parse(snake.bodyList[0].ToString()); snake.last[1] = int.Parse(snake.bodyList[1].ToString()); y = int.Parse(snake.bodyList[0].ToString()) - 1; x = int.Parse(snake.bodyList[1].ToString()); if (0> y) y++; if (map.mapInfo01[y, x] == 1) dieCheck = true; else if (map.mapInfo01[y, x] == 4) MakeTail(y,x,snake,map); snake.bodyList[0] = y; snake.bodyList[1] = x; MoveTail(snake); if (EatTail(y, x, snake)) dieCheck = true; } return dieCheck; } bool EatTail(int i,int j, SnakeBody snake) { bool eat = false; for(int roop =1; roop < (snake.bodyList.Count)/3; roop++) { if ((i == int.Parse(snake.bodyList[roop * 3].ToString()) && j == int.Parse(snake.bodyList[roop * 3 + 1].ToString()))) eat = true; } return eat; } bool CheckHead(int i,int j,SnakeBody snake) { int x = 0; int y = 0; int.TryParse(snake.bodyList[0].ToString(), out y); int.TryParse(snake.bodyList[1].ToString(), out x); bool checkHead = false; if (i == y && j == x) checkHead = true; return checkHead; } void MoveTail(SnakeBody snake) { for(int roop =1; roop < (snake.bodyList.Count / 3); roop++) { int stayY =int.Parse( snake.bodyList[roop * 3].ToString()); int stayX = int.Parse(snake.bodyList[roop * 3 + 1].ToString()); snake.bodyList[roop * 3] = snake.last[0]; snake.bodyList[roop * 3 + 1] = snake.last[1]; snake.last[0] = stayY; snake.last[1] = stayX; } } void MakeTail(int y,int x,SnakeBody snake,Map map) { int yPut = int.Parse(snake.bodyList[snake.bodyList.Count - 3].ToString()); int xPut = int.Parse(snake.bodyList[snake.bodyList.Count - 2].ToString()); if (whereGo == "-x") xPut++; if (whereGo == "+x") xPut--; if (whereGo == "-y") yPut++; if (whereGo == "+y") yPut--; snake.bodyList.Add(yPut); snake.bodyList.Add(xPut); snake.bodyList.Add(6); map.mapInfo01[y, x] = 0; } bool CheckTail(int i, int j, SnakeBody snake) { int x = 0; int y = 0; bool checkTail = false; for(int roop=0; roop < (snake.bodyList.Count / 3); roop++) { int.TryParse(snake.bodyList[roop*3].ToString(), out y); int.TryParse(snake.bodyList[roop*3+1].ToString(), out x); if (y == i && x == j) checkTail = true; } return checkTail; } public void WhereGo(ConsoleKeyInfo command) { if(command.Key == ConsoleKey.RightArrow) { this.whereGo = "+x"; } else if (command.Key == ConsoleKey.LeftArrow) { this.whereGo = "-x"; } else if (command.Key == ConsoleKey.DownArrow) { this.whereGo = "+y"; } else if (command.Key == ConsoleKey.UpArrow) { this.whereGo = "-y"; } } } } | cs |
//Map
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; namespace Snake { public class Map { public string[,] map01 = new string[32, 38];//01번 맵 정보 public int[,] mapInfo01 = new int[32, 38]; public Map() { MapInfoMake(); MapMake(); } void MapMake() { //출력되는 맵 정보 불러오기(특수문자) string path = @"D:\map.txt"; string[] lines = System.IO.File.ReadAllLines(path); int i = 0; foreach (string line in lines) { var arrX = line.Split(','); for (int j = 0; j < 38; j++) { map01[i, j] = arrX[j]; } i++; } } //맵 정보 초기화 void MapInfoMake() { //맵 정보 불러오기(숫자 - 벽등 비교정보) string path = @"D:\mapinfo.txt"; string[] lines = System.IO.File.ReadAllLines(path); int i = 0; foreach (string line in lines) { var arrX = line.Split(','); for (int j = 0; j < 38; j++) { mapInfo01[i, j] = int.Parse(arrX[j]); } i++; } } } } | cs |
//Food
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace Snake { class Food { public Food() { } public void MakeFood(SnakeBody snake,Map map) { int x; int y; while (true) { Random random = new Random(); Thread.Sleep(150); x = random.Next(2, map.mapInfo01.GetLength(1)-1); Thread.Sleep(150); y = random.Next(2, map.mapInfo01.GetLength(0)-1); if ((y != int.Parse(snake.bodyList[0].ToString())) && (x != int.Parse(snake.bodyList[1].ToString())))break; } map.mapInfo01[y, x] = 4; } } } | cs |
'정리 카테고리 > 실습_과제' 카테고리의 다른 글
실습_Unity_랜덤목표 따라가는 캐릭터 (0) | 2018.03.29 |
---|---|
실습_미니맵이 보이는 미로 게임 (0) | 2018.03.24 |
실습_특수문자로 맵 구현하기 (0) | 2018.03.14 |
실습_배열,맵이동 맵출력 프로그램 (0) | 2018.03.13 |
실습_최종 포스기계(상품명,상품개수,금액 정리및 금액 자리수(,)표시 (0) | 2018.03.13 |