C语言+EGE 推箱子小游戏
程序员文章站
2024-03-19 18:27:16
...
C语言+EGE实现推箱子小游戏
**下面展示一些 内联代码片
。
// main.cpp
main函数进入代码块
#include "head.h"
#include "jiemian.cpp"
#include "caozuo.cpp"
#include "drawmap.cpp"
#include "playgame.cpp"
#include "shezhi.cpp"
int main()
{
int x;
setinitmode(0);
initgraph(20*35,16*35);
loop:
a.OpenFile("1.mp3");
b.OpenFile("2.mp3");
c.OpenFile("3.wav");
jiemian();
Level = caozuo();
if(Level == 0)
{
return 0;
}
a.Close();
while(Level < 100)
{
changeMap(Level);
x = playgame();
switch(x)
{
case 0:
drawmap();
break;
case 1:
Level -= 2;
break;
case 2:
break;
case 3:
Level--;
break;
case 4:
goto loop;
break;
}
Level++;
if(Level<=0)
{
Level = 1;
}
if(Level >= 99)
{
Level = 99;
}
rememberlevel(Level);
}
getch();
return 0;
}
#include "head.h"
int caozuo()//开始的操作
{
mouse_msg msg = {0};
for(; is_run(); delay_fps(60))
{
jiemian();
msg.x = msg.y = 0;
while(mousemsg())
{
msg = getmouse();
}
if(msg.x>=8*35&&msg.x<=12*35&&msg.y>=5*35&&msg.y<=6*35)//如果点击开始游戏
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
return 1;
}
}
if(msg.x>=8*35&&msg.x<=12*35&&msg.y>=7*35&&msg.y<=8*35)//如果点击继续游戏
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
FILE* fp;
fp = fopen("level.txt","r");
fscanf(fp,"%d",&Level);
fclose(fp);
return Level;
}
}
if(msg.x>=8*35&&msg.x<=12*35&&msg.y>=9*35&&msg.y<=10*35)//如果点击退出游戏
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
return 0;
}
}
if(msg.x>=8*35&&msg.x<=12*35&&msg.y>=11*35&&msg.y<=12*35)//如果点击游戏说明
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
shuoming();
}
}
if(msg.x>=8*35&&msg.x<=12*35&&msg.y>=13*35&&msg.y<=14*35)//如果点击游戏设置
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
shezhi();
}
}
}
}
#include "head.h"
void drawmap()
{
int x,y,i;
num = 0;
PIMAGE img[9];
for(i = 0; i < 9; i++)
{
img[i] = newimage();
}
getimage(img[0],"images\\block.gif");
getimage(img[1],"images\\wall.png");
getimage(img[2],"images\\ball.png");
getimage(img[3],"images\\box.png");
getimage(img[4],"images\\down.png");
getimage(img[5],"images\\up.png");
getimage(img[6],"images\\right.png");
getimage(img[7],"images\\left.png");
getimage(img[8],"images\\box1.png");
for(y = 0; y < 16; y++)
{
for(x = 0; x < 16; x++)
{
putimage(x*35,y*35,img[0]);
if(Map[y-1][x] == 4 || Map[y-1][x] == 6)
{
if(ch == 1||ch == key_down)
{
putimage_withalpha(NULL,img[4],x*35-7.5,(y-1)*35-27);
}
if(ch == key_up)
{
putimage_withalpha(NULL,img[5],x*35-7.5,(y-1)*35-27);
}
if(ch == key_left)
{
putimage_withalpha(NULL,img[7],x*35-7.5,(y-1)*35-27);
}
if(ch == key_right)
{
putimage_withalpha(NULL,img[6],x*35-7.5,(y-1)*35-27);
}
px = x;
py = y - 1;
}
switch(Map[y][x])
{
case 1:
putimage_withalpha(NULL,img[1],x*35,y*35-11);
break;
case 2:
putimage_withalpha(NULL,img[2],x*35+2.5,y*35+2.5);
break;
case 3:
putimage_withalpha(NULL,img[3],x*35,y*35-10);
num++;
break;
case 5:
putimage_withalpha(NULL,img[8],x*35,y*35-10);
break;
}
}
}
char s[5];
setcolor(WHITE);
setbkmode(TRANSPARENT);
setfont(35,0,"ËÎÌå");
sprintf(s,"µÚ%02d¹Ø",Level);
outtextxy(13*35,0,s);
chose();
for(int j = 0; j < 8; j++)
{
delimage(img[j]);
}
}
#include "head.h"
void jiemian()
{
if(a.GetPlayStatus() == MUSIC_MODE_STOP)
{
if(X == 1)
{
a.Play(0);
}
}
PIMAGE img = newimage();
getimage(img,"images\\block.gif");
for(int i = 0; i < 20; i++)
{
for(int j = 0; j < 16; j++)
{
putimage(i*35,j*35,img);
}
}
delimage(img);
setcolor(BLUE);
setbkmode(TRANSPARENT);
setfont(70,0,"宋体",150,0,FW_HEAVY,0,0,0);
outtextxy(4*35,2*35,"推");
setfont(70,0,"宋体",0,0,FW_HEAVY,0,0,0);
outtextxy(9*35,1*35,"箱");
setfont(70,0,"宋体",-150,0,FW_HEAVY,0,0,0);
outtextxy(14*35+6,48,"子");
setcolor(BLACK);
setfont(35,0,"楷体");
outtextxy(8*35,5*35,"开始游戏");
outtextxy(8*35,7*35,"继续游戏");
outtextxy(8*35,11*35,"游戏说明");
outtextxy(8*35,13*35,"游戏设置");
outtextxy(8*35,9*35,"退出游戏");
}
void rememberlevel(int num)//记住关卡
{
FILE *fp;
fp = fopen("level.txt","w");
fprintf(fp,"%d",num);
fclose(fp);
}
void changeMap(int n)//地图的更换
{
int i,j;
step = 0;
char mapPath[10];
sprintf(mapPath, "Map\\%d.txt", n);
FILE* fp=fopen(mapPath,"r");
for(i = 0; i < 16; i++)
{
for(j = 0; j < 18; j++)
{
fscanf(fp,"%d",&Map[i][j]);
}
}
fclose(fp);
return ;
}
#include "head.h"
/*说明:0:代表草地 1:墙 2:球 3:箱子 4:主角*/
int playgame()
{
mouse_msg msg;
for(; is_run(); delay_fps(60))
{
drawmap();
if (b.GetPlayStatus() == MUSIC_MODE_STOP)
{
if(Y == 1)
{
b.Play(0);
}
}
msg.x = msg.y = 0;
while(mousemsg())
{
msg = getmouse();
}
if(msg.x>=16*35+6&&msg.x<=16*35+96&&msg.y>=6*35&&msg.y<=6*35+30)//如果点击上一关
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
return 1;
}
}
if(msg.x>=16*35+6&&msg.x<=16*35+96&&msg.y>=7*35&&msg.y<=7*35+30)//如果点击下一关
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
return 2;
}
}
if(msg.x>=16*35+6&&msg.x<=16*35+126&&msg.y>=8*35&&msg.y<=8*35+30)//如果点击重新开始
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
ch = 1;
return 3;
}
}
if(msg.x>=16*35+6&&msg.x<=16*35+66&&msg.y>=9*35&&msg.y<=9*35+30)//如果点击返回
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
return 4;
}
}
if(kbhit())//如果有键盘字符输入
{
ch = getch();
if(ch!=key_up&&ch!=key_left&&ch!=key_down&&ch!=key_right)
{
ch = 1;
continue;
}
step++;
if (c.GetPlayStatus() == MUSIC_MODE_STOP)
{
if(Z == 1)
{
c.Play(0);
}
}
switch(ch)
{
case key_up:
movestep(-1, 0);
break;
case key_left:
movestep(0, -1);
break;
case key_down:
movestep(1, 0);
break;
case key_right:
movestep(0, 1);
break;
}
if(num == 0)
return 0;
}
}
}
//0:空地
//4:人
//6:人和目的地的重合
//2:目的地
//3:箱子
//5:箱子和目的地的重合
void movestep(int ay,int ax)
{
//1、人的前面是空地
if(Map[py+ay][px+ax] == 0)
{
Map[py+ay][px+ax] = 4;//空地改为人
//原来人的位置还原
if(Map[py][px] == 6)
Map[py][px] = 2;
else
Map[py][px] = 0;
py += ay;
px += ax;
}
//2、人的前面是目的地
else if(Map[py+ay][px+ax] == 2)
{
Map[py+ay][px+ax] = 6;
//原来人的位置还原
if(Map[py][px] == 6)
{
Map[py][px] = 2;
}
else
{
Map[py][px] = 0;
}
py += ay;
px += ax;
}
//人的前面是箱子||人的前面是箱子和目的地的重合
else if(Map[py+ay][px+ax] == 3 || Map[py+ay][px+ax] == 5)
{
//a、箱子的前面是空地
if(Map[py+2*ay][px+2*ax] == 0)
{
Map[py+2*ay][px+2*ax] = 3;
if(Map[py+ay][px+ax] == 5)
{
Map[py+ay][px+ax] = 6;
num++;
}
else
{
Map[py+ay][px+ax] = 4;
}
//原来人的位置还原
if(Map[py][px] == 6)
{
Map[py][px] = 2;
}
else
{
Map[py][px] = 0;
}
}
//b、箱子的前面是目的地
else if(Map[py+2*ay][px+ax*2] == 2)
{
num--;
Map[py+2*ay][px+ax*2] = 5;
if(Map[py+ay][px+ax] == 5)
{
Map[py+ay][px+ax] = 6;
num++;
}
else
{
Map[py+ay][px+ax] = 4;
}
//原来人的位置还原
if(Map[py][px] == 6)
{
Map[py][px] = 2;
}
else
{
Map[py][px] = 0;
}
}
else
{
step--;
}
py += ay;
px += ax;
}
else
{
step--;
}
}
void chose()
{
PIMAGE a = newimage();
getimage(a,"images\\luse.jpg");
putimage(16*35,0,a);
char textContent[10];
setcolor(BLACK);
setbkmode(TRANSPARENT);
setfont(30, 0, "宋体");
sprintf(textContent, "步数:%d", step);
outtextxy(16*35+6,2*35, textContent);
outtextxy(16*35+6,6*35, "上一关");
outtextxy(16*35+6,7*35, "下一关");
outtextxy(16*35+6,8*35, "重新开始");
outtextxy(16*35+6,9*35, "返回");
delimage(a);
}
#include "head.h"
void shezhi()
{
PIMAGE img = newimage();
getimage(img,"images\\block.gif");
for(int i = 0; i < 20; i++)
for(int j = 0; j < 16; j++)
putimage(i*35,j*35,img);
delimage(img);
PIMAGE imgee = newimage();
getimage(imgee,"images\\1.jpg");
putimage(15*35,6*35,imgee);
putimage(15*35,8*35,imgee);
putimage(15*35,10*35,imgee);
delimage(imgee);
setcolor(BLUE);
setbkmode(TRANSPARENT);
setfont(55,0,"宋体",0,0,FW_HEAVY,0,0,0);
outtextxy(6*35,2*35,"游戏设置");
setcolor(BLACK);
setfont(35,0,"楷体");
outtextxy(4*35,6*35,"主题音乐");
outtextxy(4*35,8*35,"背景音乐");
outtextxy(4*35,10*35,"按键音");
setfont(35,0,"黑体");
if(X == 1)
{
setcolor(YELLOW);
outtextxy(14*35,6*35,"关");
}
else
{
setcolor(RED);
outtextxy(14*35,6*35,"开");
}
if(Y == 1)
{
setcolor(YELLOW);
outtextxy(14*35,8*35,"关");
}
else
{
setcolor(RED);
outtextxy(14*35,8*35,"开");
}
if(Z == 1)
{
setcolor(YELLOW);
outtextxy(14*35,10*35,"关");
}
else
{
setcolor(RED);
outtextxy(14*35,10*35,"开");
}
setcolor(BLACK);
setfont(35,0,"黑体");
outtextxy(16*35,13*35,"返回");
for(; is_run(); delay_fps(60))
{
mouse_msg msg = {0};
while(mousemsg())
msg = getmouse();
if(msg.x>=15*35&&msg.x<=16*35&&msg.y>=6*35&&msg.y<=7*35)//如果点击主题音乐的按键
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
PIMAGE img1 = newimage();
getimage(img1,"images\\block.gif");
putimage(14*35,6*35,img1);
delimage(img1);
if(X == 1)
{
setcolor(RED);
outtextxy(14*35,6*35,"开");
PIMAGE imgee = newimage();
getimage(imgee,"images\\2.jpg");
putimage(15*35,6*35,imgee);
delimage(imgee);
X = 0;
a.Stop();
}
else
{
setcolor(YELLOW);
outtextxy(14*35,6*35,"关");
PIMAGE imgee = newimage();
getimage(imgee,"images\\1.jpg");
putimage(15*35,6*35,imgee);
delimage(imgee);
X = 1;
if(a.GetPlayStatus() == MUSIC_MODE_STOP)
{
if(X == 1)
a.Play(0);
}
}
}
}
if(msg.x>=15*35&&msg.x<=16*35&&msg.y>=8*35&&msg.y<=9*35)
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
PIMAGE img1 = newimage();
getimage(img1,"images\\block.gif");
putimage(14*35,8*35,img1);
delimage(img1);
if(Y == 1)
{
setcolor(RED);
outtextxy(14*35,8*35,"开");
PIMAGE imgee = newimage();
getimage(imgee,"images\\2.jpg");
putimage(15*35,8*35,imgee);
delimage(imgee);
Y = 0;
}
else
{
setcolor(YELLOW);
outtextxy(14*35,8*35,"关");
PIMAGE imgee = newimage();
getimage(imgee,"images\\1.jpg");
putimage(15*35,8*35,imgee);
delimage(imgee);
Y = 1;
}
}
}
if(msg.x>=15*35&&msg.x<=16*35&&msg.y>=10*35&&msg.y<=11*35)
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
PIMAGE img1 = newimage();
getimage(img1,"images\\block.gif");
putimage(14*35,10*35,img1);
delimage(img1);
if(Z == 1)
{
setcolor(RED);
outtextxy(14*35,10*35,"开");
PIMAGE imgee = newimage();
getimage(imgee,"images\\2.jpg");
putimage(15*35,10*35,imgee);
delimage(imgee);
Z = 0;
}
else
{
setcolor(YELLOW);
outtextxy(14*35,10*35,"关");
PIMAGE imgee = newimage();
getimage(imgee,"images\\1.jpg");
putimage(15*35,10*35,imgee);
delimage(imgee);
Z = 1;
}
}
}
if(msg.x>=16*35&&msg.x<=18*35&&msg.y>=13*35&&msg.y<=14*35)
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
return ;
}
}
}
}
void shuoming()
{
PIMAGE img = newimage();
getimage(img,"images\\block.gif");
for(int i = 0; i < 20; i++)
for(int j = 0; j < 16; j++)
putimage(i*35,j*35,img);
delimage(img);
setcolor(BLUE);
setbkmode(TRANSPARENT);
setfont(55,0,"宋体",0,0,FW_HEAVY,0,0,0);
outtextxy(6*35,2*35,"游戏说明");
setcolor(BLACK);
setfont(35,0,"楷体");
outtextxy(4*35,5*35,"游戏有99个关卡,通过键盘");
outtextxy(3*35,6*35,"来控制人物移动。小人只能");
outtextxy(3*35,7*35,"将箱子往前推,不可向后拉,");
outtextxy(3*35,8*35,"并且小人一次只能推动一个");
outtextxy(3*35,9*35,"箱子。最后小人把箱子全部");
outtextxy(3*35,10*35,"推到小球的位置,即为过关。");
setcolor(BLACK);
setfont(35,0,"黑体");
outtextxy(16*35,13*35,"返回");
mouse_msg msg = {0};
for(; is_run(); delay_fps(60))
{
while(mousemsg())
msg = getmouse();
if(msg.x>=16*35&&msg.x<=18*35&&msg.y>=13*35&&msg.y<=14*35)
{
if(msg.is_left() == 1 && msg.is_up() == 1)
{
return ;
}
}
}
}
//head.h
#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
void jiemian();
void rememberlevel(int num);
int caozuo();
void changeMap(int n);
int playgame();
void drawmap();
void shuoming();
void shezhi();
void movestep(int ay,int ax);
void chose();
int Map[16][16] = {0};
int Level = 1;
int px,py;
int num;
int step = 0;
char ch = 1;
int X = 1,Y = 1,Z = 1;
MUSIC a,b,c;
#endif // HEAD_H_INCLUDED
上一篇: fcn数据集制作及训练自己的数据集
下一篇: 【C语言】推箱子小游戏