本文实例为大家分享了c语言实现可自定义的游戏地图的具体代码,供大家参考,具体内容如下
博主相信每个人都有想做游戏的冲动,那么本文将给出一个用c语言制作的可自定义大小的游戏地图(包含player,撞墙设置)。废话不多说,直接给出全代码!
1.完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#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