#include<windows.h>
#include<stdlib.h>
#include<fstream>
#include<conio.h>
#include<cstdio>
#include<time.h>
using namespace std;
#define X 21
#define Y 19
#define TIME 40
#define U 0
#define D 1
#define L 2
#define R 3
int MAX;
long long int _begin;
char cmap[21][19]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
{1,0,1,1,0,1,1,1,0,0,0,1,1,1,0,1,1,0,1},
{1,0,0,0,2,0,0,0,0,1,0,0,0,0,2,0,0,0,1},
{1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1},
{1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1},
{1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1},
{1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,1,1,0,1,0,1,1,3,1,1,0,1,0,1,1,0,1},
{1,0,0,0,0,1,0,1,3,3,3,1,0,1,0,0,0,0,1},
{1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1},
{1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1},
{1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1},
{1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1},
{1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1},
{1,0,0,0,2,1,0,0,0,1,0,0,0,1,2,0,0,0,1},
{1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
class Jwj{
public:
unsigned int x;
unsigned int y;
unsigned int way;
class Home{
public:
unsigned int x;
unsigned int y;
Home(){
x=9;
y=9;
}
}home;
Jwj(){
x=9;
y=9;
way=U;
}
}j[3];
class Player{
public:
unsigned int x;
unsigned int y;
unsigned int time;
unsigned int way;
unsigned int score;
int die;
Player(){
x=11;
y=9;
time=0;
way=L;
score=0;
die=1;
}
}p;
void color(int _color){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
return;
}
void gotoxy(int xx,int yy){
COORD position={yy*2,xx};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
return;
}
void init(){
system("mode con lines=23 cols=38");
system("title 吃豆人-JWJ");
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(int xx=0;xx<X;xx++)
for(int yy=0;yy<Y;yy++)
if(cmap[xx][yy]==0)
MAX++;
srand(time(NULL));
_begin=time(NULL);
return;
}
void drawmap(){
for(unsigned int xx=0;xx<X;xx++){
for(unsigned int yy=0;yy<Y;yy++){
if(cmap[xx][yy]==0){
color(14);
printf("·");
}else if(cmap[xx][yy]==1){
color(153);
printf(" ");
}else if(cmap[xx][yy]==2){
color(14);
printf("▲");
}else{
color(0);
printf(" ");
}
}
}
return;
}
void drawj(){
for(int tc=0;tc<3;tc++){
gotoxy(j[tc].x,j[tc].y);
color(tc+10);
printf("◆");
}
return;
}
void drawp(){
color(14);
gotoxy(p.x,p.y);
printf("●");
return;
}
void cplayer(){
color(0);
gotoxy(p.x,p.y);
printf(" ");
}
void eat(){
if(cmap[p.x][p.y]==0){
p.score++;
cmap[p.x][p.y]=3;
}
if(cmap[p.x][p.y]==2){
p.time=TIME;
cmap[p.x][p.y]=3;
}
}
void move(){
if(p.time>0)
p.time--;
switch(p.way){
case U:{
if(cmap[p.x-1][p.y]!=1)
p.x--;
break;
}
case D:{
if(cmap[p.x+1][p.y]!=1)
p.x++;
break;
}
case L:{
if(cmap[p.x][p.y-1]!=1)
p.y--;
break;
}
case R:{
if(cmap[p.x][p.y+1]!=1)
p.y++;
break;
}
}
return;
}
void getin(){
int in;
if(kbhit()!=0){
while(kbhit()!=0)
in=getch();
switch(in){
case 'A':case 'a':{
p.way=L;
break;
}
case 'D':case 'd':{
p.way=R;
break;
}
case 'W':case 'w':{
p.way=U;
break;
}
case 'S':case 's':{
p.way=D;
break;
}
}
}
return;
}
void win(){
int mintime=INT_MAX;
if(p.score==MAX){
system("cls");
color(14);
printf("YOU WIN!\n");
long long int end=time(NULL);
printf("Use time:%d(s)\n",end-_begin);
fstream read("time.txt",ios::in);
if(!read.fail())
read>>mintime;
read.close();
printf("Min time:%d(s)\n",mintime);
mintime=min<int>(mintime,end-_begin);
fstream out("time.txt",ios::out);
out<<mintime;
out.close();
while(kbhit()!=0)
getch();
getch();
getch();
exit(0);
}
return;
}
void gameover(){
if(p.die<=0){
system("cls");
color(7);
printf("Game over!\n");
printf("---FAIL---\n");
while(kbhit()!=0)
getch();
getch();
getch();
exit(0);
}
return;
}
void movej(){
for(unsigned int tc=0;tc<3;tc++){
int r=rand()%4;
switch(j[tc].way){
case U:{
if(cmap[j[tc].x-1][j[tc].y]!=1)
j[tc].x--;
else
j[tc].way=r;
break;
}
case D:{
if(cmap[j[tc].x+1][j[tc].y]!=1)
j[tc].x++;
else
j[tc].way=r;
break;
}
case L:{
if(cmap[j[tc].x][j[tc].y-1]!=1)
j[tc].y--;
else
j[tc].way=r;
break;
}
case R:{
if(cmap[j[tc].x][j[tc].y+1]!=1)
j[tc].y++;
else
j[tc].way=r;
break;
}
}
if(j[tc].x==9&&j[tc].y==9)
j[tc].way=U;
}
return;
}
void cjwj(){
for(unsigned int tc=0;tc<3;tc++){
color(14);
gotoxy(j[tc].x,j[tc].y);
if(cmap[j[tc].x][j[tc].y]==0)
printf("·");
else if(cmap[j[tc].x][j[tc].y]==2)
printf("▲");
else
printf(" ");
}
}
void check(){
for(unsigned int tc=0;tc<3;tc++){
if(j[tc].x==p.x&&j[tc].y==p.y){
if(p.time>0){
j[tc].x=j[tc].home.x;
j[tc].y=j[tc].home.y;
}else{
p.die--;
}
}
}
return;
}
void pt(){
color(10);
gotoxy(21,0);
for(int tc=0;tc<=40;tc++)
printf(" ");
if(p.time>0){
gotoxy(21,0);
for(unsigned int tc=0;tc<p.time;tc++)
printf("=");
}
return;
}
int main(){
init();
drawmap();
while(true){
drawj();
drawp();
eat();
pt();
win();
gameover();
_sleep(125);
getin();
cjwj();
cplayer();
movej();
check();
move();
check();
}
return 0;
}
吃豆人_代码
于 2023-05-01 14:00:59 首次发布