可以用C语言编写游戏吗

可以用C语言编写游戏吗,第1张

可以用C语言编写游戏的。

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。

//C语言写的,一个玩石头、剪刀、布的小游戏。下面是源码。
#include<stdioh>
#include<stdlibh>
int main()
{
char cq[][10]={"石头","剪刀","布"};
int guess=-1,r,youwin=0,mewin=0,daping=0,total=0;

srand(time(NULL));
while(1)
{
r=(int)((rand()/(RAND_MAX+10))3);
printf("0、石头\n1、剪刀\n2、布\n3、退出\n我已出,请你出:");
scanf("%d",&guess);
if(3==guess)
{
break;
}
else
{
total++;
printf("这一次你出的是%s,我出的是%s,所以",cq[guess],cq[r]);
if(0==guess&&1==r || 1==guess&&2==r || 2==guess&&0==r)
{
youwin++;
printf("你赢了!\n");
}
else if(guess==r)
{
daping++;
printf("我们打平了!\n");
}
else
{
mewin++;
printf("我赢了!\n");
}
}
}
printf("总共玩了%d次,你赢了%d次,我赢了%d次,打平%d次!\n",total,youwin,mewin,daping);
system("PAUSE");
return EXIT_SUCCESS;
}

能,绝对能,C语言是最早的高级语言之一,它是面向过程的语言,要实现一些功能要比比如C++,C#,JAVA等面向对象语言要困难。而且国内软件起步较晚,现在主流是JAVA、C#,基本上没C语言的市场,所以建议学好C语言,然后学习其他面向对象语言就简单了,这样才能找到一碗饭吃。

刚刚的有点问题,现在改好了:\x0d\#include\x0d\#include\x0d\#include\x0d\#include\x0d\char name[100],way[3][10]={"石头","剪子","布"},mode_name[2][20]={"三局两胜","五局三胜"};\x0d\void new_game(int mode,int p_win,int p_lose,int p_tide){\x0d\char cmd[10];\x0d\int computer,user,win=0,lose=0;\x0d\int cnt[2]={3,5};\x0d\printf("当前模式:\t%s\n",mode_name[mode]);\x0d\for(int i=0;i

//C语言实例:推箱子小游戏
#include <stdioh>  
#include <stdlibh>  
#include<stringh>  
#include <conioh>  
//行和列   
#define ROW 10  
#define COL 11  
/ run this program using the console pauser or add your own getch, system("pause") or input loop /  


 
/  
//地图  
char map[ROW][COL] = {  
"##########",//0  
"###     ##",//1  
"###     ##",//2  
"##AX  # ##",//3  
"###  ##   ",//4  
"#####    #",//5  
"##       #",//6  
"#     ####",//7  
"###       ",//8  
"##########" //9  
//A:人  , X:箱子   
} ;  
//打印地图   
void showMap();  
//接收小人的方向  
char enterDirection();  
//小人向上移动的方法  
void moveToUp();   
//小人向下移动的方法  
void moveToDown();   
//小人向右移动的方法  
void moveToRight();   
//小人向左移动的方法  
void moveToLeft();   
//当前小人的坐标  
int currentPersonRow = 3;  
int currentPersonCol = 2;  
//当前箱子的坐标   
int currentBoxRow = 3;  
int currentBoxCol = 3;  
int main(int argc, char argv[]) {  
//system("clear");  
printf("点击回车键开始游戏 ^_^\n\n");  
//1代表运行   0停止   
int flag = 1;  
while(flag==1){  
//显示地图   
showMap();  
//接收小人的方向  
char dir = enterDirection();  
switch(dir){  
//小人向上移动   
case 'w':  
case 'W':  
moveToUp();  
break;  
//小人向下移动   
case 's':  
case 'S':  
moveToDown();  
break;  
//小人向右移动   
case 'd':  
case 'D':  
moveToRight();  
break;  
//小人向左移动   
case 'a':  
case 'A':  
moveToLeft();  
break;  
//停止运行   
case 'q':  
case 'Q':  
printf("你的智商真低!T_T\n");  
flag = 0;  
break;  
}  
showMap();  
if(currentBoxRow==8&&currentBoxCol==9){  
printf("你的智商真高^_^!!!");  
flag = 0;   
}  
}  
}  

