Ⅰ 用c语言编汉诺塔游戏要有图形
Program Hanoi(input,output);
uses
crt;
type
a3=array[1..3] of byte;
var
n :byte;
Col,Flag:a3;
(* print a disk size is n on (x,y) *)
Procere Disk(x,y,n:byte);
var
i:byte;
Begin
for i:=0 to n do
begin
gotoxy(x+i,y);write('?);
gotoxy(x-i,y);write('?);
gotoxy(80,25);
end;
end;
(* a sound procere when move a disk *)
Procere Sing;
var
i,freq:integer;
Begin
Randomize;
for i:=1 to 10 do
begin
freq:=Random(2900)+100;
Sound(freq); Delay(20000);
NoSound;
end;
end;
(* clear a disk size is n on (x,y) *)
Procere ClrDisk(x,y,n:byte);
var
i:byte;
Begin
for i:=n downto 0 do
begin
gotoxy(x+i,y);write(' ');
gotoxy(x-i,y);write(' ');
end;
end;
(* initiate procere *)
Procere Initiate;
var
i:byte;
Begin
clrscr;
repeat { input n }
gotoxy(5,5);
write('输入汉锘塔的层数<1髇?1>:');
ClrEol;read(n);
until (0<n) and (n<12);
clrscr; {version information}
gotoxy(28,1);
write('** ',n,'-Hanoi Problem **');
gotoxy(37,2);
write('1992.10 W.Y.Z');
for i:=8 to 19 do {three pointers }
begin
gotoxy(15,i);write('?);
gotoxy(40,i);write('?);
gotoxy(65,i);write('?);
end;
for i:=1 to 80 do { the bottom }
begin
gotoxy(i,20);write('?);
end;
{ print A,B,C }
gotoxy(15,21);write('A');
gotoxy(40,21);write('B');
gotoxy(65,21);write('C');
for i:=n downto 1 do {n disks }
Disk(15,19-n+i,i-1);
{ initiate array Col[3],Flag[3] }
Col[1]:=15; Col[2]:=40; Col[3]:=65;
Flag[1]:=n+1; Flag[2]:=1; Flag[3]:=1;
{ some informations }
gotoxy(5,24);
write('Press spacebar to begin...');
repeat until ReadKey<>'';
gotoxy(5,24);ClrEol;
gotoxy(5,24);ClrEol;
write('Press any key to break.');
end;
(* move a disk FROM from TO too *)
Procere Move(m,from,too:byte);
var
x1,y1,x2,y2,n,step:byte;
Begin
x1:=Col[from]; y1:=20-Flag[from]+1;
x2:=Col[too] ; y2:=20-Flag[too];
step:=too-from; n:=m-1;
repeat { up }
ClrDisk(x1,y1,n);
if y1>=8 then begin gotoxy(x1,y1);write('?); end;
dec(y1);
Disk(x1,y1,n);Delay(10000);
until y1=5;
dec(Flag[from]);
repeat { shift }
ClrDisk(x1,5,n);
inc(x1,step);
Disk(x1,5,n);Delay(10000);
until x1=x2;
repeat { down }
ClrDisk(x2,y1,n);
if y1>=8 then begin gotoxy(x1,y1);write('?); end;
inc(y1);
Disk(x2,y1,n);Delay(10000);
until y1=y2;
inc(Flag[too]);
(* Sing; *)
if KeyPressed then Halt(0);
end;
(* n Hanoi Problem *)
procere N_Hanoi(n,a,b,c:byte);
Begin
if n=1 then Move(1,a,c)
else begin
N_Hanoi(n-1,a,c,b);
Move(n,a,c);
N_Hanoi(n-1,b,a,c);
end;
end;
(* print end information of this program *)
Procere Final;
Begin
gotoxy(5,24);ClrEol;
write('I have moved all disks !');
gotoxy(5,25);
write('Press spacebar to end.');
repeat until ReadKey<>'';
end;
Begin
Initiate;
N_Hanoi(n,1,2,3);
Final;
End.
Ⅱ 怎样用C语言编写一个小游戏
“贪吃蛇”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;i<H;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;i<snk.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==72&&dir!=2)dir=1; //72表示按下了向上方向键
if(key==80&&dir!=1)dir=2; //80为向下
if(key==75&&dir!=4)dir=3; //75为向左
if(key==77&&dir!=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("