代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
基于控制台的RPG游戏实现
作者:
舞维天下
/ 发布于
2013/12/26
/
870
基于控制台设计的RPG游戏,键盘的WSAD(上下左右),当遇到NPC(屏幕上N代替)的时候可以对话并接收任务,找到一只猪(屏幕上Z代替)后游戏结束。 主要使用到了逻辑逻辑判断、循环、数据结构(结构体、数组)
#include <stdio.h> /* 为了支持颜色 */ #include <windows.h> // 地图区域 const int MAP_X_LENGTH = 24; const int MAP_Y_LENGTH = 18; /* 声明函数 */ void game(); int deriction_down(); int deriction_up(); int deriction_left(); int deriction_right(); void role_up(); void role_display(); void gameinfo(); int NPC(int npc); // 屏幕缓冲区 int display[18][24] = { {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}, {3,1,2,0,0,0,0,0,2,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,2,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,2,2,2,0,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,0,0,2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}, {3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3}, {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3} }; // 描述位置结构体 struct Position{ int x;// x int y;// y }; // 角色 struct Role{ struct Position oldP;// 旧位置 struct Position newP;// 新位置 void (* display)(); }; struct Role role = { {1, 1}, {1, 1} }; int main(){ role.display = &role_display; while(1){ game(); } return 0; } void gameinfo(){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN ); printf("=======================\n"); printf("=====欢迎来玩游戏======\n"); printf("=======================\n"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); } void game(){ role.display(); gameinfo(); // 遍历map中的数据 int i,j; for( i=0; i< MAP_Y_LENGTH ; i++){ for( j=0; j< MAP_X_LENGTH ; j++){ int temp = display[i][j]; if(temp == 1){ printf("¤"); }else if(temp == 2){ printf("∏"); }else if(temp == 3){ printf("■"); }else if(temp == 4){ printf("N "); }else if(temp == 5){// 猪 printf("Z "); }else{ printf(" "); } } printf("\n"); } char a = getch(); // 记录旧位置 role.oldP.y = role.newP.y; role.oldP.x = role.newP.x; switch(a){ case 's': // 向下 if(NPC(deriction_down()) == 0){ role.newP.y = role.newP.y + 1;// 向下走 } break; case 'd': // 向右 if(NPC(deriction_right()) == 0){ role.newP.x = role.newP.x + 1; } break; case 'a': // 向左 if(NPC(deriction_left()) == 0){ role.newP.x = role.newP.x - 1; } break; case 'w': // 向上 if(NPC(deriction_up()) == 0){ role.newP.y = role.newP.y - 1; } break; default: ; } system("cls"); } char npc_info[4][80] = { "我:你就是传说中的NPC?\n", "NPC:你这是看不起NPC嘛!!!\n", "我:不不不,我是来接任务的!\n", "NPC:我有一头猪不见了,你帮我找回来吧!\n", }; int NPC(int npc){ printf("%d",npc); if(npc == 4){//遇到NPC system("cls"); gameinfo(); int i = 0,j=0; for(j=0;j < 4; j++){ for(i=0; i < sizeof(npc_info[j]); i++){ printf("%c",npc_info[j][i]); _sleep(10); } printf("\n"); } printf("是否接收任务?\n"); printf("1. 是 2. 否\n"); char key = getchar(); if(key == '1'){ display[10][9] = 5; return 0; } else{ return 4; } return 4; }else if(npc == 5){//找到了猪 system("cls"); gameinfo(); printf("恭喜你,任务完成!\n"); system("pause"); return 0; } return npc; } /* 向下 */ int deriction_down(){ return display[role.newP.y + 1][role.newP.x]; } /* 向上 */ int deriction_up(){ return display[role.newP.y - 1][role.newP.x]; } /* 向左 */ int deriction_left(){ return display[role.newP.y][role.newP.x - 1]; } /* 向右 */ int deriction_right(){ return display[role.newP.y][role.newP.x + 1]; } /* * 角色向上走 */ void role_display(){ display[role.oldP.y][role.oldP.x] = 0; display[role.newP.y][role.newP.x] = 1; }
试试其它关键字
RPG游戏
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
舞维天下
贡献的其它代码
(
4
)
.
基于控制台的RPG游戏实现
.
Java NIO网络编程
.
android文件管理
.
微信公众平台API的Java通讯实现
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3