C++打字游戏
- 格式:doc
- 大小:121.00 KB
- 文档页数:6
计算机技术基础(C程序) 课程设计报告一.题目:简单打字游戏二、选题背景:在全球信息化高度发展的今天,计算机技术是现代人必须掌握的基本技术。
而一手精湛的打字技术更是我们自动化人所必备的资本。
本c语言程序的开发为广大计算机使用者提供了一个很好的平台。
三、设计思想:本程序主要应用了e l s e…i f语句、g o t o语句等函数。
建立打字练习的程序。
通过使用积分的方式,给使用者以直观的视觉冲击。
激励使用者的练习兴趣。
三、程序流程图:四:程序清单#include "conio.h"#include "stdlib.h"#include "graphics.h"main(){char a[36];int i,j,x,key,t,score=0;int gd=DETECT,gr;initgraph(&gd,&gr,"");cleardevice();for(i=0;i<10;i++)a[i]=48+i;for(i=10;i<36;i++)a[i]=55+i;a[i]=0;for(i=1;i<81;i++){gotoxy(i,3);printf("%c",220);gotoxy(i,18);printf("%c",220);}gotoxy(5,1);printf("score:");settextstyle(1,0,8);outtextxy(100,350,"TYPE GAME"); sleep(2);randomize();for(;;){gotoxy(12,1);printf(" ");gotoxy(12,1);printf("%d",score);i=rand()%80;if(i==0)i=1;x=rand()%36;t=0;for(j=4;!kbhit()&&j!=18;j++) {gotoxy(i,j);printf("%c",a[x]);gotoxy(i,j);if(score<10)delay(10000);else if(score<20)delay(7000);else if(score<30)delay(5000);else if(score<40)delay(4000);if(score==40){clrscr();textmode(C40);delay(100000);gotoxy(18,12);printf("YOU WIN!");delay(100000);exit(1);}gotoxy(i,j);printf(" ");gotoxy(i,j);}if(j==18){t=1;score--;}if(t==1)continue;key=bioskey(0);if(key==0x011b)break;else if(key==0x231&&a[x]==49) {score++;continue;}else if(key==0x332&&a[x]==50) {score++;continue;}else if(key==0x433&&a[x]==51) {score++;continue;}else if(key==0x534&&a[x]==52) {score++;continue;}else if(key==0x635&&a[x]==53) {score++;continue;}else if(key==0x736&&a[x]==54) {score++;continue;}else if(key==0x837&&a[x]==55) {score++;continue;}else if(key==0x938&&a[x]==56) {score++;continue;}else if(key==0x939&&a[x]==57) {score++;continue;}else if(key==0xb30&&a[x]==48) {score++;continue;}else if(key==0x1e61&&a[x]==65) {score++;continue;}else if(key==0x3062&&a[x]==66) {score++;continue;}else if(key==0x2e63&&a[x]==67) {score++;continue;}else if(key==0x2064&&a[x]==68) {score++;continue;}else if(key==0x1265&&a[x]==69) {score++;continue;}else if(key==0x2166&&a[x]==70) {score++;continue;}else if(key==0x2267&&a[x]==71) {score++;continue;}else if(key==0x2368&&a[x]==72) {score++;continue;}else if(key==0x1769&&a[x]==73) {score++;continue;}else if(key==0x246a&&a[x]==74) {score++;continue;}else if(key==0x256b&&a[x]==75) {score++;continue;}else if(key==0x266c&&a[x]==76) {score++;continue;}else if(key==0x326d&&a[x]==77) {score++;continue;}else if(key==0x316e&&a[x]==78) {score++;continue;}else if(key==0x186f&&a[x]==79) {score++;continue;}else if(key==0x1970&&a[x]==80) {score++;continue;}else if(key==0x1071&&a[x]==81) {score++;continue;}else if(key==0x1372&&a[x]==82) {score++;continue;}else if(key==0x1f73&&a[x]==83){score++;continue;}else if(key==0x1474&&a[x]==84) {score++;continue;}else if(key==0x1675&&a[x]==85) {score++;continue;}else if(key==0x2f76&&a[x]==86) {score++;continue;}else if(key==0x1177&&a[x]==87) {score++;continue;}else if(key==0x2d78&&a[x]==88) {score++;continue;}else if(key==0x1579&&a[x]==89) {score++;continue;}else if(key==0x2c7a&&a[x]==90) {score++;continue;}else{score--;continue;}}cleardevice();outtextxy(100,200,"GAME OVER");getch();closegraph();}五、主要问题的解决方法及技术关键1、主要问题的解决方法:用“graphics.h”库存函数实现图形的初始化绘制。
C++学习之-C语⾔实现打字游戏C++学习之-C语⾔实现打字游戏作者介绍张伟伟,男,西安⼯程⼤学电⼦信息学院,2019级硕⼠研究⽣,张宏伟⼈⼯智能课题组。
研究⽅向:机器视觉与⼈⼯智能。
电⼦邮件:2430290933@课题组CSDN官⽅账号,欢迎⼀键三连: .学习的主要内容实现⼀个打字游戏,⽤户与终端交互,输⼊字符进⾏⽐较,输出正确率和时间。
程序编译完成后使⽤GBK编码格式(因windows默认)保存即可正常在终端输出中⽂字符。
#include<stdio.h>#include<string.h>#include<conio.h>//提⽰,vs提供的,windows没有这个库#include<stdlib.h>#include<time.h>void tips(){printf("\n\n==================伟伟版打字游戏==================\n\n");printf("===============按任意键继续================\n");printf("==============按Esc 退出游戏/打字中退出游戏================\n");//接受但不显⽰字符char ch =_getch();//按Esc就退出if(ch ==27){exit(0);}}//产⽣随机字符void rand_ch(char*arr){//随机数种⼦srand((unsigned int)time(NULL));for(int i =0; i <10; i++){arr[i]=rand()%26+'a';}}void printf_ch(char*arr){//计数器和计时器unsigned int start_time;unsigned int end_time;int val =0;for(int i =0; i <10; i++){char ch =_getch();//此处判断是否重新开始调⽤循环if(i ==0)if(i ==0){start_time =time(NULL);}if(ch == arr[i]){printf("%c",ch);val++;}else{printf("_");}//若⽤户想在此过程提前退出:按ESC键if(ch ==27){printf("\n您已提前退出游戏");exit(0);}}end_time =time(NULL);printf("\n====⽤时:%d",end_time - start_time); printf("\n====正确率:%.2f%%",val*1.0/10*100); if((val*1.0/10*100)>60){printf("\n===你太棒了====");}}int main(void){//字库char arr[51];//arr所指向内存的前51个位置赋值为0memset(arr,0,11);while(1){//1.提⽰tips();//2.产⽣随机字符rand_ch(arr);printf("%s\n",arr);//3.输⼊字符printf_ch(arr);}return EXIT_SUCCESS;}⼀起学习,欢迎关注和交流!。
#include <stdio.h>#include <stdlib.h>#include<windows.h>#include<conio.h>#include<time.h>#define NN 20#define MM 65#include<process.h>char a[NN][MM],c;int b[11]={0},N/*jiaohuan*/,i,j/*jishu*/,t/*dajishu*/,k=0,u=0,m,p,q,n,M;double s;void gamebegin();void levelselect();void sure();void former();void gaming();void gameend();void gamebegin(){printf("本游戏为了方便中国玩家,在难懂部分用中文或双语书写\n");printf("welcome to the typing game, do you want to test your typing skill?\n1 yes,i want to try it 2 no,thanks\n");printf("please write down the number which points at your option\n****************************************************\n(since then you should make your decision in this way)\n");do{scanf("%d",&b[0]);switch(b[0]){case 1:levelselect();b[1]=1;break;case 2:exit(0);b[1]=1;break;default:printf("please select again\n");}}while(b[1]==0);}void levelselect(){system("cls");printf("please choose your game level 1\n");printf("1 easy 2 medium 3 difficult\n");do{scanf("%d",&b[2]);switch(b[2]){case 1:N=2000;b[3]=1;break;case 2:N=1000;b[3]=1;break;case 3:N=800;b[3]=1;break;default:printf("please select again\n");}}while(b[3]==0);system("cls");printf("please choose your game level 2\n");printf("1 lowercase小写字母2 lowercase+capital大写加小写3 lowercase+capital+number 大写加小写加数字\n");printf("*****************************************************************\n^.^the punctuation will take up among all the three level,so it is a challenge to you.GO for it!\n符号在三个等级中随机出现\n");do{scanf("%d",&b[4]);switch(b[4]){case 1:p=30;q=97;b[5]=1;break;case 2:p=62;q=97;b[5]=1;break;case 3:p=94;q=97;b[5]=1;break;default:printf("please select again\n");}}while(b[5]==0);system("cls");printf("please write down the number of letter you want to hit\n为了计算精确,请输入三的倍数,为了时间的精准,请让字母的个数大于60\n");scanf("%d",&b[6]);}void sure(){system("cls");printf("please affirm your answer\n");printf("your answer is:the level 1 is number %d,the level 2 is %d, there are %d letters to run the game\n1 yes 2 no,i will select again\n",b[2],b[4],b[6]);do{scanf("%d",&b[7]);{case 1:gaming();b[9]=1;break;case 2:former();b[9]=1;break;default:printf("please select again\n");}}while(b[9]==0);}void former(){gamebegin();sure();}void gaming(){printf("game introduction:there will be many letter to drop down,so you should hit it!come on!");system("cls");printf("please input arbitrary letter to begin the game\n");getch();for(i=0;i<NN;i++){for(j=0;j<MM;j++){a[i][j]=32;}}while(M<(b[6]/3+NN-1)){M++;if(k<b[6]){srand((unsigned)time(NULL));a[u][rand()%(MM-1)]=(rand()%p+q);a[u][rand()%(MM-1)]=(rand()%p+q);a[u][rand()%(MM-1)]=(rand()%p+q);k=k+3;}Sleep(N);system("cls");{c=getch();for(i=0;i<NN;i++){for(j=0;j<MM-1;j++){if(c==27){exit(1);}else if(c==a[i][j]){putchar('\a');a[i][j]=32;t++;}}}}for(i=0;i<NN;i++){a[i][MM-1]='\0';}for(m=u++;m>=0;m--)printf("%s\n",a[m]);for(m=NN-1;m>=u;m--)printf("%s\n",a[m]);if(u==NN)u=0;for(j=0;j<MM-1;j++){a[u][j]=32;}}}void gameend(){system("cls");printf("我的游戏还有很大纰漏。
本文将给出一个使用C语言编写的简单的文本冒险游戏的示例。
这个游戏的玩法是玩家在不同的房间中走动,并在每个房间中寻找物品。
在每个房间中,玩家可以输入命令来查看当前房间的情况、捡起物品或移动到其他房间。
首先,我们需要定义几个结构体来表示房间、物品和玩家。
struct Room {char* description;struct Room* north;struct Room* south;struct Room* east;struct Room* west;struct Item* items;};struct Item {char* description;struct Item* next;};struct Player {struct Room* current_room;struct Item* inventory;};然后我们需要定义一些函数来创建房间、物品和玩家,以及处理玩家的命令。
struct Room* create_room(char* description) {struct Room* room = malloc(sizeof(struct Room));room->description = description;room->north = NULL;room->south = NULL;room->east = NULL;room->west = NULL;room->items = NULL;return room;}struct Item* create_item(char* description) {struct Item* item = malloc(sizeof(struct Item));item->description = description;item->next = NULL;return item;}struct Player* create_player(struct Room* starting_room) {struct Player* player = malloc(sizeof(struct Player));player->current_room = starting_room;player->inventory = NULL;return player;}void free_room(struct Room* room) {// 释放房间中的物品struct Itemvoid free_room(struct Room* room) {// 释放房间中的物品struct Item* item = room->items;while (item != NULL) {struct Item* next = item->next;free(item);item = next;}// 释放房间本身free(room);}void free_item(struct Item* item) {free(item);}void free_player(struct Player* player) {// 释放玩家的物品struct Item* item = player->inventory;while (item != NULL) {struct Item* next = item->next;free(item);item = next;}// 释放玩家本身free(player);}void print_room(struct Room* room) {printf("%s\n", room->description);printf("There is a door to the north, south, east, and west.\n"); printf("There is also the following items here:\n");struct Item* item = room->items;while (item != NULL) {printf("- %s\n", item->description);item = item->next;}}void print_inventory(struct Player* player) {printf("You have the following items:\n");struct Item* item = player->inventory;while (item != NULL) {printf("- %s\n", item->description);item = item->next;}}void execute_command(struct Player* player, char* command) { if (strcmp(command, "north") == 0) {if (player->current_room->north != NULL) {player->current_room = player->current_room->north;printf("You go north.\n");} else {printf("There is no door to the north.\n");}} else if (strcmp(command, "south") == 0) {if (player->current_room->south != NULL) {player->current_room = player->current_room->south;printf("You go south.\n");} else {printf("There is no door to the south.\n");}} else if (strcmp(command, "east") == 0) {if (player->current_room->east != NULL) {player->current_room = player->current_room->east;printf("You go east.\n");} else {printf("There is no door to the east.\n");}} else if (strcmp(command, "west") == 0) {if (player->current_room->west != NULL) {player->current_room = player->current_room->west;printf("You go west.\n");}else if (strcmp(command, "west") == 0) {if (player->current_room->west != NULL) {player->current_room = player->current_room->west;printf("You go west.\n");} else {printf("There is no door to the west.\n");}} else if (strcmp(command, "look") == 0) {print_room(player->current_room);} else if (strcmp(command, "inventory") == 0) {print_inventory(player);} else if (strncmp(command, "pickup ", 7) == 0) {// 检查玩家是否正在尝试捡起房间中的物品char* item_name = command + 7;struct Item* item = player->current_room->items;while (item != NULL && strcmp(item->description, item_name) != 0) { item = item->next;}if (item == NULL) {printf("There is no item with that name in the room.\n");} else {// 从房间中删除物品if (player->current_room->items == item) {player->current_room->items = item->next;} else {struct Item* previous = player->current_room->items;while (previous->next != item) {previous = previous->next;}previous->next = item->next;}// 将物品添加到玩家的物品列表中item->next = player->inventory;player->inventory = item;printf("You pick up the %s.\n", item->description);}} else {printf("I don't understand that command.\n");}}else if (strcmp(command, "west") == 0) {if (player->current_room->west != NULL) {player->current_room = player->current_room->west;printf("You go west.\n");} else {printf("There is no door to the west.\n");}} else if (strcmp(command, "look") == 0) {print_room(player->current_room);} else if (strcmp(command, "inventory") == 0) {print_inventory(player);} else if (strncmp(command, "pickup ", 7) == 0) {// 检查玩家是否正在尝试捡起房间中的物品char* item_name = command + 7;struct Item* item = player->current_room->items;while (item != NULL && strcmp(item->description, item_name) != 0) {item = item->next;}if (item == NULL) {printf("There is no item with that name in the room.\n");} else {// 从房间中删除物品if (player->current_room->items == item) {player->current_room->items = item->next;} else {struct Item* previous = player->current_room->items;while (previous->next != item) {previous = previous->next;}previous->next = item->next;}// 将物品添加到玩家的物品列表中item->next = player->inventory;player->inventory = item;printf("You pick up the %s.\n", item->description);}} else {printf("I don't understand that command.\n");}}最后,我们可以编写一个main函数来创建房间、物品和玩家,并进入游戏循环,在每一次迭代中读取玩家输入并执行命令。
《C++课程设计实验报告》姓名: XXX学号: XXXXXXX班级: XXXXXX指导教师: XX完成日期: XXXX年X月X日目录一、问题简述 (2)二、需求分析及系统设计 (2)(一)需求分析 (2)(二)系统设计 (2)三、程序测试结果 (4)四、问题及解决方案和收获与心得 (7)(一)问题及解决方案 (7)(二)收获与心得 (7)五、程序源代码 (8)一、问题简述本题目(参考题五)要求设计并实现一个打字游戏,游戏要求如下:游戏开始后,屏幕上方同时有四个数字分四列下落,每个数字由三位组成(随机生成,但首位不可相同)。
玩家用键盘依次输入数字,如果输入的数字序列与某一列正在下落的数字三位全部相同,则给玩家加10分,该列数字消失,该列新产生数字并下落;如果输入的数字某一位不相同,则输入的数字序列全部作废,重新接收输入。
某一列数字到达屏幕底部则游戏结束。
对于本题目我采用C++进行设计并实现。
二、需求分析及系统设计(一)需求分析需要设计的这个游戏程序,其规则在上述问题简述中已经进行了详细描述。
其中有几个方面是这个程序所需要的主要模块:(1)生成四组随机的三位数,并且所生成的四组随机数要求首位不相同;(2)分析并选取合适的方式来存储这些随机数;(3)所有显示出的数能够按照一定频率向下进行滚动;(4)键盘输入一组数字,并在输入匹配的情况下消除与之匹配的数,然后产生一个不同于其他三组的新随机数;(5)判断游戏结束的标准并输出分数。
(二)系统设计通过分析,选择二维数组作为存储并处理这些随机数的容器,所有主要步骤都在这个二维数组中设计并实现;在随机数的生成上选择以当前系统时间为随机种子来生成所需要的四组随机数;该程序包含的模块:class GRN -(该类负责生成随机数 Generate Random Number)void generate()-(生成随机数)int RN()-(将生成的随机数返回n)class Run -(该类负责实现程序的其他主要功能)void initialize()-(对数组以及各类参数进行初始化)void importing()-(将生成的符合要求的随机数导入到数组中)void show()-(显示游戏运行过程)void judge()-(判断所输入的数是否满足消去条件并进行记分)void act(int t)-(执行,消去满足条件的数并在该列顶端生成新随机数)系统流程图:三、程序测试结果(OS:Windows 8 编译:VS2012)1.游戏程序开始执行界面2.输入数字每次移动的时间间隔(单位ms)3.游戏开始运行4.游戏进行中5.游戏结束6.经过测试,游戏能够正常运行并且所得分数正确。
c打字游戏课程设计报告一、课程目标知识目标:1. 学生能够掌握C语言基础语法,包括变量声明、数据类型、运算符等。
2. 学生能够理解并运用循环结构、条件语句编写简单的打字游戏程序。
3. 学生了解基本的计算机编码知识,如ASCII码。
技能目标:1. 学生能够运用C语言编写简单的打字游戏程序,提高编程实践能力。
2. 学生能够通过编写代码解决问题,培养逻辑思维和问题解决能力。
3. 学生能够熟练使用计算机键盘,提高打字速度和准确性。
情感态度价值观目标:1. 学生培养对编程的兴趣,激发学习C语言的热情。
2. 学生在团队协作中,培养沟通、协作能力,增强团队意识。
3. 学生通过编程实践,体会计算机科学的魅力,树立正确的科技观。
课程性质:本课程为实践性较强的编程课程,结合打字游戏的形式,让学生在轻松愉快的氛围中学习C语言编程。
学生特点:五年级学生,具备一定的计算机操作能力,对游戏有浓厚兴趣,具备初步的逻辑思维能力。
教学要求:教师应注重引导学生主动探究、实践,关注学生的个体差异,鼓励学生发挥创意,提高编程能力和团队协作能力。
通过本课程的学习,使学生达到上述具体的学习成果。
二、教学内容1. C语言基础语法:- 变量声明、数据类型(整型、字符型)- 常量和变量- 运算符(算术、关系、逻辑)- 语法结构(顺序、选择、循环)2. 编程实践:- 打字游戏程序设计思路- 设计简单循环结构,实现打字游戏基本框架- 引入随机函数,生成随机字母或单词- 使用条件语句,判断用户输入与随机字母的匹配3. 计算机编码知识:- ASCII码介绍- 键盘布局与打字技巧4. 教学内容的安排和进度:- 第一课时:C语言基础语法回顾,介绍打字游戏目标- 第二课时:设计打字游戏基本框架,学习循环结构- 第三课时:引入随机函数,学习条件语句- 第四课时:完善打字游戏功能,进行编程实践- 第五课时:总结与展示,讨论编程过程中的问题及解决方法教材关联:- 《C语言趣味编程》第三章:循环结构- 《C语言趣味编程》第四章:分支结构- 《计算机科学》第二单元:计算机编码知识教学内容注重科学性和系统性,结合教材相关章节,引导学生通过实践掌握C 语言编程知识,提高编程技能。
简单的猜数字游戏(C语⾔)猜数字游戏1.系统分析在本游戏中,电脑会给出1~100之间的⼀个随机数,玩家通过键盘输⼊猜测的数字,电脑会判断输⼊的数字是否为⽣成的随机数,并做出相应的提⽰(猜⼤了,猜⼩了,答对了)。
2.概要设计此系统可分为以下⼏个功能模块:菜单模块;随机数⽣成模块;判断及提⽰模块;主函数模块。
3.详细设计3.1 菜单模块void menu(){printf("********************\n");printf("** 1.play 0.exit **\n");printf("********************\n");}3.2 随机数⽣成模块ret=rand()%100+1;//⽣成1~100之间的随机数srand((unsigned int)time(NULL));//拿时间戳来设置随机数的⽣成起始点3.3 判断及提⽰模块while(1){printf("请猜数字:");scanf("%d",&guess);if(guess>ret)printf("猜⼤了\n");else if(guess<ret)printf("猜⼩了\n");else{printf("恭喜你,猜对了!\n");break;}}3.4 主函数模块int main(){int input;srand((unsigned int)time(NULL));//拿时间戳来设置随机数的⽣成起始点do{menu();printf("请选择:");scanf("%d",&input);switch(input){case1:game();break;case0:printf("退出游戏\n");break;default:printf("选择错误\n");break;}}while(input);return0;}4.源程序代码#include<stdio.h>#include<time.h>//time()#include<stdlib.h>//rand();srand()void menu(){printf("********************\n");printf("** 1.play 0.exit **\n");printf("********************\n");}void game(){int ret;int guess;ret=rand()%100+1;//⽣成1~100之间的随机数while(1){printf("请猜数字:");scanf("%d",&guess);if(guess>ret)printf("猜⼤了\n");else if(guess<ret)printf("猜⼩了\n");else{printf("恭喜你,猜对了!\n");break;}}}int main(){int input;srand((unsigned int)time(NULL));//拿时间戳来设置随机数的⽣成起始点do{menu();printf("请选择:");scanf("%d",&input);switch(input){case1:game();break;case0:printf("退出游戏\n");break;default:printf("选择错误\n");break;}}while(input);return0;}。
C#实现简单打字游戏本⽂实例为⼤家分享了C#实现简单打字游戏的具体代码,供⼤家参考,具体内容如下运⾏效果图如下:功能:程序运⾏后,点击开始按钮,窗体中的⽂本框中出现字母,⽤户通过键盘输⼊⽂本框中字母,窗体显⽰⽤时、正确数、错误数和正确率。
按钮:开始、结束、退出。
菜单:设置(开始游戏、结束游戏、退出游戏),查看(正确率、所⽤时间)。
页⾯:控件属性:timer1:enabled选择false,Interval设置为5.timer2:enabled选择false,Interval设置为1000.代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication3{public partial class Form1 : Form{public Form1(){InitializeComponent();}private int x = 200, y, num;private DateTime dt1, dt2;private int count_all = 0;private int count_correct = 0;private TimeSpan ts;Random rd = new Random();private void btnStart_Click(object sender, EventArgs e){tsmiRate.Enabled = true;//启⽤控件dt1 = DateTime.Now;timer1.Start();timer2.Start();textBox1.Visible = true;num = rd.Next(65, 90);}private void btnStop_Click(object sender, EventArgs e){tsmiTime.Enabled = true;dt2 = DateTime.Now;timer1.Stop();timer2.Stop();textBox1.Visible = false;MessageBox.Show("游戏结束。
C#实现打字游戏本⽂实例为⼤家分享了C#实现打字游戏的具体代码,供⼤家参考,具体内容如下思路:1、有⼀个游戏界⾯,我⽤panel作为游戏界⾯2、开始⽣成字母打字游戏的字母是不断⽣成的,所以⽤计时器timer来⽣成字母所有⽣成的字母设置tag⽅便寻找3、字母下落字母下落是⼀个持续的动作,所以也在计时器⾥做在计时器⾥通过foreach遍历panel中的所有控件,同时通过tag找到字母,让字母下降4、⽣成⼦弹通过获取键盘事件⽣成⼦弹5、⼦弹与字母相碰代码:private void Form1_Load(object sender, EventArgs e){this.panel1.BackColor = Color.White;timer1.Start();timer2.Start();timer1.Interval = 1000;timer2.Interval = 100;fj.Tag = "feiji";fj.Size = new Size(30, 40);fj.BackColor = Color.Black;fj.Text = "飞机";fj.TextAlign = ContentAlignment.MiddleCenter;fj.ForeColor = Color.White;fj.Location = new Point(panel1.Width / 2 - fj.Width / 2, panel1.Height - fj.Height);panel1.Controls.Add(fj);}Label fj = new Label();Random r = new Random();private void timer1_Tick(object sender, EventArgs e){Label zm = new Label();zm.Tag = "zimu";zm.Text = ((char)r.Next(97, 123)).ToString();zm.Font = new Font("", r.Next(20, 30));zm.AutoSize = true;zm.Location = new Point(r.Next(0, panel1.Width - zm.Width), 0);zm.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));panel1.Controls.Add(zm);}private void timer2_Tick(object sender, EventArgs e){foreach (Control item in panel1.Controls){if (item.Tag.ToString() == "zimu"||item.Tag.ToString()=="zzm"){item.Top += 5;if (item.Top >= panel1.Height){item.Dispose();}}else if (item.Tag.ToString() == "zidan"){item.Top -= 9;foreach (Control con in panel1.Controls){if (con.Tag.ToString() == "zzm"){if (con.Top + con.Height >= item.Top){con.Dispose();item.Dispose();SoundPlayer ply = new SoundPlayer();ply.SoundLocation = ".../.../Sound/MyBomb.wav";ply.Play();}}}}}}private void Form1_KeyPress(object sender, KeyPressEventArgs e){foreach (Control item in panel1.Controls){if (item.Tag.ToString() == "zimu"){if (item.Text == e.KeyChar.ToString()){Label zd = new Label();zd.Tag = "zidan";zd.Size = new Size(20, 20);item.Tag = "zzm";zd.BackColor = Color.Red;zd.Location = new Point(item.Left + item.Width / 2 - zd.Width / 2, fj.Top - fj.Height);fj.Left = item.Left + item.Width / 2 - fj.Width / 2;panel1.Controls.Add(zd);return;}}}}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
C# 打字游戏程序核心代码主要部分:设计程序流程图:主窗体代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;//课程名称 C# 程序设计实践//设计题目打字游戏//专业计算机科学与技术//班级//学号//姓名//指导老师namespace Typing_Game{public partial class FrmTyping : Form {public FrmTyping(){InitializeComponent();public int right = 0; //正确个数计数public int wrong = 0; //错误个数计数public int sum = 0; //总个数技术public int count = 0; //按键总次数public float accuracy; //正确率public int Minute = 1; //设置默认没局游戏时间public int minute; //设置游戏中的倒计时分public int second; //设置游戏中的倒计时秒private void FrmTyping_Load(object sender, EventArgs e){}//在状态栏中显示当前系统时间private void timerSys_Tick(object sender, EventArgs e){DateTime now = DateTime.Now;this.tsslSysTime.Text = "系统时间: " + now.Hour.ToString() + ":" + now.Minute.ToString() + ":" + now.Second.ToString()+'\t';}//开始&结束按钮private void tsmiStratOrOver_Click(object sender, EventArgs e){if (tsmiStratOrOver.Text == "开始"){tsmiStratOrOver.Text = "结束";timerWord.Start();timerGame.Start();timerGT.Start();//在游戏开始时暂停&继续按钮可用tsmiPauseOrContinue.Enabled = true;//在游戏未开始时选择游戏等级与设置游戏按钮无效tsmiLevel.Enabled = false;tsmiOption.Enabled = false;//游戏开始初始化游戏数据right = 0;wrong = 0;sum = 0;count = 0;minute = Minute;second = 0;}else{tsmiStratOrOver.Text = "开始";//在游戏未开始时暂停&继续按钮无效tsmiPauseOrContinue.Enabled = false;//在游戏未开始时可以选择游戏等级与设置游戏tsmiLevel.Enabled = true;tsmiOption.Enabled = true;frmResult frm = new frmResult(this);frm.Show();}}//游戏暂停&继续功能实现private void tsmiPauseOrContinue_Click(object sender, EventArgs e) {if (tsmiPauseOrContinue.Text == "暂停"){tsmiPauseOrContinue.Text = "继续";timerWord.Enabled = false;timerGame.Enabled = false;timerGT.Enabled = false;}else{tsmiPauseOrContinue.Text = "暂停";timerWord.Enabled = true;timerGame.Enabled = true;timerGT.Enabled = true;}}//退出程序private void tsmiExit_Click(object sender, EventArgs e){Application.Exit();}//游戏等级为初级时private void tsmiLevelPrimary_Click(object sender, EventArgs e) {tsmiLevelPrimary.Checked = true;tsmiLevelMidder.Checked = false;tsmiLevelHigh.Checked = false;timerGame.Interval = 180;timerWord.Interval = 1800;}//游戏等级为中级时private void tsmiLevelMidder_Click(object sender, EventArgs e) {tsmiLevelPrimary.Checked = false;tsmiLevelMidder.Checked = true;tsmiLevelHigh.Checked = false;timerGame.Interval = 90;timerWord.Interval = 900;}//游戏等级为高级时private void tsmiLevelHigh_Click(object sender, EventArgs e){tsmiLevelPrimary.Checked = false;tsmiLevelMidder.Checked = false;tsmiLevelHigh.Checked = true;timerGame.Interval = 50;timerWord.Interval = 500;}//跳出设置每局时间窗体private void tsmiSetTime_Click(object sender, EventArgs e){frmSetTime frm = new frmSetTime(this);frm.Show();}//跳出关于窗体private void tsmiAbout_Click(object sender, EventArgs e){About frm = new About();frm.Show();}//对打字游戏中字母的掉落private void timerGame_Tick(object sender, EventArgs e){foreach (Control con in this.Controls){if (con is Label){if (con.Top >= this.Height-51){con.Dispose(); //当label掉落到窗体边框 label消失}elsecon.Top += 5;}}}//对打字游戏开始时不断随机生成的字母添加到新生成的label中并设置label的基本属性private void timerWord_Tick(object sender, EventArgs e){Random r = new Random();int a = r.Next(65, 90); //随机生成A~Z的ASCII码char c = Convert.ToChar(a); //ASCII转换成字母Label lalChar = new Label(); //生成新LabellalChar.Text = c.ToString(); //字母放入//设置Label基本属性固定大小字体居中对齐等等...lalChar.AutoSize = false;lalChar.Height = 51;lalChar.Width = 51;lalChar.Left = r.Next(r.Next( 0, this.Width/5-25),r.Next(this.Width-100,this.Width - 51));lalChar.Font = new Font("楷体", 15, FontStyle.Regular);lalChar.TextAlign = ContentAlignment.MiddleCenter;//将Label加入到Controls中this.Controls.Add(lalChar);//每生成一个Label 计入字母总数中sum++;//每生成一个Label都重新计算一次正确率accuracy = (float)right / (2 * sum) + (float)right / (2 * count);//状态栏中实时更新数据统计tsslSum.Text = "总个数为 :" + sum.ToString();tsslAccuracy.Text = "正确率为: " + (accuracy * 100).ToString() + "%";}//状态栏中的倒计时算法private void timerGT_Tick(object sender, EventArgs e){if (minute == 0 && second == 0){frmResult frm = new frmResult(this);frm.Show();}else if (second == 0){minute -= 1;second = 59;}elsesecond -= 1;//实时更新倒计时数据tsslGameTime.Text = "游戏剩余时间: "+ minute.ToString() + ":"+ second.ToString(); }//键盘输入对应的正确或错误的处理private void FrmTyping_KeyPress(object sender, KeyPressEventArgs e){//每按下一个按键加入输入总数的计数count++;//bool t的值来判定输入的对错bool t = false;string s = e.KeyChar.ToString().ToUpper();foreach (Control con in this.Controls){if (con is Label){if (con.Text==s){t = true;right++;con.Dispose();break;}}}if (t == false)wrong++;//实时更新状态栏的数据tsslRightNo.Text = "正确个数: " + right.ToString();tsslWrongNo.Text = "错误个数: " + wrong.ToString();accuracy = (float)right / (2 * sum) + (float)right / (2 * count);tsslAccuracy.Text = "正确率为: " + (accuracy * 100).ToString() + "%";}}}时间设置窗体代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Typing_Game{public partial class frmSetTime : Form{FrmTyping frm;public frmSetTime(FrmTyping frm){InitializeComponent();this.frm = frm;}//当窗体跳出时 textbox默认显示当前游戏的每局时间private void frmSetTime_Load(object sender, EventArgs e){textBox1.Text = frm.Minute.ToString();}//当textbox中内容发生变化时,对textbox中的内容做出判断如果不符合要求 label显示提示错误的输入。