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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| #include "Game.h"
void HideCursor() { CONSOLE_CURSOR_INFO conCursor = {0}; conCursor.dwSize = 1; conCursor.bVisible = FALSE; HANDLE hStdoutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(hStdoutput, &conCursor); }
void SetPosOutput(int x, int y) { COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); }
void SetConColor(int rgb) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rgb); }
void InitWall() { for (int w = 0; w < WIDTH; w++) { for (int h = 0;h < HEIGTH;h++) { if (w == 0 || w == WIDTH-1) { SetConColor(0xC); SetPosOutput(w * 2,h); printf("□"); } else if (h == 0 || h == HEIGTH-1) { SetConColor(0xC); SetPosOutput(w * 2, h); printf("□"); } } } }
void InitSnake(PGameStatus pgames) { PSnakeCell cell = NULL; for (int i = 0;i<SnakeLen;i++) { cell = malloc(sizeof(SnakeCell)); cell->x = WIDTH / 2 + i; cell->y = HEIGTH / 2; cell->pNext = NULL; if (pgames->psnake == NULL) { pgames->psnake = cell; } else { cell->pNext = pgames->psnake; pgames->psnake = cell; } PSnakeCell tmp = pgames->psnake; SetConColor(0xD); while (tmp) { SetPosOutput(tmp->x * 2, tmp->y); printf("■"); tmp = tmp->pNext; } } }
void InitFood(PGameStatus pgames) { srand(time(NULL)); int f_x = 0; int f_y = 0; while (1) { int is_eque = 0; PSnakeCell tmp = pgames->psnake; while (tmp) { f_x = rand() % (WIDTH - 2) + 1; f_y = rand() % (HEIGTH - 2) + 1; if ((tmp->x == f_x) && (tmp->y == f_y)) { is_eque = 1; } tmp = tmp->pNext; } if (!is_eque) break; } pgames->food.x = f_x; pgames->food.y = f_y; SetPosOutput(pgames->food.x*2, pgames->food.y); SetConColor(0xE); printf("●"); }
void Readme() { SetConColor(9); SetPosOutput(WIDTH*2 + 3, HEIGTH/2 - 10); printf("游戏说明"); SetPosOutput(WIDTH * 2 + 3, HEIGTH / 2 - 8); printf("↑ ↓← →"); }
void OutScores(PGameStatus pgames) { SetConColor(9); SetPosOutput(WIDTH * 2 + 3, HEIGTH / 2 - 5); printf("游戏分数:%d", pgames->scores); }
void PlayGameExce(PGameStatus pgames) { while (pgames->live_status== NORMAL) { if (GetAsyncKeyState(VK_UP) && pgames->dir != DOWN) { pgames->dir = UP; } else if (GetAsyncKeyState(VK_DOWN) && pgames->dir != UP) { pgames->dir = DOWN; } else if (GetAsyncKeyState(VK_LEFT) && pgames->dir != RIGHT) { pgames->dir = LEFT; } else if (GetAsyncKeyState(VK_RIGHT) && pgames->dir != LEFT) { pgames->dir = RIGHT; } SnakeMove(pgames); IsSnakeDeath(pgames); OutScores(pgames); Sleep(300); }; if (pgames->live_status == WALL_DEATH) { MessageBox(NULL, L"碰到墙壁,游戏结束!", NULL, NULL); } if (pgames->live_status == SELF_DEATH) { MessageBox(NULL, L"碰到自身,游戏结束!", NULL, NULL); } }
void SnakeMove(PGameStatus pgames) { int add = 0; PSnakeCell new_cell = malloc(sizeof(SnakeCell)); if (new_cell != NULL && pgames->dir == UP) { new_cell->x = pgames->psnake->x; new_cell->y = pgames->psnake->y - 1; } else if (pgames->dir == DOWN) { new_cell->x = pgames->psnake->x; new_cell->y = pgames->psnake->y +1; } else if (pgames->dir == LEFT) { new_cell->x = pgames->psnake->x - 1; new_cell->y = pgames->psnake->y; } else if (pgames->dir == RIGHT) { new_cell->x = pgames->psnake->x + 1; new_cell->y = pgames->psnake->y; } new_cell->pNext = pgames->psnake; pgames->psnake = new_cell; PSnakeCell tmp = pgames->psnake; if ((tmp->x == pgames->food.x) && (tmp->y == pgames->food.y)) { add = 1; pgames->scores = pgames->scores + 1; InitFood(pgames); }; while (tmp) { if (!add && tmp->pNext != NULL && tmp->pNext->pNext == NULL) { SetPosOutput(tmp->pNext->x * 2, tmp->pNext->y); printf(" "); free(tmp->pNext); tmp->pNext = NULL; } else{ SetConColor(0xD); SetPosOutput(tmp->x * 2, tmp->y); printf("■"); tmp = tmp->pNext; } } printf("\r\n"); }
void IsSnakeDeath(PGameStatus pgames) { if (pgames->psnake->x >= WIDTH-1 || pgames->psnake->x <= 0) { pgames->live_status = WALL_DEATH; } else if (pgames->psnake->y >= HEIGTH-1 || pgames->psnake->y <= 0) { pgames->live_status = WALL_DEATH; } PSnakeCell tmp = pgames->psnake->pNext; while (tmp) { if ((tmp->x == pgames->psnake->x) && (tmp->y == pgames->psnake->y)) { pgames->live_status = SELF_DEATH; break; } tmp = tmp->pNext; } }
|