方法的实现   
/  
//打印地图   
void showMap(){  
int i;  
for(i = 0;i < ROW; i++){  
printf("%s\n",map[i]);  
}  
printf("\n\n\n\n\n");   
printf("W:上,S:下, A:左, D:右。Q:退出");  
printf("\n\n\n\n\n");  
}  
//接收小人的方向  
char enterDirection(){  
//清除SCANF中的缓冲区   
rewind(stdin);  
char dir;  
dir = getch();  
//scanf("%c",&dir);  
return dir;  
}  
//小人向上移动的方法  
void moveToUp(){  
//小人的下一个坐标   
int nextPersonCol = currentPersonCol;  
int nextPersonRow = currentPersonRow - 1;  
//箱子的下一个坐标  
int nextBoxRow = currentBoxRow - 1;  
int nextBoxCol = currentBoxCol;    
//如果小人的下一个坐标是路   
if(map[nextPersonRow][nextPersonCol]==' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
}  
//如果小人的下一个坐标是墙   
if(map[nextPersonRow][nextPersonCol]=='#'){  
//什么也不做   
}  
//如果小人的下一个坐标是箱子   
if(map[nextPersonRow][nextPersonCol]=='X'){  
if(map[nextBoxRow][nextBoxCol] == ' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
map[nextBoxRow][nextBoxCol] = 'X';  
map[currentBoxRow][currentBoxCol] = 'A';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
currentBoxRow = nextBoxRow;  
currentBoxCol = nextBoxCol;  
}  
}  
}  
//小人向下移动的方法  
void moveToDown(){  
//小人的下一个坐标   
int nextPersonCol = currentPersonCol;  
int nextPersonRow = currentPersonRow + 1;  
//箱子的下一个坐标  
int nextBoxRow = currentBoxRow + 1;  
int nextBoxCol = currentBoxCol;    
//如果小人的下一个坐标是路   
if(map[nextPersonRow][nextPersonCol]==' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
}  
//如果小人的下一个坐标是墙   
if(map[nextPersonRow][nextPersonCol]=='#'){  
//什么也不做   
}  
//如果小人的下一个坐标是箱子   
if(map[nextPersonRow][nextPersonCol]=='X'){  
if(map[nextBoxRow][nextBoxCol] == ' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
map[nextBoxRow][nextBoxCol] = 'X';  
map[currentBoxRow][currentBoxCol] = 'A';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
currentBoxRow = nextBoxRow;  
currentBoxCol = nextBoxCol;  
}  
}  
}   
//小人向右移动的方法  
void moveToRight(){  
//小人的下一个坐标   
int nextPersonCol = currentPersonCol + 1;  
int nextPersonRow = currentPersonRow;  
//箱子的下一个坐标  
int nextBoxRow = currentBoxRow;  
int nextBoxCol = currentBoxCol + 1;   
//如果小人的下一个坐标是路   
if(map[nextPersonRow][nextPersonCol]==' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
}  
//如果小人的下一个坐标是墙   
if(map[nextPersonRow][nextPersonCol]=='#'){  
//什么也不做   
}  
//如果小人的下一个坐标是箱子   
if(map[nextPersonRow][nextPersonCol]=='X'){  
if(map[nextBoxRow][nextBoxCol]==' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
map[nextBoxRow][nextBoxCol] = 'X';  
map[currentBoxRow][currentBoxCol] = 'A';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
currentBoxRow = nextBoxRow;  
currentBoxCol = nextBoxCol;  
}  
}  
}  
//小人向左移动的方法  
void moveToLeft(){  
//小人的下一个坐标   
int nextPersonCol = currentPersonCol - 1;  
int nextPersonRow = currentPersonRow;  
//箱子的下一个坐标  
int nextBoxRow = currentBoxRow;  
int nextBoxCol = currentBoxCol - 1;    
//如果小人的下一个坐标是路   
if(map[nextPersonRow][nextPersonCol]==' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
}  
//如果小人的下一个坐标是墙   
if(map[nextPersonRow][nextPersonCol]=='#'){  
//什么也不做   
}  
//如果小人的下一个坐标是箱子   
if(map[nextPersonRow][nextPersonCol]=='X'){  
if(map[nextBoxRow][nextBoxCol]==' '){  
map[nextPersonRow][nextPersonCol] = 'A';  
map[currentPersonRow][currentPersonCol] = ' ';  
map[nextBoxRow][nextBoxCol] = 'X';  
map[currentBoxRow][currentBoxCol] = 'A';  
currentPersonRow = nextPersonRow;  
currentPersonCol = nextPersonCol;  
currentBoxRow = nextBoxRow;  
currentBoxCol = nextBoxCol;  
}  
}  
}

我不是个做游戏的,但我是个玩游戏的。

C语言能用来做游戏吗 -> 能

我在网上看到了C语言的游戏源代码复制到VC++里执行都会报错不知道是怎么回事 -> 要具体问题具体分析,你可以看看都报什么错,针对性修改即可。

目前编程语言繁多,精通的话,绝大多数都能用来做游戏。推荐你先学好C,再根据之后的兴趣、发展、机会等等确定一门专攻语言。学会C,触类旁通,学其他语言就不是很费力了。

个人认为,编程语言只是一个次要方面(可能30%都不到),整个游戏的设计理念才是重点。你可能要用到文学、物理学、数学很多方面的知识,绝对不要死抠语言。比如一个游戏的人工智能,就是个很大的挑战。你可以自己看书,不必跟着老师走(只是跟着老师走会很惨),觉得要用什么就学什么,多用baidu、google。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13398021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-28
下一篇 2023-07-28

发表评论

登录后才能评论

评论列表(0条)

保存