c语言简单游戏代码大全直接复制(c语言代码怎么复制)

http://www.itjxue.com  2023-02-12 16:27  来源:未知  点击次数: 

用C语言编写的小游戏代码是什么?

“猜数字小游戏”,每个数字后按空格,最后按回车确认

#includestdio.h

#includestdlib.h

#includetime.h

int a[4],b[4];

int count=0;? //计算猜测次数

void csh( );? //初始化

void start( );? //开始游戏

int main( )

{ csh( );

start( );

}

void csh( )? //初始化

{ printf("\n\n???????? 猜? 数? 字? 小? 游? 戏\n\n");

printf(“? ??猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.\n”);

}

void start( )? //开始游戏

{int m,n;? //m是完全猜对的个数,n是顺序不对的个数

while(1)

{srand((unsigned)time(NULL));? //初始化随机数发生器srand( )

while(1) { for(int i=0;i4;i++) a[i]=rand( )%10; ?//rand( )函数每次随机产生一个0-9的数

if( (a[3]!=a[2]a[3]!=a[1]a[3]!=a[0])

(a[2]!=a[1]a[2]!=a[0])a[1]!=a[0] ) break; }? //4个随机数各自不相等

printf("?? ?请依次输入4个一位整数:\n\n? ?");

while(1)

{for(int i=0;i4;i++) scanf(“%d”,b[i]);

printf("?? ?你输入的是:%d? %d? %d? %d ",b[0],b[1],b[2],b[3]);

m=0;n=0;

for(int i=0;i4;i++)

{for(int j=0;j4;j++)

{ if(b[i]==a[j]i==j)m=m+1; if(b[i]==a[j]i!=j)n=n+1; }

}

count=count+1;

printf("????? %dA? %dB?? 你试了%d次\n? ?",m,n,count);

if(m==4)break;

if(count==8){ count=0; break; }

}

printf("\n");

if(m==4)printf("???? 你猜对了(^-^)! 就是:%d %d %d %d\n",a[0],a[1],a[2],a[3]);

else printf("???? 你输了(T-T)!哈哈!应该是:%d %d %d %d\n",a[0],a[1],a[2],a[3]);

int z;

printf("???? (要继续吗?1或0)\n? ?");

scanf(“%d”,z);

if(z==0) break;

}

}

谁有一些简单小游戏的C语言程序?

可以学写“贪吃蛇”代码:

#include stdio.h

#include stdlib.h

#include conio.h

#include time.h

#include Windows.h

#define W 78? //游戏框的宽,x轴

#define H 26? //游戏框的高,y轴

int dir=3; ???//方向变量,初值3表示向“左”

int Flag=0; ??//吃了食物的标志(1是0否)

int score=0;? //玩家得分

struct food{ int x;? //食物的x坐标

? ? ? ? ? ? ? ?? int y;? //食物的y坐标

? ? ? ? ? ? ?? }fod; ?//结构体fod有2个成员

struct snake{ int len;? //蛇身长度

? ? ? ? ? ? ? ? ?? int speed; //速度

? ? ? ? ? ? ? ? ? int x[100];

? ? ? ? ? ? ? ? ? int y[100];

? ? ? ? ? ? ? ?? }snk;? //结构体snk有4个成员

void gtxy( int x,int y) ?//控制光标移动的函数

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y);? //以下声明要用到的几个自编函数

void csh( );? //初始化界面

void keymove( ); //按键操作移动蛇

void putFod( );? //投放食物

int? Over( ); ??//游戏结束(1是0否)

void setColor(unsigned short p, unsigned short q); //设定显示颜色

int main( )?? //主函数

{ csh( );

? while(1)

? ?? { Sleep(snk.speed);

? ? ?? keymove( );

? ? ?? putFod( );

? ? ?? if(Over( )) {system(“cls”);

? ? ? ? ? ? ? ? ? ? ? gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);

? ? ? ? ? ? ? ? ? ? ? gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);

? ? ? ? ? ? ? ? ? ? ? getch( );? break;

? ? ? ? ? ? ? ? ? ?? }

? ?? }

? return 0;

}

void csh( )? //初始化界面

{ int i;? gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0};? //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),cursor_info);

for(i=0;i=W;i=i+2)? //横坐标要为偶数,因为这个要打印的字符占2个位置

{ setColor(2, 0);? //设定打印颜色为绿字黑底

? gtxy(i,0); ?printf("■"); ?//打印上边框

? gtxy(i,H); printf("■"); ?//打印下边框

}

for(i=1;iH;i++)

{ gtxy(0,i); printf("■"); ?//打印左边框

gtxy(W,i); printf("■"); ?//打印右边框

}

while(1)

