📄 game.c
字号:
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#define BKCOLOR BLACK
#define MANCOLOR RED
#define OBJECTCOLOR YELLOW
#define Key_R 0x1372
#define Key_Up 0x4800
#define Key_Enter 0x1c0d
#define Key_Down 0x5000
#define Key_Esc 0x11b
#define Key_Right 0x4d00
#define Key_Left 0x4b00
int Ide;
int Key;
int s1[7]={0},s2[7]={0},s3[7]={0},p1,p2,p3,hball,flag1,p;
int n=3;
void InitMisson()
{
int i;
cleardevice();
for(i=1;i<=n;i++)
s1[i]=n-i+1;
flag1=0;
p1=n;
p2=0;
p3=0;
p=1;
}
void Init()
{
int gd=DETECT,gm; //图形函数初始化
initgraph(&gd,&gm,"");
}
void MainMenu()
{
setbkcolor(BKCOLOR);
cleardevice();
draw(); //绘制背景
Ide=0,Key=0;
DrawMenu(Ide); //输出菜单
do
{ //bioskey(m) m={0,1,2}
if(bioskey(1)) /* function 1 returns 0 until a key is pressed */
{ /* function 0 returns the value of key that is waiting */
Key=bioskey(0); /* use function 2 to determine if shift keys were used */
switch(Key)
{
case Key_Down:{Ide++;Ide=Ide%4;DrawMenu(Ide);break;}
case Key_Up: {Ide--;Ide=(Ide+4)%4;DrawMenu(Ide);break;}
}
}
else {if(TimeCome()) {cleardevice();draw();DrawMenu(Ide);}}
}while (Key!=Key_Enter);
JudgeIde();
}
int draw()
{
int i;
randomize();
for(i=0;i<100;i++)
{
setcolor(rand()%16);
circle(rand()%getmaxx(),rand()%getmaxy(),rand()%100);
}
}
int TimeCome()
{
static long tm, old;
tm=biostime(0,tm); //
if(tm-old<2000) return 0;
else
{
old=tm; return 1;
}
}
int DrawMenu(int j)
{
int n;
char *s[4]={"1.Start Game","2.Set n","3.Help","4.Exit Game"};
settextstyle(0,0,1);
setcolor(GREEN);
for(n=0;n<4;n++)
outtextxy(250,170+n*20,s[n]);
setcolor(RED);
outtextxy(250,170+j*20,s[j]);
}
void Setn()
{
int i;
do
{
clrscr();
cleardevice();
printf("Input n(0<n<8):\n");
scanf("%d",&i);}
while(i<1||i>7);
n=i;
printf("\nn is OK. n=%d",n);
getch();
}
void Help()
{
char *p="\n\n\t\t\t Welcome to play this game!\n\t\
\t There are seven leaves,you can choose by menu 2. \n\
\t You can press 'Left','Right','Up'and'down'key to move.\n\
\t Press the 'r' key to restart game. \n\
\t Press the 'Esc' key to return the initial menu. \n\n\n\n\
\t ******************************************************\n\
\t\t\t********Good Luck! *******\n\
\t*******************************************************\n\n\n\
\t\t\t\t\t\t\t\tmade by hjj." ;
randomize();
textcolor(random(6)+2);
while(*p++){printf("%c",*p);delay(5000);}
getch();
}
int JudgeIde()
{
switch(Ide)
{
case 0:Game();break;
case 1:Setn(); MainMenu(); break;
case 2:Help();MainMenu();break;
case 3:exit(0);
}
}
void drawrec(int h,int b,int p)
{ int maxx=getmaxx();
setwritemode(XOR_PUT);
rectangle(maxx/3*(h)-maxx/6-(b+1)*10,(p+1)*20,maxx/3*h-maxx/6+(b+1)*10,p*20);
}
void drawc1()
{int maxx=getmaxx();
setwritemode(XOR_PUT);
circle((maxx/3*p-maxx/6),250,10);
}
void drawc2(int h)
{int maxx=getmaxx();
setwritemode(XOR_PUT);
circle((maxx/3*p-maxx/6),(h+1)*20,10);
}
void Output()
{int i;
cleardevice();
if(flag1==0)drawc1();
else if(flag1==1&&p==1)drawc2(p1);
else if(flag1==1&&p==2)drawc2(p2);
else if(flag1==1&&p==3)drawc2(p3);
for(i=1;i<=p1;i++)
drawrec(1,s1[i],i);
for(i=1;i<=p2;i++)
drawrec(2,s2[i],i);
for(i=1;i<=p3;i++)
drawrec(3,s3[i],i);
}
int JudgeWin()
{
if(p3==n)return 1;
else return 0;
}
int Game()
{
int i,j,flag;
InitMisson();
Output();
do
{
flag=0;
Key=bioskey(0);
switch(Key)
{
case Key_Enter: {if(flag1==0)flag1=1;else flag1=0;flag=1;break;}
case Key_Up: {if(flag1==0)flag1=1;else flag1=0;flag=1;break;}
case Key_Down:{if(flag1==0)flag1=1;else flag1=0;flag=1;break;}
case Key_Left:
{
if(p==1)
{if(flag1==1&&s1[p1]!=0)
{ if(s3[p3]>s1[p1]||p3==0){s3[++p3]=s1[p1--];p=3;}
else if(s2[p2]>s1[p1]||s2[p2]==0){s2[++p2]=s1[p1--];p=2;}
else flag1=0;}
else {p=(p+2)%3;if(p==0)p=3;}
}
else if(p==2&&s2[p2]!=0)
{if(flag1==1)
{ if(s1[p1]>s2[p2]||p1==0){s1[++p1]=s2[p2--];p=1;}
else if(s3[p3]>s2[p2]||p3==0){s3[++p3]=s2[p2--];p=3;}
else flag1=0;}
else {p=(p+2)%3;if(p==0)p=3;}
}
else if(p==3&&s3[p3]!=0)
{if(flag1==1)
{if(s2[p2]>s3[p3]||p2==0){s2[++p2]=s3[p3--];p=2;}
else if(s1[p1]>s3[p3]||p1==0){s1[++p1]=s3[p3--];p=1;}
else flag1=0;}
else {p=(p+2)%3;if(p==0)p=3;}
}
else {p=(p+2)%3;if(p==0)p=3;}
flag=1;
break; }
case Key_Right:
{
if(p==1&&s1[p1]!=0)
{if(flag1==1)
{if(s2[p2]>s1[p1]||p2==0){s2[++p2]=s1[p1--];p=2;}
else if(s3[p3]>s1[p1]||p3==0){s3[++p3]=s1[p1--];p=3;}
else flag1=0;}
else {p=(p+1)%3;if(p==0)p=3;}
}
else if(p==2&&s2[p2]!=0)
{if(flag1==1)
{if(s3[p3]>s2[p2]||p3==0){s3[++p3]=s2[p2--];p=3;}
else if(s1[p1]>s2[p2]||p1==0){s1[++p1]=s2[p2--];p=1;}
else flag1=0;}
else {p=(p+1)%3;if(p==0)p=3;}
}
else if(p==3&&s3[p3]!=0)
{if(flag1==1)
{if(s1[p1]>s3[p3]||p1==0){s1[++p1]=s3[p3--];p=1;}
else if(s2[p2]>s3[p3]||p2==0){s2[++p2]=s3[p3--];p=2;}
else flag1=0;}
else {p=(p+1)%3;if(p==0)p=3;}
}
else {p=(p+1)%3;if(p==0)p=3;}
flag=1;
break;}
case Key_R:{InitMisson();break;}
case Key_Esc:{MainMenu();break;}
}
if(flag==1)
{ Output(); if(JudgeWin()){printf("you win!!\n\n\nPress any key return Menu.");getch();clrscr(); cleardevice();MainMenu();} }
}while(1);
}
void main()
{
Init();
MainMenu();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -