c语言小游戏代码
“贪吃蛇”C代码,在dev C++试验通过(用4个方向键控制)
#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]; //蛇身某节x坐标
int y[100]; //蛇身某节y坐标
}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 Color(int a); //设定显示颜色的函数
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个位置
{Color(2); //设定打印颜色为绿色
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的倍数(为偶数)
}
Color(12); //设定打印颜色为淡红
gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物
snk.len=3; //蛇身长初值为3节
snk.speed=350; //刷新蛇的时间,即移动速度初值为350毫秒
snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)
snk.y[0]=H/2; //蛇头纵坐标
Color(9); //设定打印颜色为淡蓝
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("■"); //打印蛇身
}
Color(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要向右移动
}
Color(9);
gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头
if (snk.x[0] == fod.x snk.y[0] == fod.y) //如吃到食物则执行以下操作
{ printf("\7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是响铃
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且横坐标为偶数,则食物坐标取值成功
}
Color(12); //设定字符为红色
gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物
}
return;
}
int Over( ) //判断游戏是否结束的函数
{ int i;
Color(7);
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 Color(int a) //设定颜色的函数
{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }
C语言简易文字冒险游戏源代码
记忆游戏
#includestdio.h
#includetime.h
#includestdlib.h
#includewindows.h
#define N 10
int main( )
{int i,k,n,a[N],b[N],f=0;
srand(time(NULL));
printf(" 按1开始\n 按0退出:_");
scanf("%d",n);
system("cls");
while(n!=0)
{for(k=0;kN;k++)a[k] = rand( )%N;
printf("\n\t\t[请您牢记看到颜色的顺序]\n\n");
for(k=0;kN;k++)
{switch(a[k])
{case 0:system("color 90");printf(" 0:淡蓝色\n");break; //淡蓝色
case 1:system("color f0");printf(" 1:白色\n");break; //白色
case 2:system("color c0");printf(" 2:淡红色\n");break; //淡红色
case 3: system("color d0");printf(" 3:淡紫色\n");break; //淡紫色
case 4: system("color 80");printf(" 4:灰色\n"); break; //灰色
case 5: system("color e0");printf(" 5:黄色\n");break; //黄色
case 6: system("color 10");printf(" 6:蓝色\n"); break; //蓝色
case 7: system("color 20");printf(" 7:绿色\n");break; //绿色
case 8: system("color 30");printf(" 8:浅绿色\n");break; //浅绿色
case 9: system("color 40");printf(" 9:红色\n");break; //红色
}
Sleep(1500);
system("color f"); //单个控制 文字颜色
Sleep(100);
}
system("cls");
printf(" 0:淡蓝色,1:白色,2:淡红色,3:淡紫色,4:灰色,5:黄色,6:蓝色7:绿色,8:浅绿色,9:红色\n");
printf("\n\t请输入颜色的顺序:");
for(k=0;kN;k++)scanf("%d",b[k]);
for(k=0;kN;k++)if(a[k] == b[k]) f++;
if(f==0) printf(" 你的记忆弱爆了0\n");
else if(f==1) printf(" 你的记忆有点弱1\n");
else if(f5) printf(" 你的记忆一般5\n");
else printf(" 你的记忆力很强!\n");
Sleep(2000);
system("cls");
printf("\t\t按0退出\n\t\t按任意键继续游戏:\n");
scanf("%d",n);
system("cls");
}
return 0;
}
注:DEVc++运行通过,每输入一个数字要加入一个空格。
怎样用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;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++编写的小游戏源代码
以下是贪吃蛇源代码:
#includeiostream.h
#includewindows.h
#includetime.h
#includestdlib.h
#includeconio.h
#define N 21
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i=N;i++)
{
for(j=1;j=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;iN+2;i++)
{
for(j=0;jN+2;j++)
{
if(wall[i][j])
cout"■";
else cout"□" ;
}
coutendl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout"按 W S A D 移动方向"endl;
gotoxy(N+3,2);
color(20);
cout"按任意键暂停"endl;
gotoxy(N+3,3);
color(20);
cout"得分:"endl;
apple[0]=rand()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout"●"endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;ilen;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;ilen;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout"★"endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout"■"endl;
for(i=len-1;i0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout"★"endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout"★"endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout"●"endl;
gotoxy(N+5,3);
color(20);
coutscoreendl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout"失败!!!"endl;
for(i=0;ilen;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}参考资料:从C++吧看来的
各一个最简单的vb打字游戏代码
46. 键盘打字练习程序
本程序用于打字练习,训练和提高打字速度。可选择小写字母、大写字母、大小写混合字母、特殊字符、数字、混合字符、汉字等 7 种练习方式。
程序启动后,字符不断随机出现并落下,如果练习者键入的字符正确,该字符变红变大,随后快速缩小,最终消失。
程序可显示练习者的成绩、正确率和每分钟的打字速度。练习中双击窗口,可清除数据,重新开始。注意进行字母或汉字输入练习时,要将输入法切换到正确的状态。
' '下面是窗体代码,在 VB6.0 调试通过:
'在窗体上放置下面 6 个控件:
' Option1: 用于进行字符类型选择
' Label1: 用于显示下落的字符
' Label2: 用于显示游戏信息
' Timer1: 用于移动字符
' Picture1:下落字符的背景
' Text1: 用于显示游戏者键入的字符
'在属性窗口将 Option1 和 Label1 的 Index 属性设置为 0,其他控件及属性不必设置,采用默认属性即可。
'如果要进行汉字打字练习,在工程所在目录下用记事本建立一个文本文件,文件名为:HanZi.txt
' 在此文本文件中输入练习所用的汉字,如:汉字=中华人民共和国,伟大的奥运会!
' 注意,输入的字符前面三个必须是:汉字=
'本人原创,转载请注明来源:http://hi.baidu.com/100bd/blog/item/633b6467dbfdd828aa184cdb.html
Dim ctY As Single, ctCount As Long, ctFinish As Long, ctTime As Single
Dim ctSize As Single, ctStr As String, ctStrS As Long, ctNoCheck As Boolean
Private Sub Form_Load()
Dim I As Long
Me.KeyPreview = True: Me.Caption = "键盘打字速度练习"
Text1.Text = "": ctSize = 36 '显示字符大小
Me.BackColor = RGB(50, 50, 155): Picture1.BackColor = RGB(100, 100, 255)
Label2.BackColor = Me.BackColor: Label2.ForeColor = HFFFFFF
Label2.Caption = "成绩: 0%": Label2.AutoSize = True: '用于显示成绩信息
Me.Move Screen.Width * 0.1, Screen.Height * 0.1, Screen.Width * 0.8, Screen.Height * 0.8
Set Label1(0).Container = Picture1
Label1(0).Font.Bold = True: Label1(0).BackStyle = 0 '用于显示下落的字符
Label1(0).UseMnemonic = False: Label1(0).AutoSize = True
For I = 0 To 4 '同时出现的字符个数,这里设置为 5,可修改
If I 0 Then Load Label1(I)
Next
Option1(0).BackColor = Me.BackColor: Option1(0).ForeColor = HFFFFFF
For I = 0 To 6
If I 0 Then Load Option1(I)
Next
Option1(0).Caption = "小写字母": Option1(1).Caption = "大写字母"
Option1(2).Caption = "大小写混合字母": Option1(3).Caption = "特殊字符"
Option1(4).Caption = "数字": Option1(5).Caption = "混合字母和字符"
Option1(6).Caption = "汉字":
For I = 0 To Option1.Count - 1
Option1(I).Visible = True
Next
Option1(0).Value = True
Call StartPlay
ctY = Picture1.ScaleY(5, 3, Picture1.ScaleMode) '字符下落速度(这里为 5 个像素),可修改
Timer1.Enabled = True: Timer1.Interval = 50
End Sub
Private Sub Form_Activate()
On Error Resume Next
Text1.SetFocus
End Sub
Private Sub Text1_Change()
Dim I As Long
If ctNoCheck Or Len(Text1.Text) 11 Then Exit Sub
ctNoCheck = True
I = Text1.SelStart: If I = 0 Then I = 1
Text1.Text = Mid(Text1.Text, I, 1)
Text1.SelStart = Len(Text1.Text)
ctNoCheck = False
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim I As Long, nStr As String, T As Single
nStr = Chr(KeyAscii)
If nStr = " " Then nStr = "(空格)"
For I = 0 To Label1.Count - 1
If nStr = Label1(I).Caption And Label1(I).Tag = "1" Then
Label1(I).ForeColor = 255: Label1(I).Tag = "2"
Label1(I).Font.Size = ctSize * 4
T = Picture1.ScaleHeight - Label1(I).Height
If Label1(I).Top T Then Label1(I).Top = T
ctFinish = ctFinish + 1: Call ShowInf
Exit Sub
End If
Next
End Sub
Private Sub ShowInf()
Dim nStr As String, T As Long, M As Long, H As Long, StrT As String, V As Long
If ctCount 0 Then nStr = Format(ctFinish / ctCount * 100, "0.0") Else ctCount = "0"
nStr = "正确率:" nStr "%"
nStr = "总数:" ctCount vbCrLf vbCrLf "正确:" ctFinish vbCrLf vbCrLf nStr
T = Int(Timer - ctTime)
If T 0 Then V = ctFinish / T * 60
H = T \ 3600: T = T Mod 3600
M = T \ 60: T = T Mod 60
If H 0 Then StrT = H ":"
If H 0 Or M 0 Then StrT = StrT M "'"
'StrT = "12:34'"'****调试代码
nStr = nStr vbCrLf vbCrLf "时间:" StrT T """"
Label2.Caption = nStr vbCrLf vbCrLf "速度:" V "字/分"
End Sub
Private Sub Form_Resize()
Dim I As Long, L As Single, T As Single, W As Single, H As Single, MaxW As Single
Text1.Font.Size = 16: Label2.Font.Size = 12
H1 = Me.TextHeight("1")
L = H1: T = H1
For I = 0 To Option1.Count - 1
Option1(I).Visible = True: W = Me.TextWidth(Option1(I).Caption "123456")
If MaxW W Then MaxW = W
Option1(I).Move L, T, W, H1 * 1.25
T = T + H1 * 1.5
Next
Label2.Move L * 0.8, T + H1
H = Me.ScaleHeight - Text1.Height
L = L + MaxW: W = Me.ScaleWidth - L
If W 0 And H 0 Then Picture1.Move L, 0, W, H
W = Me.ScaleWidth - L
If W 0 Then Text1.Move L, Picture1.Height, W
End Sub
Private Sub Picture1_Click()
Text1.SetFocus
End Sub
Private Sub Picture1_DblClick()
Call StartPlay
End Sub
Private Sub StartPlay()
ctFinish = 0: ctCount = 0: ctTime = Timer
For I = 0 To Label1.Count - 1
Label1(I).Tag = "": Label1(I).Visible = False
Next
End Sub
Private Sub Option1_Click(Index As Integer)
GetStr Index
Call StartPlay
On Error Resume Next
Text1.SetFocus
End Sub
Private Sub GetStr(nMode As Integer)
Dim Str1 As String, Str2 As String, Str3 As String
Str1 = " abcdefghijklmnopqrstuvwxyz"
Str2 = " ~!@#$%^*()_+|-=\{}:""?[];',./"
Str3 = "0123456789"
Select Case nMode
Case 1: ctStr = UCase(Str1) '大写字母
Case 2: ctStr = Str1 UCase(Str1) '大小写混合字母
Case 3: ctStr = Str2: '特殊字符
Case 4: ctStr = Str3: '数字
Case 5: ctStr = Str1 UCase(Str1) Str2 Str3 '大小写混合和特殊字符
Case 6: ctStr = ReadText(): '汉字
Case Else: ctStr = Str1: '小写字母
End Select
ctStrS = Len(ctStr)
End Sub
Private Function ReadText() As String
'从文本文件中读出要进行汉字练习的字符
Dim F As String, H As Long, nStr As String
On Error GoTo cuo
F = App.Path
If Right(F, 1) "\" Then F = F "\"
F = F "HanZi.txt"
H = FreeFile
Open F For Input As #H
Line Input #H, nStr
Close #H
If Left(nStr, 3) "汉字=" Then GoTo cuo '检查文件标记
nStr = Trim(Mid(nStr, 4))
If nStr = "" Then GoTo cuo
ReadText =nStr
'ReadText = "中华人民共国" ' ****调试代码
Exit Function
cuo:
ReadText = "错误"
End Function
Private Sub RndStr(I As Long)
'随机字符和位置
Static xUp As Single
Dim x As Single, W As Single, S As Long
If Label1(I).Tag "" Then Exit Sub
Label1(I).Font.Size = ctSize: Label1(I).ForeColor = HFFFFFF
Randomize: S = 1 + Int(Rnd * ctStrS): Label1(I).Caption = Mid(ctStr, S, 1) '随机字符
If Label1(I).Caption = " " Then Label1(I).Caption = "(空格)"
Randomize
W = Picture1.ScaleWidth
Do '随机位置:确保不重叠
x = Rnd * (W - Label1(I).Width)
S = S + 1
If S 100 Or W Label1(I).Width * 2 Then Exit Do
If Abs(xUp - x) Label1(I).Width Then Exit Do
Loop
Label1(I).Move x, -ctY
xUp = x
Label1(I).Visible = True: Label1(I).Tag = "1"
ctCount = ctCount + 1: Call ShowInf
End Sub
Private Sub Timer1_Timer()
Dim I As Long, NextI As Long, S As Single, Have1 As Boolean, T As Long
For I = 0 To Label1.Count - 1
If Label1(I).Tag "" Then
Have1 = True
Label1(I).Top = Label1(I).Top + ctY
If Label1(I).Tag = "2" Then '已击中的字符
S = Label1(I).Font.Size * 0.7
If S 3 Then Label1(I).Font.Size = S Else Label1(I).Tag = "": Label1(I).Visible = False
End If
If Label1(I).Top Picture1.ScaleHeight Then Label1(I).Tag = "": Label1(I).Visible = False
If Label1(I).Top Picture1.ScaleHeight * 0.2 Then '超过窗体高度 1/5 ,下一个字符出现
NextI = I + 1
If NextI Label1.Count - 1 Then NextI = 0
RndStr NextI
End If
End If
Next
If Not Have1 Then RndStr 0 '已全部消失
T = T + Timer1.Interval
If T 200 Then T = 0: Call ShowInf
End Sub
'本人原创,转载请注明来源:http://hi.baidu.com/100bd/blog/item/633b6467dbfdd828aa184cdb.html
参考:http://hi.baidu.com/100bd
求一个简单又有趣的JAVA小游戏代码
那你就自己做个猜数字好了
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
for (int i=0;i=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("输入的数大了,输小点的!");
else if (shunum)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你,猜对了!");
if (i=2)
System.out.println("你真是个天才!");
else if (i=6)
System.out.println("还将就,你过关了!");
else if (i=8)
System.out.println("但是你还……真笨!");
else
System.out.println("你和猪没有两样了!");
break;}
}
}
}