C语⾔实现吃⾖⼈⼩游戏(转载)
c语⾔实现吃⾖⼈⼩游戏(转载)
游戏内还有⼀些bug,凑活着来玩⼀玩吧!
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
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];//0:墙 1:有分的路 2:没分的路 3:怪物的家
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},
{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,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,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u, guozi, guaitimer;
int T1,T2, t1, t2, stopped;//T:计时 t1:玩家速度 t2:怪物速度
int f;//f:{0:继续 1:被吃 2:赢了 3:输了}
int beichi;
void color(int a){//颜⾊函数
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
void gotoxy(int x, int y){//位置函数(⾏为x 列为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,12);
printf("│");
}else if(k %80==20){
color(13);
gotoxy(3,12);
printf("╱");
}else if(k %80==40){
color(13);
gotoxy(3,12);
printf("─");
}else if(k %80==60){
}else 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 ==75){
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 ==80){
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 ==77){
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 ==72){
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 ==' '){
stopped =(stopped +1)%2;
}else if(ch =='-'){
t1++;
}else if((ch =='+')&& t1 -1>0){
t1--;
}
else if(ch =='$'){
dali =(dali +1)%2;
}
}
void reset(){
system("cls");
color(7);
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;
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);
c语言编程小游戏
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++;
changdi[xx][yy]=2;
}
color(14);
gotoxy(x, y);
printf("  ");
gotoxy(xx, yy);
if(!dali){
printf("♀");
}else{
printf("☆");
}
now = x *27+ y;
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++;
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;//相当于INT_MAX
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("·");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。