求几个比较有趣,简单的C语言源代码 小白自己敲着练一下手感

求几个比较有趣,简单的C语言源代码 小白自己敲着练一下手感,第1张

最简单的模拟计时器:

#include<stdio.h>

#include<conio.h>

#include<windows.h>

int m=0,s=0,ms=0  //m是分 s是秒 ms是毫秒

//以下是5个自编函数

void csh( )  //初始化界面

void yinc(int x,int y)  //隐藏光标的函数(y值设为0就会隐藏)

void jishi( ) //计时器运行(每100毫秒变化一次)

void Color (short x, short y) //设定颜色的函数(y设为0就是黑底)

void gtxy (int x, int y) 闷带//控制光标位置的函数

int main(  )  //主函数

{  csh( )

   getch( )

   while(1)

       { jishi( )

         Sleep(100)  //间隔100毫秒

         if( kbhit( ) )break  //有键按下就退出循环

       }

    return 0

}

void csh( )   //初始化界面

{Color(14,0)   //设定淡黄字配黑底

printf(“\n\n\t    计时器”)

Color(10,0)  //设定淡绿字配黑底

printf("\n\t┌───────────┐")

printf("\n\t│    汪罩橘   困团    │")

printf("\n\t└───────────┘")

gtxy(10,4)  //光标到屏幕第10列4行处输出

Color(7,0)  //恢复白字黑底

printf(" 00:00:00 ")

yinc(1,0 )  //隐藏光标(yinc代表隐藏)

return

}

void jishi( )  //计时器运行

{ms+=1

if(ms==10){s+=1ms=0}

if(s==60){m+=1s=0}

gtxy(10,4)

Color(9,0)  //设定淡蓝字配黑底

if(m>9) printf(" %d:",m)

else printf(" 0%d:",m)

Color(14,0)  //设定淡黄字配黑底

if(s>9) printf("%d:",s)

else printf("0%d:",s)

Color(12,0)  //设定淡红字配黑底

printf("0%d",ms)

}

void gtxy (int x, int y)   //控制光标位置的函数

{ COORD pos

pos.X = x

pos.Y = y

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos )

}

void Color (short ForeColor= 7, short BackGroundColor= 0)   //设定颜色的函数

{ HANDLE  handle = GetStdHandle ( STD_OUTPUT_HANDLE )

SetConsoleTextAttribute ( handle, ForeColor + BackGroundColor * 0x10 )

}

void yinc(int x,int y)   //隐藏光标的设置(gb代表光标)

{ CONSOLE_CURSOR_INFO  gb={x,y}  //x为1-100,y为0就隐藏光标

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &gb)

}

/*太大了估计你不想要,给你一个扫雷哪渗程序,turbo

C下才能编译成功

空格:打标记

回车:扫雷

方向键:改变方向

*/

#include<stdio.h>

#include<conio.h>逗缓巧

#include<time.h>

void

adjust(int*,int*)

int

a[23][23],c[22][22],mm,nn

int

roundmine(int,int)

void

spread()

main()

{

int

i,j,b[22][22],minenum,mine

int

x,y,yy,s

char

g,h,z

int*x1

int*y1

time_t

t

again:srand((unsigned)

time(&t))

textcolor(LIGHTGREEN)

for(i=2i<=21i++)

for(j=2j<=21j++)

{

gotoxy(i,j)

putch(219)

b[i][j]=1

c[i][j]=1

}

minenum=0

for(i=1i<=22i++)

for(j=1j<=22j++)

{

a[i][j]=0

if(i>1&&i<22&j>1&&j<22)

{if((s=rand()%10)>0)

a[i][j]=0

else

if(s==0)

a[i][j]=1

}

if(a[i][j])

minenum++

}

gotoxy(50,2)

puts("dirction")

gotoxy(50,3)

puts("SPACE:mark")

gotoxy(50,4)

puts("ENTER:dig")

gotoxy(50,8)

printf("mine

num:%4d",minenum)

gotoxy(50,5)

printf("ESC:exit")

gotoxy(30,15)

puts("

")

gotoxy(30,20)

puts("

")

mine=minenum

gotoxy(2,2)

x=2y=2

while(mine)

{

h=getch()

if(h=='H')

gotoxy(x,--y)

else

if(h=='P')

gotoxy(x,++y)

else

if(h=='K')

gotoxy(--x,y)

else

if(h=='M')

gotoxy(++x,y)

x1=&xy1=&y

adjust(x1,y1)

if(h=='

')

{

if(c[wherex()][wherey()])

if(b[wherex()][wherey()])

{

textcolor(LIGHTRED)

putch(16)

mine--

gotoxy(59,8)

printf("%4d",mine)

gotoxy(x,y)

b[wherex()][wherey()]=0

}

else

{

textcolor(LIGHTGREEN)

putch(219)

mine++

gotoxy(59,8)

printf("%4d",mine)

gotoxy(x,y)

b[wherex()][wherey()]=1

}

}

if(h=='\015')

{

mm=wherex()nn=wherey()

c[wherex()][wherey()]=0

if(a[wherex()][wherey()])

{

for(i=2i<=21i++)

for(j=2j<=21j++)

{gotoxy(i,j)

if(a[i][j])

{textcolor(LIGHTRED)

putch(017)

}

}

break

}

else

{spread()gotoxy(mm,nn)}

}

if(h=='\033')

exit(0)

}

yy=1

for(i=2i<=21i++)

for(j=2j<=21j++)

if

(a[i][j]==1&&b[i][j]==1)

{gotoxy(30,15)

cprintf("You

lose!!")

yy=0

}

if(yy)

{gotoxy(30,15)

cprintf("You

win!!")

}

gotoxy(30,20)

cprintf("Once

again(y/n)?"山键)

z=getch()

if(z=='y')

goto

again

}

void

adjust(int*

x,int*

y)

{

if(*x<2)

gotoxy(*x+=20,*y)

else

if(*x>21)

gotoxy(*x-=20,*y)

else

if(*y<2)

gotoxy(*x,*y+=20)

else

if(*y>21)

gotoxy(*x,*y-=20)

}

int

roundmine(int

x,int

y)

{

int

i,j,r=0

for(i=x-1i<=x+1i++)

for(j=y-1j<=y+1j++)

if(a[i][j])

r++

return

r

}

void

spread()

{

int

r,i,j,x,y

x=wherex()

y=wherey()

r=roundmine(x,y)

if(r)

{printf("%d",r)c[x][y]=0}

else

if(r==0&&x>1&&x<22&&y>1&&y<22)

{

putchar('

')c[x][y]=0

for(i=x-1i<=x+1i++)

for(j=y-1j<=y+1j++)

if(x>1&&x<22&&y>1&&y<22&&c[i][j])

{gotoxy(i,j)

spread()

}

}

}


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

原文地址: http://outofmemory.cn/yw/8271580.html

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

发表评论

登录后才能评论

评论列表(0条)

保存