吃豆人_代码

#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;
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的人游戏的 Python 代码示例: ```python import pygame import random # 初始化 Pygame pygame.init() # 创建屏幕 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置游戏标题 pygame.display.set_caption("人游戏") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) GREEN = (0, 255, 0) # 定义人 pacman_img = pygame.image.load("pacman.png") pacman_rect = pacman_img.get_rect() pacman_rect.centerx = screen_width // 2 pacman_rect.centery = screen_height // 2 # 定义子 dot_img = pygame.image.load("dot.png") dots = [] for i in range(10): dot_rect = dot_img.get_rect() dot_rect.centerx = random.randint(0, screen_width) dot_rect.centery = random.randint(0, screen_height) dots.append(dot_rect) # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 清空屏幕 screen.fill(BLACK) # 绘制人 screen.blit(pacman_img, pacman_rect) # 绘制子 for dot in dots: screen.blit(dot_img, dot) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() ``` 这个代码创建了一个简单的人游戏,其中包含一个人和一些子。游戏循环不断绘制屏幕,处理事件,并更新屏幕。子的位置是随机生成的。请注意,此示例仅包含最基本的游戏逻辑,您可以根据需要添加更多功能和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值