網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了c語言實(shí)現(xiàn)可自定義的游戲地圖的具體代碼,供大家參考,具體內(nèi)容如下
博主相信每個人都有想做游戲的沖動,那么本文將給出一個用c語言制作的可自定義大小的游戲地圖(包含player,撞墻設(shè)置)。廢話不多說,直接給出全代碼!
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; }
注:游戲說明:先自定義游戲地圖的大小,然后自定義玩家生成在游戲棋盤中的哪個位置,最后通過數(shù)字鍵盤控制玩家的移動:下:2 ?上:8 ?左:4 ?右:6
2.運(yùn)行成果
大家可以自己在游戲地圖的基礎(chǔ)上設(shè)置不同的玩家,甚至可以進(jìn)行雙人對戰(zhàn)!
原文鏈接:https://blog.csdn.net/weixin_64524066/article/details/122386274
相關(guān)推薦
- 2022-08-03 C++單例設(shè)計(jì)模式詳細(xì)講解_C 語言
- 2022-07-11 EasyExcel實(shí)現(xiàn)追加寫入文件
- 2022-04-10 Blazor數(shù)據(jù)綁定用法_基礎(chǔ)應(yīng)用
- 2022-06-08 問題記錄:HttpServletRequestWrapper導(dǎo)致跨域失敗的問題
- 2021-12-14 HTML+jQuery實(shí)現(xiàn)簡單的登錄頁面_jquery
- 2023-12-11 IOC和AOP概念簡述
- 2022-12-04 Python?棧實(shí)現(xiàn)的幾種方式及優(yōu)劣詳解_python
- 2023-03-05 Python中ConfigParser模塊示例詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支