c语言实现可自定义的游戏地图
作者:Wccyy883 时间:2021-10-25 14:38:34
本文实例为大家分享了c语言实现可自定义的游戏地图的具体代码,供大家参考,具体内容如下
博主相信每个人都有想做游戏的冲动,那么本文将给出一个用c语言制作的可自定义大小的游戏地图(包含player,撞墙设置)。废话不多说,直接给出全代码!
1.完整代码
#include<stdio.h>
#include<windows.h>
#include<conio.h>
//this is the add function
int drawroom(int length,int height,int x,int y);
int transform(int tran);
//begin the function
int main()
{
int p=10;
int q=10;
printf("control player to move:up“8” down“2” left“4” right“6”\n");//control the player
int y,x;
int ret=1;
int a,b;
printf("请输入地图的大小,例如:10 10:\n");
scanf("%d %d",&p,&q);
printf("请输入玩家的位置:“例如:3 5”\n"); //get the first player place
scanf("%d %d",&y,&x);
drawroom(p,q,y,x);
printf("\n");
while(ret!=0){
printf("\n");
int i;
i=0;
int tran;
tran=getch();
i=transform(tran);
if(i==8){
drawroom(p,q,y-1,x);
y=y-1;
}
else if(i==2){
drawroom(p,q,y+1,x);
y=y+1;
}
else if(i==4){
drawroom(p,q,y,x-1);
x=x-1;
}
else if(i==6){
drawroom(p,q,y,x+1);
x=x+1;
}
if(y==0 || x==0){
ret=0;
printf("\n\nyou touch the board!game over!!");
break;
}
if(y==p+1 || x==q+1){
ret=0;
printf("\n\nyou touch the board!game over!!");
break;
}
}
getchar();
system("pause");
return 0;
}
int drawroom(int length,int height,int y,int x)
{ int ret=1;
int size1=height+2;
int size2=length+2;
int a[size1][size2];
int i,j;
int count=1;
for(i=0;i<size1;i++){
for(j=0;j<size2;j++){
a[i][j]=0;
}
}
a[0][0]=1;
a[0][size2-1]=1;
a[size1-1][0]=1;
a[size1-1][size2-1]=1;
for(j=1;j<size2-1;j++){
a[0][j]=2;
}
for(j=1;j<size2-1;j++){
a[size1-1][j]=2;
}
for(i=1;i<size1-1;i++){
a[i][0]=3;
}
for(i=1;i<size1-1;i++){
a[i][size2-1]=3;
}
a[y][x]=4;
for(i=0;i<size1;i++){
printf("\n");
for(j=0;j<size2;j++){
if(a[i][j]==1){
printf("+ ");
}else if(a[i][j]==2){
printf("- ");
}else if(a[i][j]==3){
printf("| ");
}else if(a[i][j]==4){
printf("@ ");
}else{
printf(". ");
}
}
}
printf("\n\n\n\n\n\n\n\n\n\n");
return 0;
}
int transform(int tran)
{
int tot;
if(tran==52)tot=4;
if(tran==54)tot=6;
if(tran==50)tot=2;
if(tran==56)tot=8;
return tot;
}
注:游戏说明:先自定义游戏地图的大小,然后自定义玩家生成在游戏棋盘中的哪个位置,最后通过数字键盘控制玩家的移动:下:2 上:8 左:4 右:6
2.运行成果
大家可以自己在游戏地图的基础上设置不同的玩家,甚至可以进行双人对战!
来源:https://blog.csdn.net/weixin_64524066/article/details/122386274
标签:c语言,游戏地图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java基础之Object类
2022-11-17 21:20:10
GSON实现Java对象与JSON格式对象相互转换的完全教程
2023-11-23 09:23:37
Android自定义轮播图效果
2022-10-27 06:31:26
springBoot整合rabbitMQ的方法详解
2022-08-19 02:28:33
![](https://img.aspxhome.com/file/2023/8/70948_0s.png)
浅谈Java代码的 微信长链转短链接口使用 post 请求封装Json(实例)
2023-07-27 19:36:09
Android开发之android_gps定位服务简单实现
2023-07-31 20:02:25
![](https://img.aspxhome.com/file/2023/3/83553_0s.gif)
Maven配置文件pom.xml详解
2022-07-03 02:26:43
java程序员如何编写更好的单元测试的7个技巧
2023-09-05 14:57:24
C#使用LINQ查询操作符实例代码(一)
2021-11-27 01:41:57
![](https://img.aspxhome.com/file/2023/8/77258_0s.png)
详解spring-boot actuator(监控)配置和使用
2022-07-12 17:20:37
Spring-Data-JPA整合MySQL和配置的方法
2023-10-29 10:19:41
![](https://img.aspxhome.com/file/2023/4/58734_0s.png)
java Semaphore共享锁实现原理解析
2021-11-02 23:12:38
![](https://img.aspxhome.com/file/2023/5/61815_0s.jpg)
Java浅析枚举类的使用
2023-11-10 14:12:58
![](https://img.aspxhome.com/file/2023/6/104696_0s.png)
java实现文件的断点续传
2023-11-23 09:11:00
![](https://img.aspxhome.com/file/2023/4/59544_0s.jpg)
java制作简单验证码功能
2022-12-07 02:12:46
![](https://img.aspxhome.com/file/2023/5/60835_0s.jpg)
使用Logback日志保存到相对路径的操作
2021-10-11 16:42:30
C#集合之队列的用法
2022-11-24 16:15:38
![](https://img.aspxhome.com/file/2023/4/108794_0s.jpg)
使用自定义注解实现redisson分布式锁
2021-09-30 18:31:22
Java矩阵连乘问题(动态规划)算法实例分析
2022-05-04 21:50:54
![](https://img.aspxhome.com/file/2023/8/65598_0s.jpg)
浅析C#中结构与类的区别
2023-09-12 23:41:39