-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (107 loc) · 3.27 KB
/
main.cpp
File metadata and controls
114 lines (107 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 打字练习系统.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <graphics.h>
#include<stdio.h>
#include<time.h>
#include<conio.h>//按键
#include<mmsystem.h>//多媒体音频接口
#include<Windows.h>
#pragma comment(lib,"winmm.lib")
HWND hwnd;
void Welcome() {
//欢迎界面
initgraph(600,700);
loadimage(NULL, "back.png", 600, 700);
//设置背景模式 透明
setbkmode(0);
settextcolor(WHITE);
settextstyle(50, 0, "微软雅黑");
outtextxy(160, 50, "爱爱打字练习系统");
settextcolor(WHITE);
settextstyle(30, 0, "微软雅黑");
outtextxy(350, 500, "按回车键开始游戏");
outtextxy(350, 550, "欢迎进入打字练习系统");
getchar();
}
void PlayGame() {
cleardevice();//清屏
char MyChar;
int MyCharX, MyCharY;
char GetKey;//获取按键值
int error = 0;
int Score = 0;
char Str_error[128];
char Str_Score[128];
hwnd = GetHWnd();//获取窗口句柄
while (1) {
//printf("错误次数:%d,总计分数:%d\n", error, Score);
MyChar = rand() % 26 + 65;
MyCharX = rand() % 550;
sprintf(Str_Score, "得分:%d",Score);
sprintf(Str_error, "失误:%d", error);
for (MyCharY = 100; MyCharY <= 700; MyCharY++) {
cleardevice();//清屏
outtextxy(0, 0, Str_Score);
outtextxy(0, 30, Str_error);
outtextxy(MyCharX, MyCharY, MyChar);
Sleep(1);
if (kbhit()) {//判断有没有按键
GetKey = getch();//从键盘接收字母 字符,存储到getkey
//A~Z||a~z
if (GetKey == MyChar || GetKey - 32 == MyChar) {
Score += 1;
break;//退出循环,消除字符
}
else {//按错了
/* MessageBox(hwnd, "哎呀,宝宝手滑了!", "Game Over", MB_OK);
exit(0);*/
error = error + 1;
break;
}
}
}
if (MyCharY > 700) {
/* MessageBox(hwnd, "哎呀,宝宝没来得及点!", "Game Over", MB_OK);
exit(0);*/
error = error + 1;
break;
}
if (error >= 5) {
MessageBox(hwnd, "哎呀,游戏失败了", "Game Over", MB_OK);
exit(0);
}
}
getchar();
}
int main()
{
srand((unsigned)time(NULL));
Welcome();
//往多媒体发送指令
mciSendString("play willow.wav repeat", 0, 0, 0);
PlayGame();
getchar();
return 0;
}
/*
贴图三部曲:
1.定义变量 int val
2.赋值 scanf("%d",&val)
3.显示打印使用 printf("%d\n",val)
*/
//IMAGE img;
//loadimage(&img,"back.png",700,800);
//putimage(0, 0, &img);
//loadimage(NULL, "back.png", 700, 800);
//设置文字颜色 RGB(red,green,blue)
//settextcolor(WHITE);
//字体风格
//settextstyle(50, 0, "华文彩云");
//插入文字
//outtextxy(0, 0, "我系爱爱呀");
////把设计作为随机数的种子
//srand((unsigned)time(NULL));
////随机产生字母A~Z,
//char val = rand()%26+65;
//int MyCharX = rand() % 550;
//printf("val=%c\n", val);