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
| #include <bits/stdc++.h> #include <windows.h> #include <conio.h>
const int width = 60,height = 28;
using namespace std;
char input; bool running; int score;
struct FOOD{ int x,y; };
struct SNACK{ int x,y; };
FOOD food;
deque<SNACK>snack;
void to(int x,int y){ COORD pos; HANDLE hOutput; pos.X = x; pos.Y = y; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput, pos); }
void print(){ for(int i = 0;i<snack.size();i++){ to(snack[i].x,snack[i].y); cout<<"■"; } }
void create_food(){ while(true){ bool flag = 1; food.x = (rand()%(width-1))+1; food.y = (rand()%(height-1))+1; if(food.x%2!=0)continue; for(int i = 0;i<snack.size();i++){ if(food.x==snack[i].x and food.y==snack[i].y){ flag = 0; break; } } if(flag==1)break; } to(food.x,food.y); cout<<"■"; }
void setup(){ running = 1; score = 0; input = 'w'; int dx[] = {2,0,-2,0}; int dy[] = {0,1,0,-1}; int x = 0,y = 0; for(int i = 0;i<4;){ to(x,y); cout<<"■"; if(x+dx[i]>width or y+dy[i]>height or x+dx[i]<0 or y+dy[i]<0){ i++; continue; } x+=dx[i],y+=dy[i]; } for(int i = 0;i<=10;i+=2){ snack.push_back({24+i,20}); } print(); to(80,12); cout<<"Score = "<<score; create_food(); }
void get_input(){ if(input!='s' and GetAsyncKeyState(VK_UP))input = 'w'; if(input!='w' and GetAsyncKeyState(VK_DOWN))input = 's'; if(input!='a' and GetAsyncKeyState(VK_RIGHT))input = 'd'; if(input!='d' and GetAsyncKeyState(VK_LEFT))input = 'a'; }
int check(int x,int y){ if(x==0 or x==width or y==0 or y==height){ return 0; } for(int i = 0;i<snack.size();i++){ if(snack[i].x==x and snack[i].y==y){ return 0; } } return 1; }
void update_snack(){ int now_x = snack.front().x; int now_y = snack.front().y; if(input=='w')now_y--; if(input=='a')now_x-=2; if(input=='s')now_y++; if(input=='d')now_x+=2; if(check(now_x,now_y)==0){ running = 0; return; } snack.push_front({now_x,now_y}); if(snack.front().x==food.x and snack.front().y==food.y){ to(88,12); score++; cout<<score; create_food(); } else{ to(snack.back().x,snack.back().y); cout<<" "; snack.pop_back(); } }
int main(){ srand(time(NULL)); system("chcp 65001"); setup(); while(running){ get_input(); update_snack(); print(); Sleep(100); } to(80,14); cout<<"Game Over!"; to(80,16); system("pause"); }
|