想法:设计一个迷宫,走出迷宫,获得奖励。
思路:
1.首先要有个游戏开始界面,主要就是显示一些东西。
printf("\n\t\t\t");
for(int i = 0;i < 20;i ++){
printf("-"); //输出界面上界
}
printf("\n");
printf("The * is the symbol of you.\n"); //*表示你所处的位置
printf("You can press (up,down,left,right) to move the (*).\n"); //你可以按上、下、左右键控制移动
printf("If your keyboard doesn't have these buttons,you can also press (w,s,a,d) to move the (*).\n"); //也可以按w/s/a/d控制移动
printf("Please press Enter to enter the maze.\n"); //请按回车键进入游戏界面
getch(); //don't show in the screen,不回显
system("cls"); //清屏
2.画迷宫。4表示*,输出*;0表示道路,输出空格;1表示墙,输出#。在这之前要对迷宫数组进行初始化,这里我用的迷宫数组是参照的网上的一个。
int maze[20][20] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{4,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
{1,1,1,0,0,1,1,1,0,0,1,0,0,0,0,0,1,0,0,1},
{1,1,1,0,0,1,1,1,0,0,1,1,1,1,0,0,1,0,0,1},
{1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,1,1,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,1,1,1},
{1,0,0,0,0,0,0,1,1,0,0,1,1,1,1,0,0,1,1,1},
{1,1,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,1,0,0,1},
{1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,1},
{1,0,0,1,1,1,1,1,0,1,1,1,1,0,0,0,0,0,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,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}};
把迷宫打印出来:
//画迷宫
void draw(int maze[20][20]){
int i,j;
for(i = 0;i < 20;i ++){
printf("\t\t\t"); //display int the center of the screen
for(j = 0;j < 20;j ++){
if(maze[i][j] == 4) printf("*"); //4---you
else if(maze[i][j] == 0) printf(" "); //0---road
else printf("#"); //1---wall
}
printf("\n");
}
}
3.移动。
初始化a和b,a表示*所在的行坐标,b表示*所在的列坐标。用了do while循环。每次用getch()读入一个按键,然后判断*当前位置的上下左右四个位置,如果不是1,表示不是墙,有路可以走,记得要break。这里有一个注意点,就是判断完一个if之后,如果不符合条件,是不会break的,因为用的是switch,所以如果对应到case,不符合条件,会跳到下一个if,所以会出现“下边是墙,然后你按→键会神奇地出现往左移的情况”。为了避免这种情况,所以要在if外面再加个break,这样就好啦。每移动一次,就用cls清一下屏。最后*如果出现在迷宫出口,就表示成功,退出并且输出“儿童节快乐”。
//移动
void move(int maze[20][20]){
int a = 1; //row
int b = 0; //col
char num;
do{
num = getch();
//only change four locations of *
switch(num){
case 72: //↑的ASCII
case 87: //大写W的ASCII
case 119: //小写w的ASCII
if(maze[a-1][b] != 1){ //up
swap(maze[a][b],maze[a-1][b]);
a --;
system("cls"); //clear the screen
draw(maze);
break;
}
break;
case 80:
case 83:
case 115:
if(maze[a+1][b] != 1){ //down
swap(maze[a+1][b],maze[a][b]);
a ++;
system("cls");
draw(maze);
break;
}
break;
case 75:
case 65:
case 97:
if(maze[a][b-1] != 1){ //left
swap(maze[a][b-1],maze[a][b]);
b --;
system("cls");
draw(maze);
break;
}
break;
case 77:
case 68:
case 100:
if(maze[a][b+1] != 1){ //right
swap(maze[a][b+1],maze[a][b]);
b ++;
system("cls");
draw(maze);
break;
}
break;
default:break;
}
if(maze[18][19] == 4){
printf("\n\t\t Happy Children's Day to You!\n\n\n");
Sleep(2000); //delay
break;
}
}while(1);
}
4.这里我还美化了一下程序,改了界面的字体颜色,插入了一个BGM。
改字体:我看的百度经验用了一行代码
system("color 0D"); //color
插入BGM:BGM要和exe文件放在同一个文件夹里,另外,PlaySound函数只支持wav,我用mp3试了半天没成功。
PlaySound("1.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
5.最后两个注意点:
一:编译的时候,要进行链接,因为用到了winmm文件,所以要链接。打开菜单栏“项目”选项,选择“构建选项”,进入界面,选择链接器设置,添加winmm进行链接。
二:本着送给同学作为儿童节礼物,结果把exe和BGM,还有使用说明压缩打包发给同学,反映打不开,说缺少dll文件(有些同学不学编程,电脑里没编程软件)。
上网查了一下,说要进行静态链接,-static-libgcc。但是我用Code Blocks进行链接器设置的时候,一直没成功,不知道为啥。觉得要凉了。突然灵机一动,把dll文件也发给他们不就好了嘛。于是加进打包文件里,果然成功了。
完整程序:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h> //get the ASCII of up, down, left and right
#include<windows.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib") //PlaySound()
#include<time.h>
#include<algorithm>
using namespace std;
//画迷宫
void draw(int maze[20][20]){
int i,j;
for(i = 0;i < 20;i ++){
printf("\t\t\t"); //display int the center of the screen
for(j = 0;j < 20;j ++){
if(maze[i][j] == 4) printf("*"); //4---you
else if(maze[i][j] == 0) printf(" "); //0---road
else printf("#"); //1---wall
}
printf("\n");
}
}
//移动
void move(int maze[20][20]){
int a = 1; //row
int b = 0; //col
char num;
do{
num = getch();
//only change four locations of *
switch(num){
case 72: //↑的ASCII
case 87: //大写W的ASCII
case 119: //小写w的ASCII
if(maze[a-1][b] != 1){ //up
swap(maze[a][b],maze[a-1][b]);
a --;
system("cls"); //clear the screen
draw(maze);
break;
}
break;
case 80:
case 83:
case 115:
if(maze[a+1][b] != 1){ //down
swap(maze[a+1][b],maze[a][b]);
a ++;
system("cls");
draw(maze);
break;
}
break;
case 75:
case 65:
case 97:
if(maze[a][b-1] != 1){ //left
swap(maze[a][b-1],maze[a][b]);
b --;
system("cls");
draw(maze);
break;
}
break;
case 77:
case 68:
case 100:
if(maze[a][b+1] != 1){ //right
swap(maze[a][b+1],maze[a][b]);
b ++;
system("cls");
draw(maze);
break;
}
break;
default:break;
}
if(maze[18][19] == 4){
printf("\n\t\t Happy Children's Day to You!\n\n\n");
Sleep(2000); //delay
break;
}
}while(1);
}
int main(){
system("color 0D"); //color
//system("pause");
PlaySound("1.wav",NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
//mciSendString("open D:\\Code\\一时兴起\\1.wav",NULL,0,NULL); //这种方法没有成功
int maze[20][20] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{4,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1},
{1,1,1,0,0,1,1,1,0,0,1,0,0,0,0,0,1,0,0,1},
{1,1,1,0,0,1,1,1,0,0,1,1,1,1,0,0,1,0,0,1},
{1,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,1,1,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1},
{1,0,0,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,1,1,1},
{1,0,0,0,0,0,0,1,1,0,0,1,1,1,1,0,0,1,1,1},
{1,1,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,1,0,0,1},
{1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,1},
{1,0,0,1,1,1,1,1,0,1,1,1,1,0,0,0,0,0,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1},
{1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,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}};
printf("\n\t\t\t");
for(int i = 0;i < 20;i ++){
printf("-"); //输出界面上界
}
printf("\n");
printf("The * is the symbol of you.\n"); //*表示你所处的位置
printf("You can press (up,down,left,right) to move the (*).\n"); //你可以按上、下、左右键控制移动
printf("If your keyboard doesn't have these buttons,you can also press (w,s,a,d) to move the (*).\n"); //也可以按w/s/a/d控制移动
printf("Please press Enter to enter the maze.\n"); //请按回车键进入游戏界面
getch(); //don't show in the screen,不回显
system("cls"); //清屏
draw(maze);
move(maze);
return 0;
}
效果展示:
版权声明:本文为博主原创文章,未经博主允许不得转载。