#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
using namespace std;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
#define W 40
#define H 40
struct snake{
int x,y;
};
snake snakes[W*H];
int len = 0;
char dir = 'r';
int foodx = 0,foody = 0;
int FPS = 200;
int score = 0, speed = (220-FPS)/20;
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void setPos(int x,int y) {
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
csbiInfo.dwCursorPosition.X = x;
csbiInfo.dwCursorPosition.Y = y;
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
}
void COLOR_PRINT(const char* s, int color){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
printf(s);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
bool GameOver(){
snake t = snakes[0];
if(t.x <= 0){
snakes[0].x = W-2;
}
if(t.x >= W-1){
snakes[0].x = 0;
}
if(t.y <= 0){
snakes[0].y = H-2;
}
if(t.y >= H-1){
snakes[0].y = 0;
}
for(int i=3;i<=len-1;i++){
if(t.x == snakes[i].x && t.y == snakes[i].y){
return true;
}
}
return false;
}
bool inSnake(int x, int y){
for(int i=0;i<len;i++){
if(snakes[i].x == x && snakes[i].y == y) return false;
}
return true;
}
void createFood(){
srand(time(NULL));
while(1){
foodx = rand()%(W-2)+1;
foody = rand()%(H-2)+1;
if(inSnake(foodx,foody)) return;
}
}
void init(){
len = 4;
snakes[0].x = 2;
snakes[0].y = 2;
for(int i=1;i<len;i++){
snakes[i].x = snakes[i-1].x + 1;
snakes[i].y = snakes[i-1].y;
}
createFood();
}
void tips(){
setPos(W*2+4, 3);
printf("游戏说明:\n");
setPos(W*2+4, 5);
printf("按W A S D 或者 ↑ ↓ ← → 操控游戏\n");
setPos(W*2+4, 7);
printf("按Q减速,按E加速\n");
setPos(W*2+4, 9);
printf("当前食物分数:%d\n", 100+10*speed);
setPos(W*2+4, 11);
printf("当前速度:%d\n", speed);
setPos(W*2+4, 13);
printf("总 分:%d\n", score);
}
void draw(){
for(int i=0;i<W;i++){
for(int j=0;j<H;j++){
if(i==0 || j==0 || i == W-1 || j == H-1) {
setPos(i*2,j);
printf("■");
}
}
}
for(int i=0;i<len;i++){
setPos(snakes[i].x*2,snakes[i].y);
if(i == 0) COLOR_PRINT("■", 4);
else COLOR_PRINT("■", 1);
}
setPos(foodx*2,foody);
COLOR_PRINT("■", 10);
FPS = min(FPS, 200);
FPS = max(FPS, 60);
speed = (220-FPS)/20;
tips();
}
void move(){
int dx = 0, dy = 0;
if(dir == 'u') dy = -1;
else if(dir == 'd') dy = 1;
else if(dir == 'l') dx = -1;
else if(dir == 'r') dx = 1;
setPos(snakes[len-1].x*2,snakes[len-1].y);
printf(" ");
for(int i=len-1;i>=1;i--){
snakes[i].x = snakes[i-1].x;
snakes[i].y = snakes[i-1].y;
}
snakes[0].x = snakes[0].x + dx;
snakes[0].y = snakes[0].y + dy;
}
void keyDown(){
char key;
while(kbhit()) key = _getch(); switch(key){
case 'W':
case 'w':
case 72:
if(dir != 'd') dir = 'u';
break;
case 'S':
case 's':
case 80:
if(dir != 'u') dir = 'd';
break;
case 'A':
case 'a':
case 75:
if(dir != 'r') dir = 'l';
break;
case 'D':
case 'd':
case 77:
if(dir != 'l') dir = 'r';
break;
case 'E':
case 'e':
FPS -= 20;
break;
case 'Q':
case 'q':
FPS += 20;
break;
}
}
void eatFood(){
if(snakes[0].x == foodx && snakes[0].y == foody){
len++;
for(int i=len-1;i>=1;i--){
snakes[i].x = snakes[i-1].x;
snakes[i].y = snakes[i-1].y;
}
snakes[0].x = foodx;
snakes[0].y = foody;
setPos(foodx*2,foody);
printf(" ");
score += 100+speed*10;
createFood();
}
}
int main(){
HideCursor(); init(); tips();
while(1){
if(GameOver()) break; keyDown(); move(); eatFood(); draw(); Sleep(FPS);
}
setPos((W+1)/2,(H-1)/2);
COLOR_PRINT("游戏结束!,您获得了", 4);
printf("%d分", score);
setPos(0,H+2);
system("pause");
return 0;
}
共 20 条回复
我是来搞笑的,嘻嘻嘻
抵制不良游戏,拒绝盗版游戏。 注意自我保护,谨防受骗上当。 适度游戏益脑,沉迷游戏伤身。 合理安排时间,享受健康生活。
我六千多分
666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666
👍