贪吃蛇加速版
#include<cstdio>
#include<iostream>
#include<ctime>
#include<stdlib.h>
#include<windows.h>
#include <conio.h>
#include<bits/stdc++.h>
using namespace std;
#define frame_width 50
#define frame_height 25
typedef struct
{
int x,y;
} Food;
typedef struct
{
int x[100],y[100],len,state;
} Snake;
void gotoxy(int x,int y);
void print_map();
void get_newfood();
bool check_foodinsnake();
void move_snake();
void check_foodeating();
bool check_snakealive();
int score;
Snake snake;
Food food;
bool check_eaten;
int main()
{
system("color 0B");
do
{
system("cls");
print_map();
score=0,check_eaten=0;
while(1)
{
check_foodeating();
move_snake();
Sleep(max(50,300-score));
if(!check_snakealive())
break;
}
printf("Game Over!\n");
printf("1:Restart\t2:exit\n");
char com2;
cin>>com2;
if(com2=='2')
break;
}while(1);
}
void gotoxy(int x,int y)
{
COORD pos;
HANDLE han=GetStdHandle(STD_OUTPUT_HANDLE);
pos.X=y,pos.Y=x;
SetConsoleCursorPosition(han,pos);
}
void print_map()
{
for(int i=0;i<frame_height;i++)
{
gotoxy(i,0);
printf("#");
gotoxy(i,frame_width-1);
printf("#");
}
for(int i=0;i<frame_width;i++)
{
gotoxy(0,i);
printf("#");
gotoxy(frame_height-1,i);
printf("#");
}
snake.len=15;
snake.state='w';
snake.x[1]=frame_height/2;
snake.y[1]=frame_width/2;
gotoxy(snake.x[1],snake.y[1]);
printf("@");
for(int i=2;i<=snake.len;i++)
{
snake.x[i]=snake.x[i-1]+1;
snake.y[i]=snake.y[i-1];
gotoxy(snake.x[i],snake.y[i]);
printf("@");
}
get_newfood();
gotoxy(2,frame_width+3);
printf("WELCOME TO THE GAME OF RETRO SNAKE");
gotoxy(4,frame_width+3);
printf("UP: w");
gotoxy(6,frame_width+3);
printf("DOWN: s");
gotoxy(8,frame_width+3);
printf("LEFT: a");
gotoxy(10,frame_width+3);
printf("RIGHT:d");
gotoxy(12,frame_width+3);
printf("Your score:%d",score);
gotoxy(28,frame_width+3);
printf("Made by jokersio");
}
bool check_foodinsnake()
{
for(int i=1;i<=snake.len;i++)
if(snake.x[i]==food.x&&snake.y[i]==food.y)
return 1;
return 0;
}
void get_newfood()
{
do{
srand(time(0));
food.x=rand()%(frame_height-2)+1;
food.y=rand()%(frame_width-2)+1;
}while(check_foodinsnake());
gotoxy(food.x,food.y);
cout<<"$";
}
void move_snake()
{
char com;
while(kbhit())
com=getch();
if(!check_eaten)
{
gotoxy(snake.x[snake.len],snake.y[snake.len]);
printf(" ");
}
for(int i=snake.len;i>1;i--)
snake.x[i]=snake.x[i-1],
snake.y[i]=snake.y[i-1];
switch(com)
{
case 'w':
{
if(snake.state=='s')
snake.x[1]+=100;
else
snake.x[1]-=100,snake.state='w';
break;
}
case 's':
{
if(snake.state=='w')
snake.x[1]-=100;
else
snake.x[1]+=100,snake.state='s';
break;
}
case 'a':
{
if(snake.state=='d')
snake.y[1]+=100;
else
snake.y[1]-=100,snake.state='a';
break;
}
case 'd':
{
if(snake.state=='a')
snake.y[1]-=100;
else
snake.y[1]+=100,snake.state='d';
break;
}
default:
{
if(snake.state=='s')
snake.x[1]++;
else if(snake.state=='w')
snake.x[1]--;
else if(snake.state=='d')
snake.y[1]++;
else if(snake.state=='a')
snake.y[1]--;
break;
}
}
gotoxy(snake.x[1],snake.y[1]);
cout<<"@";
check_eaten=0;
gotoxy(frame_height,0);
}
void check_foodeating()
{
if(snake.x[1]==food.x&&snake.y[1]==food.y)
{
score+=100000;
check_eaten=20;
gotoxy(12,frame_width+3);
printf("Your score:%d",score);
snake.len++;
get_newfood();
}
}
bool check_snakealive()
{
if(snake.x[1]==0||snake.x[1]==frame_height-1||snake.y[1]==0||snake.y[1]==frame_width-1)
return 0;
for(int i=2;i<=snake.len;i++)
if(snake.x[i]==snake.x[1]&&snake.y[i]==snake.y[1])
return 0;
return 1;
}好玩的网站: 1.jinitaimei.com 2.xuziqian.my.enjoycode.com 3.code.xueersi.com 4.latexlive.com 5.speedtest.cn 6.geogebra.org 7.matools.com 我的练习数据 (更新于2023.9.28)