C++一个吃豆人小游戏

代码如下

#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <cstring>
using namespace std;
const int n=809;
struct Point {int x,y;};
int dali;
int fx[4]={-1,27,1,-27};
int fxfx[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
int dis[1000][1000];
int changdi[30][27]={
{0,0,0,0,0,0,0,0,0,0,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,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,1,0,0,0,0,0,2,0,2,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,2,0,2,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,2,2,2,2,2,2,2,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,3,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,3,3,3,3,3,0,2,0,0,1,0,0,0,0,0,0},
{2,2,2,2,2,2,1,2,2,2,0,3,3,3,3,3,0,2,2,2,1,2,2,2,2,2,2},
{0,0,0,0,0,0,1,0,0,2,0,3,3,3,3,3,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,2,2,2,2,2,2,2,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int x,x1,x2,x3,x4,y,y1,y2,y3,y4;
int now,now1,now2,now3,now4;
int g1,g2,g3,g4;
int fangx,nextfx,last1,last2,last3,last4;
int fenshu,guozi,guaitimer;
int T1,T2,t1,t2,stopped, f=0;
int beichi;
void color(int a)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(int x,int y)
{
COORD pos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void begin(){
system("cls");
color(11); printf(" ★"); color(10); printf("吃豆人"); color(11); printf("★\n\n"); color(7);
printf(" 正在初始化,请耐心等待");
for (int i=0; i<=n; i++) for (int j=1; j<=n; j++) dis[i][j]=900;

for (int i=0; i<=n; i++){
for (int j=0; j<=3; j++){
if (i+fx[j]>=0 && i+fx[j]<=n){
int k=i+fx[j],xx=k/27,yy=k%27,kk;
if (changdi[i/27][i%27] && changdi[xx][yy]) dis[i][k]=kk=1;
}
}
}

for (int k=0; k<=n; k++)if(changdi[k]){
for (int i=0; i<=n; i++)if(changdi[i])
for (int j=0; j<=n; j++)if(changdi[j])
if (dis[i][j]>dis[i][k]+dis[k][j]) dis[i][j]=dis[i][k]+dis[k][j];
if (k%80==0){color (13); gotoxy(3,0); printf("│");}
if (k%80==20){color(13); gotoxy(3,0); printf("╱");}
if (k%80==40){color(13); gotoxy(3,0); printf("─");}
if (k%80==60){color(13); gotoxy(3,0); printf("╲");}
if (k%80==0){color (13); gotoxy(3,12); printf("│");}
if (k%80==20){color(13); gotoxy(3,12); printf("╱");}
if (k%80==40){color(13); gotoxy(3,12); printf("─");}
if (k%80==60){color(13); gotoxy(3,12); printf("╲");}
if (k%60==0){color(11); gotoxy(5,k/60); printf("●");}
}

}

void shuru(){
char ch=getch();
if (ch=='a') if (changdi[x+fxfx[0][0]][y+fxfx[0][1]]==1|changdi[x+fxfx[0][0]][y+fxfx[0][1]]==2) fangx=nextfx=0; else nextfx=0;
else if (ch=='s') if (changdi[x+fxfx[1][0]][y+fxfx[1][1]]==1|changdi[x+fxfx[1][0]][y+fxfx[1][1]]==2) fangx=nextfx=1; else nextfx=1;
else if (ch=='d') if (changdi[x+fxfx[2][0]][y+fxfx[2][1]]==1|changdi[x+fxfx[2][0]][y+fxfx[2][1]]==2) fangx=nextfx=2; else nextfx=2;
else if (ch=='w') if (changdi[x+fxfx[3][0]][y+fxfx[3][1]]==1|changdi[x+fxfx[3][0]][y+fxfx[3][1]]==2) fangx=nextfx=3; else nextfx=3;
else if (ch=='0') stopped=(stopped+1)%2;
else if (ch=='j') t1++;
else if (ch=='u') t2++;
else if ((ch=='k') && t1-1>0) t1--;
else if ((ch=='i') && t2-1>0) t2--;
else if (ch=='h') dali=(dali+1)%2;
}

void reset(){
system("cls"); color(7);
gotoxy(2,30); printf("控制方向:w/a/s/d");
gotoxy(4,30); printf("控制你的速度:j/k");
gotoxy(6,30); printf("控制怪物速度:u/i");
x=22; y=13;
x1=x2=x3=x4=14; y1=11; y2=12; y3=14; y4=15;
now=607; now1=389; now2=390; now3=392; now4=393;
for (int k=0; k<=n; k++){
int i=k/27,j=k%27;
gotoxy(i,j);
if (changdi[i][j]==1){color(7); printf("·");}
else if (!changdi[i][j]){color(1); printf("■");}
if (j=26){gotoxy(i,27); color(7); printf("%d",i);}
}
gotoxy(0,0);
gotoxy(x,y); color(14); printf("●");
gotoxy(x1,y1); color(4); printf("◆");
gotoxy(x2,y2); color(5); printf("◆");
gotoxy(x3,y3); color(3); printf("◆");
gotoxy(x4,y4); color(2); printf("◆");
fangx=0; T1=T2=guaitimer=0; t1=75; t2=100;stopped=0; fenshu=0; guozi=237; g1=g2=g3=g4=0; dali=0;
gotoxy(14,30); printf(" ");
}

void move1(){
int xx,yy;
xx=x+fxfx[nextfx][0]; yy=y+fxfx[nextfx][1];
if (changdi[xx][yy]){
if (changdi[xx][yy]==1){fenshu+=1; changdi[xx][yy]=2;}
color(14);
gotoxy(x,y); printf(" ");
gotoxy(xx,yy);
if(!dali)printf("●"); else printf("★");
now=x*27+y; x=xx; y=yy;
fangx=nextfx;
}
else{
if (x==13 && y==0 && fangx==0){xx=x; yy=26;}
else if (x==13 && y==26 && fangx==2){xx=x; yy=0;}
else{xx=x+fxfx[fangx][0]; yy=y+fxfx[fangx][1];}
if (changdi[xx][yy]){
if (changdi[xx][yy]==1){fenshu+=1; changdi[xx][yy]=2;}
color(14);
gotoxy(x,y); printf(" ");
gotoxy(xx,yy); if (!dali) printf("●"); else printf("★");
now=x*27+y; x=xx; y=yy;

}

}
color(7);
}

void move2(){
int haha,minhaha,xx,yy,chi=0;
if (g1){
minhaha=2147483647;
if (now1%27==0 | now1%27==26) haha=last1;
else if (!dali){
for (int i=0; i<=3; i++)
if (changdi[(now1+fx[i])/27][(now1+fx[i])%27] && i!=last1 && minhaha>dis[now1+fx[i]][now])
{minhaha=dis[now1+fx[i]][now]; haha=i;}
}
else{
minhaha=-minhaha;
for (int i=0; i<=3; i++)
if (changdi[(now1+fx[i])/27][(now1+fx[i])%27] && i!=last1 && minhaha<dis[now1+fx[i]][now])
{minhaha=dis[now1+fx[i]][now]; haha=i;}
}
xx=now1/27; yy=now1%27; gotoxy(xx,yy);
if (changdi[xx][yy]==1) printf("·");else printf(" ");
now1+=fx[haha]; last1=(haha+2)%4;
xx=now1/27; yy=now1%27; gotoxy(xx,yy); color(4); printf("◆"); color(7);
if (xx==x && yy==y){
if (!dali) chi+=1;
else {
guozi+=50;
fenshu+=50;
last1=0;
gotoxy(now1/27,now1%27);
if (changdi[now1/27][now1%27]==1) printf("·"); else printf(" ");
now1=389;
}
}
}
if (g2){
int k;
minhaha=2147483647;
if (fangx==0 | fangx==2){
k=y+(fxfx[fangx][1])*3;
while (k>25 | !changdi[x][k]) k--;
while (k<1 | !changdi[x][k]) k++;
} else{
k=x+(fxfx[fangx][0])*3;
while (k>28 | !changdi[k][y]) k--;
while (k<1 | !changdi[k][y]) k++;
}
if (fangx==0 | fangx==2) k=x*27+k; else k=k*27+y;

if (now2%27==0 | now2%27==26) haha=last2;
else if (!dali)
for (int i=0; i<=3; i++){
if (changdi[(now2+fx[i])/27][(now2+fx[i])%27] && i!=last2 && minhaha>dis[now2+fx[i]][k])
{minhaha=dis[now2+fx[i]][k]; haha=i;}
}
else{
minhaha=-minhaha;
for (int i=0; i<=3; i++){
if (changdi[(now2+fx[i])/27][(now2+fx[i])%27] && i!=last2 && minhaha<dis[now2+fx[i]][k])
{minhaha=dis[now2+fx[i]][k]; haha=i;}
}
}

xx=now2/27; yy=now2%27; gotoxy(xx,yy);
if (changdi[xx][yy]==1) printf("·");else printf(" ");
now2+=fx[haha]; last2=(haha+2)%4; gotoxy(18,30);
xx=now2/27; yy=now2%27; gotoxy(xx,yy); color(5); printf("◆"); color(7);
if (xx==x && yy==y){
if (!dali) chi+=1;
else {
guozi+=50;
fenshu+=50;
last2=0;
gotoxy(now2/27,now2%27);
if (changdi[now2/27][now2%27]==1) printf("·"); else printf(" ");
now2=390;
}
}
}
if (g3){
int k;
minhaha=2147483647;
if (fangx==0 | fangx==2){
k=y+(fxfx[(fangx+1)%4][1])*3;
while (k>25 | !changdi[x][k]) k--;
while (k<1 | !changdi[x][k]) k++;
} else{
k=x+(fxfx[(fangx+1)%4][0])*3;
while (k>28 | !changdi[k][y]) k--;
while (k<1 | !changdi[k][y]) k++;
}
if (fangx==0 | fangx==2) k=x*27+k; else k=k*27+y;

if (now3%27==0 | now3%27==26) haha=last3;
else if (!dali)
for (int i=0; i<=3; i++){
if (changdi[(now3+fx[i])/27][(now3+fx[i])%27] && i!=last3 && minhaha>dis[now3+fx[i]][k])
{minhaha=dis[now3+fx[i]][k]; haha=i;}
}
else {
minhaha=-minhaha;
for (int i=0; i<=3; i++){
if (changdi[(now3+fx[i])/27][(now3+fx[i])%27] && i!=last3 && minhaha<dis[now3+fx[i]][k])
{minhaha=dis[now3+fx[i]][k]; haha=i;}
}
}

xx=now3/27; yy=now3%27; gotoxy(xx,yy);
if (changdi[xx][yy]==1) printf("·");else printf(" ");
now3+=fx[haha]; last3=(haha+2)%4; gotoxy(18,30);
xx=now3/27; yy=now3%27;
gotoxy(xx,yy); color(3); printf("◆"); color(7);
if (xx==x && yy==y){
if (!dali) chi+=1;
else {
guozi+=50;
fenshu+=50;
last3=0;
gotoxy(now3/27,now3%27);
if (changdi[now3/27][now3%27]==1) printf("·"); else printf(" ");
now3=341;
}
}
}
if (chi) beichi++;
}

int main(){
SetConsoleTitle("吃豆人");
begin();
int jixu=1;
reset();

string bb[4]={"●","①","②","③"}; color(7);
gotoxy(12,12); printf("倒计时"); color(12);
for (int i=3; i>=0; i--){if (i==0) color(11);gotoxy(13,13); cout<<bb[i]; Sleep(1000);}
gotoxy(12,12); printf(" "); gotoxy(13,13); printf(" "); color(7);
while (!f){
Sleep(1);
gotoxy(10,30); printf("得分:%d ",fenshu);
gotoxy(8,30); printf("怪物速度:%d ",300-t2);
gotoxy(9,30); printf("你的速度:%d ",300-t1);
gotoxy(15,30); printf("被吃次数:%d ",beichi);
if (kbhit()) shuru();
if (stopped) continue;
T1=(T1+1)%t1; T2=(T2+1)%t2;
if (T1%t1==0 && now+fx[fangx]>0 && now+fx[fangx]<n) move1();
if (T2%t2==0){
if (guaitimer<=8){
if (guaitimer==0) g1=1;
if (guaitimer==8) g2=1;
guaitimer++;
}
if (!g3 && fenshu>=30) g3=1;
move2();
}
if (fenshu==guozi)f=2;
}
if (f=2) {
Sleep(3000);
system("cls");
printf("恭喜你吃完了!\n你一共被怪物吃掉了 %d 次",beichi);
Sleep(3000);
char ying;
scanf("%c",&ying);
}
}

C++一个吃豆人小游戏的更多相关文章

  1. 【C语言】用C语言输出一个吃豆人

    大圆盘减去扇形和小圆盘: #include <math.h> #include <stdio.h> int main() { double x, y; ; y >= -; ...

  2. [代码]解析nodejs的require,吃豆人的故事

    最近在项目中需要对nodejs的require关键字做解析,并且替换require里的路径.一开始我希望nodejs既然作为脚本语言,内核提供一个官方的parser库应该是一个稳定可靠又灵活的渠道,然 ...

  3. Unity项目 - 吃豆人Pacman

    项目展示 Github项目地址:Pacman 涉及知识 切片制作 Animations 状态机设置,any state切换,重写状态机 按键读取进行整数距离的刚体移动 用射线检测碰撞性 渲染顺序问题 ...

  4. Fzu2124 - 吃豆人 BFS

    Description 吃豆人是一款非常经典的游戏,游戏中玩家控制吃豆人在地图上吃光所有豆子,并且避免被怪物抓住. 这道题没有怪物,将游戏的画面分成n*m的格子,每格地形可能为空地或者障碍物,吃豆人可 ...

  5. FZU 2124 FOJ 2124 吃豆人【BFS】

     Problem 2124 吃豆人 Accept: 134    Submit: 575 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  6. 用python的turtle作图(二)动画吃豆人

    本文是用python的turtle作图的第二篇,通过这个例子可以了解动画的原理,用python自带的turtle库制作一些小动画. 1.问题描述 在上一篇"用python的turtle作图( ...

  7. JAVA课程设计——“小羊吃蓝莓”小游戏

    JAVA课程设计--"小羊吃蓝莓"小游戏 1. 团队课程设计博客链接 http://www.cnblogs.com/HXY071/p/7061216.html 2. 个人负责模块或 ...

  8. FZU 2124 吃豆人 bfs

    题目链接:吃豆人 比赛的时候写的bfs,纠结要不要有vis数组设置已被访问,没有的话死循环,有的话就不一定是最优解了.[此时先到的不一定就是时间最短的.]于是换dfs,WA. 赛后写了个炒鸡聪明的df ...

  9. css吃豆人动画

    一. Css吃豆人动画 1. 上半圆:两个div,内部一个圆div,外部设置宽高截取半圆 外部div动画:animation: 动画样式 1s(时长) ease(动画先低速后快速) infinite( ...

  10. TurnipBit开发板DIY呼吸的吃豆人教程实例

    转载请以链接形式注明文章来源(MicroPythonQQ技术交流群:157816561,公众号:MicroPython玩家汇) 0x00前言 吃豆人是耳熟能详的可爱形象,如今我们的TurnipBit也 ...

随机推荐

  1. React版/Vue版都齐了,开源一套【特别】的后台管理系统...

    本项目主要基于Elux+Antd构建,包含React版本和Vue版本,旨在提供给大家一个简单基础.开箱即用的后台管理系统通用模版,主要包含运行环境.脚手架.代码风格.基本Layout.状态管理.路由管 ...

  2. torch.sort 和 torch.argsort

    定义 torch.sort(input,dim,descending) torch.argsort(input,dim,descending) 用法 torch.sort:对输入数据排序,返回两个值, ...

  3. 【开源打印组件】vue-plugin-hiprint初体验

    vue-plugin-hiprint的学习与应用 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习 ...

  4. SQL 时间范围和时间粒度

    前言 使用 SQL 进行业务数据计算时,经常会遇到两个概念:时间范围 和 时间粒度 .以 最近一天的每小时的用户访问人数 为例: 最近一天 是时间范围 每小时 是时间粒度 常见的时间范围:最近五分钟. ...

  5. MySQL8更改数据存储目录

  6. 8. Ceph 基础篇 - 运维常用操作

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247485300&idx=1&sn=aacff9f7 ...

  7. 使用kuboard界面配置springcloud的其中一个模块设置环境变量,使用nacos配置地址等有关设置

    总结: 工作负载类型是StatefulSet的pod,不论其上层的service是nodeport还是Headless, 对外提供的地址格式是: <pod name>.<servic ...

  8. echarts pie饼状图绑定点击事件

    var valueData = [ {value: 33,name: '诊所'}, {value: 29,name: '汽车服务相关'}, {value: 27, name: '洗衣店'}, {val ...

  9. Linux make编译

    安装问题 linux编译流程 linux开发部分 一般来说著名的linux系统基本上分两大类: RedHat系列:Redhat.Centos.Fedora等 Debian系列:Debian.Ubunt ...

  10. MySQL用户也可以是个角色

    前言 角色(Role),可以认为是一些权限的集合,一直是存在各个数据库中,比如Oracle.SQL Server.OceanBase等,MySQL 自从 8.0 release 才引入角色这个概念. ...