??{ srand((unsigned)time(NULL));? //启动随机数发生器srand( )

fod.x=rand()%(W-4)+2;? //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

fod.y=rand()%(H-2)+1;? //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

if (fod.x%2==0) break;? //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

setColor(12, 0);? //设定打印颜色为淡红字黑底

gtxy(fod.x,fod.y); printf("●"); ?//到食物坐标处打印初试食物

snk.len=3; ?????//蛇身长

snk.speed=350;? //刷新蛇的时间,即是移动速度

snk.x[0]=W/2+1;? //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2;?? ?//蛇头纵坐标

setColor(9, 0);? //设定打印颜色为淡蓝字黑底

gtxy(snk.x[0], snk.y[0]); ?printf("■"); ?//打印蛇头

for(i=1;isnk.len;i++)

?{ snk.x[i]=snk.x[i-1]+2; ?snk.y[i]=snk.y[i-1];

??gtxy(snk.x[i],snk.y[i]); ?printf("■"); ?//打印蛇身

}

setColor(7, 0);? //恢复默认的白字黑底

return;

}

void keymove( )? //按键操作移动蛇

{ int key;

if( kbhit( ) )??? //如有按键输入才执行下面操作

{ key=getch( );

? ? if (key==224)? //值为224表示按下了方向键,下面要再次获取键值

? ? { key=getch( );

? ? ? if(key==72dir!=2)dir=1;? //72表示按下了向上方向键

? ?? if(key==80dir!=1)dir=2;? //80为向下

? ?? if(key==75dir!=4)dir=3;? //75为向左

? ?? if(key==77dir!=3)dir=4;? //77为向右

?? }

if (key==32)

? { while(1) if((key=getch( ))==32) break; }? //32为空格键,这儿用来暂停

? }

if (Flag==0)? //如没吃食物,才执行下面操作擦掉蛇尾

?{gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); ?printf("? "); }

int i;

for (i = snk.len - 1; i 0; i--) ?//从蛇尾起每节存储前一节坐标值(蛇头除外)

? ? ? { snk.x[i]=snk.x[i-1]; ?snk.y[i]=snk.y[i-1]; }

switch (dir)? //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--; break; ??//dir=1要向上移动

? case 2: snk.y[0]++; break;? //dir=2要向下移动

? case 3: snk.x[0]-=2; break;? //dir=3要向左移动

? case 4: snk.x[0]+=2; break;? //dir=4要向右移动

}

setColor(9, 0);

gtxy(snk.x[0], snk.y[0]); printf("■"); ?//打印蛇头

if (snk.x[0] == fod.x snk.y[0] == fod.y)? //如吃到食物则执行以下操作

??{ printf("\007"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; }//这007是响铃

else Flag = 0;? //没吃到食物Flag的值为0

if(snk.speed150) snk.speed= snk.speed+5;? //作弊码,不让速度无限加快

}

void putFod( )? //投放食物

{ if (Flag == 1)? //如吃到食物才执行以下操作,生成另一个食物

??{ while (1)

??{ int i,n= 1;

???srand((unsigned)time(NULL));? //启动随机数发生器srand( ),接下产生食物坐标值

? ?fod.x = rand( ) % (W - 4) + 2;? //产生在游戏框范围内的一个x坐标值

? ?fod.y = rand( ) % (H - 2) + 1;? //产生在游戏框范围内的一个y坐标值

? ?for (i = 0; i snk.len; i++) ??//随机生成的食物不能在蛇的身体上

? ? ? ? ? ? ? { if (fod.x == snk.x[i] fod.y == snk.y[i]) { n= 0; break;} }

???if (n fod.x % 2 == 0) break; ?//n不为0且横坐标为偶数,则食物坐标取值成功

??}

? ? setColor(12, 0);

?? gtxy(fod.x, fod.y); ?printf("●"); ?//光标到取得的坐标处打印食物

? }

return;

}

int Over( )? //判断游戏是否结束的函数

{ int ?i;

setColor(7, 0);

gtxy(2,H+1); printf(“暂停键:space.”); ?//以下打印一些其它信息

gtxy(2,H+2); printf(“游戏得分:%d”,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1;? //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1;? //蛇头触碰上下边界

for (i = 1; i snk.len; i++)

{ if (snk.x[0] == snk.x[i] snk.y[0] == snk.y[i]) return 1; } //蛇头触碰自身

return 0;? //没碰到边界及自身时就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);

} //用来设定颜色的函数

求几C语言个小游戏代码,简单的,要注释、、谢谢了、

// Calcu24.cpp : Defines the entry point for the console application.

//

/*

6-6

24点游戏

*/

#include "conio.h"

#include "stdlib.h"

#include "time.h"

#include "math.h"

#include "string.h"/*

从一副扑克牌中,任取4张。

2-10 按其点数计算(为了表示方便10用T表示),J,Q,K,A 统一按 1 计算

要求通过加减乘除四则运算得到数字 24。

本程序可以随机抽取纸牌,并用试探法求解。

*/void GivePuzzle(char* buf)

{

char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}; for(int i=0; i4; i++){

buf[i] = card[rand() % 13];

}

}

