c语言最短游戏代码
/* Snake */
# includestdio.h
#includetime.h
#includeconio.h
#includestdlib.h
inthead = 3,tail = 0;
intmain()
{
inti,j,k = 0;
Intzuobiaoc语言游戏代码 最基本的蛇代码。
# includestdio.h
#includewindows.h//基本类型定义。支持类型定义功能。用户界面功能图形设备界面功能。
# include conio . h//用户按键盘产生的相应操作(控制台)
#includestdlib.h
#includetime.h //日期和时间头文件
#定义镜头30
#定义WID 25
int Snake[LEN][WID]= { 0 };//数组的元素表示蛇的各个部分。
char Sna _ Hea _ Dir = ' a//记录蛇头的移动方向。
int Sna_Hea_X,Sna _ Hea _ Y;//记录黑鱼的位置
int Snake _ Len = 3;//记录蛇的长度
时钟_t现在_时间;//记录自动移动的当前时间。
int Wait _ Time//记录自动运动的时间间隔。
int Eat _ Apple = 1;//吃苹果的意思是1。
int级别;
int All _ Score =-1;
int Apple _ Num =-1;
HANDLE hConsole = GetStdHandle(STD _ OUTPUT _ HANDLE);//获取标准输出的句柄,windows.h
//Handle:表示应用程序中的不同对象和同一类对象中的不同实例易于操作。
Void gotoxy(int x,int y)//设置光标位置。
{
COORD pos = {x,y };//定义字符在控制台屏幕上的坐标位置。
SetConsoleCursorPosition(hConsole,pos);//定位光标位置的函数windows.h
}
Void Hide_Cursor()//隐藏光标固定函数
{
控制台_光标_信息光标_信息= {1,0}。
SetConsoleCursorInfo(hConsole,cursor _ info);
}
Void SetColor(int color)//设置颜色。
{
SetConsoleTextAttribute(hConsole,color);
//是设置字体颜色和背景颜色的API的函数格式:SetConsoleTextAttribute (handle,color);
}
Void Print_Snake()//打印蛇头和蛇尾。
{
int iy,ix,color
for(iy = 0;iy WID;iy)
for(IX = 0;ix LEN九)
{
If(Snake[ix][iy] == 1)//蛇头
{
set color(0xf);//oxf代表分配的内存地址setcolor:34行自定义颜色设置函数。
gotoxy(ix*2,iy);
printf(" ※");
}
If(Snake[ix][iy] == 2)//蛇的脖子
{
颜色= rand() 1;//rand()函数是生成随机数的随机函数。C语言中也有srand()函数。
//头文件:stdlib.h
if(color == 14)
color-= rand()% 13 1;//变色
SetColor(颜色);
gotoxy(ix*2,iy);
printf("■");
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2,iy);
set color(0xe);
printf("≈";
}
}
}
Void Clear_Snake()//擦除蛇
{
int iy,IX;
for(iy = 0;iy WID;iy)
for(IX = 0;ix LEN九)
{
gotoxy(ix*2,iy);
if(Snake[ix][iy] == Snake_Len)
printf(" ");
}
}
void land _ apple()//随机生成苹果。
{
int ix,iy;
做
{
IX = rand()% LEN;
iy = rand()% WID;
} while(Snake[IX][iy]);
snake[IX][iy]=-1;
gotoxy(ix*2,iy);
printf("⊙");
eat _ Apple = 0;
}
Void Game_Over()//蛇死了。
{
gotoxy(30,10);
printf("游戏结束");
睡眠(3000);
system(" pause nul ");
退出(0);
}
Void Move_Snake()//让蛇动起来。
{
int ix,iy;
for(IX = 0;ix LENIx)//先标记黑鱼
for(iy = 0;iy WID;iy)
if(Snake[ix][iy] == 1)
{
Switch(Sna_Hea_Dir)//根据新的蛇头方向标记蛇头。
{
案例“w”:
如果(iy == 0)
Game_Over()。
其他
SNA _ Hea _ Y = iy-1;
Sna _ Hea _ X = ix
打破;
案例:
如果(iy == (WID -1))
Game_Over()。
其他
SNA _ Hea _ Y = iy 1;
Sna _ Hea _ X = ix
打破;
案例“a”:
如果(ix == 0)
Game_Over()。
其他
SNA _ Hea _ X = IX-1;
Sna _ Hea _ Y = iy
打破;
案例“d”:
if(ix == (LEN - 1))
Game_Over()。
其他
SNA _ Hea _ X = IX 1;
Sna _ Hea _ Y = iy
打破;
默认值:
打破;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1Snake[Sna_Hea_X][Sna_Hea_Y]!=0Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over()。
if(SNA[SNA _ HEA _ X][SNA _ HEA _ Y]0)//吃苹果。
{
Snake _ Len
eat _ Apple = 1;
}
for(IX = 0;ix LENIx)//处理蛇尾
for(iy = 0;iy WID;iy)
{
if(Snake[ix][iy] 0)
{
if(Snake[ix][iy]!=蛇_Len)
snake[IX][iy]= 1;
其他
snake[IX][iy]= 0;
}
}
snake[Sna _ Hea _ X][Sna _ Hea _ Y]= 1;//对付蛇头
}
Void Get_Input()//控制蛇的移动方向。
{
if(kbhit())
{
开关(getch())
{
案例87:
Sna _ Hea _ Dir = ' w
打破;
案例83:
Sna _ Hea _ Dir = ' s
打破;
案例65:
Sna _ Hea _ Dir = ' a
打破;
案例68:
Sna _ Hea _ Dir = ' d
打破;
默认值:
打破;
}
}
if(clock()-now _ time = wait _ time)//时间到了蛇自动行走。
{
clear _ Snake();
move _ Snake();
print _ Snake();
现在_时间=时钟();
}
}
Void Init()//初始化
{
系统(“标题贪婪的毛毛蛇”);
system("mode con: cols=80行= 25 ");
Hide_Cursor()。
gotoxy(61,4);
printf("你得分:");
gotoxy(61,6);
printf("你水平:");
gotoxy(61,8);
printf(" The length:");
gotoxy(61,10);
printf("速度:");
gotoxy(61,12);
printf(" Apple Num:");
int I;
for(I = 0;我Snake _ LenI)//生成蛇
snake[10 I][15]= I 1;
int iy,IX;//打印蛇
for(iy = 0;iy WID;iy)
for(IX = 0;ix LEN九)
{
if(Snake[ix][iy])
{
set color(Snake[IX][iy]);
gotoxy(ix*2,iy);
printf("■");
}
}
}
Void Pri_News()//打印信息
{
set color(0xe);
gotoxy(73,4);
All _ Score = Level
printf("= ",All _ Score);
gotoxy(73,6);
printf("= ",Level);
gotoxy(73,8);
printf("= ",Snake _ Len);
gotoxy(73,10);
printf("0。=s”,Wait _ Time/10);
gotoxy(73,12);
printf("%d ",Apple _ Num);
}
Void Lev_Sys()//层次结构
{
if((Apple _ Num-1)/10)= = Level)
{
水平;
如果(等待时间50)
wait _ Time-= 50;
其他
if(等待时间10)
wait _ Time-= 10;
其他
wait _ Time-= 1;
}
}
int main(void)
{
init();
srand((无符号)时间(空));//设置随机数的种子
现在_时间=时钟();
int speed1=1000,speed2,a;
printf(" \ n ");
Printf("请输入您想要的速度\ n ");
scanf("%d ",speed 2);
level = 1;
wait _ Time = speed speed 2;
Printf("请输入您想要的苹果数量\ n ");
scanf("%d ",a);
当(a -)
rand _ Apple();
while(1)
{
如果(吃_苹果)
{
Apple _ Num
rand _ Apple();
lev _ Sys();
pri _ News();
}
get _ Input();
睡眠(10);
}
返回0;
}
求几个C语言的小游戏代码,简单,带注释,谢谢, //calc 24 . CPP:定义控制台应用程序的入口点。
//
/*
6-6
二十一点游戏
*/
#包含“conio.h”
#包含" stdlib.h "
#包含“time.h”
#包含“math.h”
#include "string.h"/*
从一副扑克牌中任选四张。
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];
}
}
无效洗牌(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 ')返回10;
if(c='0' c='9 ')返回c-' 0 ';
返回1;
}
char GetOper(int n)
{
开关(n)
{
案例0:
返回“”;
案例1:
返回'-';
案例二:
返回“*”;
案例三:
返回“/”;
} return“”;
} double my calc(double op1,double op2,int oper)
{
开关(操作)
{
案例0:
返回op1 op2
案例1:
返回opop2;
案例二:
返回op1 * op2
案例三:
if(fabs(op2)0.0001)
返回op1/op2;
其他
返回100000;
}返回0;
}
void MakeAnswer(char* answer,int type,char* question,int* oper)
{
char p[4][3];
for(int I = 0;i4;我)
{
if(问题[i] == 'T ')
strcpy(p[i]," 10 ");
其他
sprintf(p[i]," %c ",问题[I]);
}
开关(类型)
{
案例0:
sprintf(答案," %s %c (%s %c (%s %c %s))",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
打破;
案例1:
sprintf(答案," %s %c ((%s %c %s) %c %s)",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
打破;
案例二:
sprintf(答案,"(%s %c %s) %c (%s %c %s)",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
打破;
案例三:
sprintf(答案,“((%s %c %s) %c %s) %c %s”,
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
打破;
案例4:
sprintf(答案,"(%s %c (%s %c %s)) %c %s ",
p[0],GetOper(oper[0]),p[1],GetOper(oper[1]),p[2],GetOper(oper[2]),p[3]);
打破;
}
}
bool TestResolve(char* question,int* oper,char*答案)
{
//等待考生完成。
int type[5]={0,1,2,3,4 };//计算类型
双p[4];
double sum = 0;
//
for(int I = 0;i4;I) //循环获得积分。
{
p[I]= GetCardValue(int(question[I]));
} for(I = 0;i5;我)
{
MakeAnswer(答案,类型[i],问题,操作);//获得可能的答案
开关(类型[i])
{
案例0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2],p[3],oper[2]),oper[1],oper[0]);//A*(B*(c*D))
打破;
案例1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1],p[2],oper[1]),p[3],oper[2],oper[0]);//A *(B * C)* D)
打破;
案例二:
sum=MyCalcu(MyCalcu(p[0],p[1],oper[0]),MyCalcu(p[2],p[3],oper[2],oper[1]);// (A*B)*(C*D)
打破;
案例三:
sum = my calcu(my calcu(my calcu(p[0],p[1],oper[0]),p[2],oper[1]),p[3],oper[2]);//((A*B)*C)*D
打破;
案例4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1],p[2],oper[1]),oper[0],p[3],oper[2]);//(A*(B*C))*D
打破;
}
if(sum==24)返回true
}
返回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我)
{
//打乱卡片的顺序
洗牌(问题);
//随机生成的运算符
for(int j = 0;J3;j)
oper[j]= rand()% 4;if( TestResolve(question,oper,answer))返回true
}返回false
}
int main(int argc,char* argv[])
{
//初始化随机种子
srand((无符号)时间(空));char buf 1[4];//主题
char buf 2[30];//回答
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");
Printf("计算24 \ n ");
Printf("A J Q K按1计算,其他按卡点数计算\ n ");
Printf("目标是通过四次运算组合结果:24 \ n ");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * \ n \ n ");
for(;;)
{
give puzzle(buf 1);//给出一个问题
printf(" Title:");
for(int j = 0;j4;j ){
if( buf1[j] == 'T ')
printf(" 10 ");
其他
printf("%c ",buf 1[j]);
} printf(" \ n按任意键查看答案...\ n ");
getch();If( TryResolve(buf1,buf2)) //解决问题
Printf ("reference: %s\n ",buf 2);
其他
Printf("可能无解...\ n ");Printf("按任意键进入下一个主题,按X键退出...\ n ");
if(getch()= = ' x ')break;
}返回0;
}
最后更新于 2023-10-13 08:48:31 并被添加「」标签,已有 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
相关文章