本文作者:Zhang, Xuheng 本文分类:C++小游戏 编程学习笔记 浏览:259
阅读时间:1974字, 约2-3.5分钟
更新~
自创迷宫小游戏:
#include<cstdio> #include<windows.h> #include<conio.h> int map[10][10]={{1,1,1,1,3,1,1,1,1,1}, {1,0,0,0,0,0,0,1,1,1}, {1,0,0,1,0,1,0,0,1,1}, {1,1,0,1,1,0,0,1,1,1}, {1,0,0,0,1,0,0,0,0,1}, {1,1,0,0,1,1,0,1,0,1}, {1,1,0,1,0,1,1,0,0,1}, {1,0,0,0,1,1,1,1,0,1}, {1,1,1,1,1,1,1,1,2,1}}; int pos_y=0;//人物的y坐标 int pos_x=4;//人物的x坐标 //打印地图 void jzmap() { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(map[i][j]==0) printf(" ");//可走的地方 if(map[i][j]==1) printf("■");//障碍 if(map[i][j]==2) printf("!!");//出口 if(map[i][j]==3) printf("* ");//人物所在的位置 } printf("\n"); } } //走动函数2 void z2(){ char c=getch(); //下 if(c=='s') { //找到人物所在的位置 if(map[pos_y][pos_x]==3&&map[pos_y+1][pos_x]!=1) { map[pos_y+1][pos_x]=3;//下面的位置变成人物所在的位置 map[pos_y][pos_x]=0;//走过的地方变成0 pos_y++; } } //上 if(c=='w') { if(map[pos_y][pos_x]==3&&map[pos_y-1][pos_x]!=1) { map[pos_y-1][pos_x]=3; map[pos_y][pos_x]=0; pos_y--; } } //左 if(c=='a') { if(map[pos_y][pos_x]==3&&map[pos_y][pos_x-1]!=1) { map[pos_y][pos_x-1]=3; map[pos_y][pos_x]=0; pos_x--; } } //右 if(c=='d') { if(map[pos_y][pos_x]==3&&map[pos_y][pos_x+1]!=1) { map[pos_y][pos_x+1]=3; map[pos_y][pos_x]=0; pos_x++; } } } //走动函数 void z(){ char c=getch(); int has_zou=0; //下 if(c=='s') { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { //找到人物所在的位置 if(map[i][j]==3&&map[i+1][j]!=1&&!has_zou) { has_zou=1; map[i+1][j]=3;//下面的位置变成人物所在的位置 map[i][j]=0;//走过的地方变成0 break; } } } } //上 if(c=='w') { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(map[i][j]==3&&map[i-1][j]!=1&&!has_zou) { has_zou=1; map[i-1][j]=3; map[i][j]=0; break; } } } } //左 if(c=='a') { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(map[i][j]==3&&map[i][j-1]!=1&&!has_zou) { has_zou=1; map[i][j-1]=3; map[i][j]=0; break; } } } } //右 if(c=='d') { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(map[i][j]==3&&map[i][j+1]!=1&&!has_zou) { has_zou=1; map[i][j+1]=3; map[i][j]=0; break; } } } } } void yx() { jzmap();//重绘地图 //z(); z2();//走操作 } //结束 bool js() { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(map[i][j]==map[9][8]) { return 1; } else{ return 0; } } } } int main() { for(int i=0;i<100;i++) { system("cls"); yx(); if(js()) { system("cls"); printf("game over!"); return 0; } } return 0; }
关于作者Zhang, Xuheng
- 还没有填写个人简介
- Email: hy23682@126.com
- 注册于: 2020-04-07 05:11:14
就很棒 加更冲冲冲