void shuffle(char * buf)

{

for(int i=0; i5; i++){

int k = rand() % 4;

char t = buf[k];

buf[k] = buf[0];

buf[0] = t;

}

}

int GetCardValue(int c)

{

if(c=='T') return 10;

if(c='0' c='9') return c - '0';

return 1;

}

char GetOper(int n)

{

switch(n)

{

case 0:

return '+';

case 1:

return '-';

case 2:

return '*';

case 3:

return '/';

} return ' ';

}double MyCalcu(double op1, double op2, int oper)

{

switch(oper)

{

case 0:

return op1 + op2;

case 1:

return op1 - op2;

case 2:

return op1 * op2;

case 3:

if(fabs(op2)0.0001)

return op1 / op2;

else

return 100000;

} return 0;

}

void MakeAnswer(char* answer, int type, char* question, int* oper)

{

char p[4][3];

for(int i=0; i4; i++)

{

if( question[i] == 'T' )

strcpy(p[i], "10");

else

sprintf(p[i], "%c", question[i]);

}

switch(type)

{

case 0:

sprintf(answer, "%s %c (%s %c (%s %c %s))",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);

break;

case 1:

sprintf(answer, "%s %c ((%s %c %s) %c %s)",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);

break;

case 2:

sprintf(answer, "(%s %c %s) %c (%s %c %s)",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);

break;

case 3:

sprintf(answer, "((%s %c %s) %c %s) %c %s",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);

break;

case 4:

sprintf(answer, "(%s %c (%s %c %s)) %c %s",

p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);

break;

}

}

bool TestResolve(char* question, int* oper, char* answer)

{

// 等待考生完成

int type[5]={0,1,2,3,4};//计算类型

double p[4];

double sum=0;

//

for(int i=0; i4; i++) //循环取得点数

{

p[i]=GetCardValue(int(question[i]));

} for(i=0;i5;i++)

{

MakeAnswer(answer,type[i],question,oper); //获取可能的答案

switch(type[i])

{

case 0:

sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))

break;

case 1:

sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)

break;

case 2:

sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)

break;

case 3:

sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D

break;

case 4:

sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D

break;

}

if(sum==24) return true;

}

return false;

}

/*

采用随机试探法:就是通过随机数字产生 加减乘除的 组合,通过大量的测试来命中的解法

提示:

1. 需要考虑用括号控制计算次序的问题 比如:( 10 - 4 ) * ( 3 + A ), 实际上计算次序的数目是有限的:

A*(B*(c*D))

A*((B*C)*D)

(A*B)*(C*D)

((A*B)*C)*D

(A*(B*C))*D

2. 需要考虑计算结果为分数的情况:( 3 + (3 / 7) ) * 7

3. 题目中牌的位置可以任意交换

*/

bool TryResolve(char* question, char* answer)

{

int oper[3]; // 存储运算符,0:加法 1:减法 2:乘法 3:除法

for(int i=0; i1000 * 1000; i++)

{

// 打乱纸牌顺序

shuffle(question);

// 随机产生运算符

for(int j=0; j3; j++)

oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;

} return false;

}

int main(int argc, char* argv[])

{

// 初始化随机种子

srand( (unsigned)time( NULL ) ); char buf1[4]; // 题目

char buf2[30]; // 解答

printf("***************************\n");

printf("计算24\n");

printf("A J Q K 均按1计算,其它按牌点计算\n");

printf("目标是:通过四则运算组合出结果:24\n");

printf("***************************\n\n");

for(;;)

{

GivePuzzle(buf1); // 出题

printf("题目:");

for(int j=0; j4; j++){

if( buf1[j] == 'T' )

printf("10 ");

else

printf("%c ", buf1[j]);

} printf("\n按任意键参考答案...\n");

getch(); if( TryResolve(buf1, buf2) ) // 解题

printf("参考:%s\n", buf2);

else

printf("可能是无解...\n"); printf("按任意键出下一题目,x 键退出...\n");

if( getch() == 'x' ) break;

} return 0;

}

求一段C语言的完整代码,复制粘贴答案后可以运行,要求如下:

#includestdio.h

#includestdlib.h

#includetime.h

int?main(void)

{

int?i,n;

char?string1[][100]={"方块","红桃","黑桃","梅花"};

????char?string2[][10]={"2","3","4","5","6","7","8","9","10","J","Q","K","A",};

printf("请输入所需要牌的张数:");

scanf("%d",n);

srand(time(NULL));?

for(i=0;in;i++)

{

printf("%s",string1[rand()%4]);?

printf("%s",string2[rand()%13]);

printf("\n");

????}

????return?0;

}

//执行结果

希望采纳!3Q!

(责任编辑:IT教学网)

更多

推荐Painter教程文章