我的C++之旅—02
(原创)
以下是我编写的手速测试系统的源代码。
功能:
双人小游戏,测试你们的手速,看谁快谁先拿到90分的小游戏
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<ctime>
#include<iostream>
#include<graphics.h>
using namespace std;
int temp[9] = { 0,0,0,0,0,0,0,0,0 };
HWND hwnd = NULL;
struct Point {
int x;
int y;
};
struct count {
Point xy[9];
}c;
struct Role {
Point xy;
int score;
}r1,r2;
void initCount() {
int i, j, flag;
for (i = 0;i < 9;i++) {
flag = 0;
c.xy[i].x = (int)rand() % 61 * 10;
c.xy[i].y = (int)rand() % 61 * 10;
for (j = 0;j < i;j++) {
if (c.xy[j].x == c.xy[i].x && c.xy[i].y == c.xy[j].y) {
flag++;
}
}
if (flag != 0) {
i--;
}
}
}
void initRole() {
r1.xy.x = 10;
r1.xy.y = 10;
r2.xy.x = 590;
r2.xy.y = 10;
}
void drawRole() {
setlinecolor(RED);
setfillcolor(RED);
fillrectangle(r1.xy.x, r1.xy.y, r1.xy.x + 10, r1.xy.y + 10);
fillrectangle(r2.xy.x, r2.xy.y, r2.xy.x + 10, r2.xy.y + 10);
}
void drawCount() {
int i;
for (i = 0;i < 9;i++) {
if (temp[i] == 0) {
setlinecolor(BLUE);
setfillcolor(BLUE);
fillrectangle(c.xy[i].x, c.xy[i].y, c.xy[i].x + 10, c.xy[i].y + 10);
}
else if(temp[i] == 1){
setlinecolor(WHITE);
setfillcolor(WHITE);
fillrectangle(c.xy[i].x, c.xy[i].y, c.xy[i].x + 10, c.xy[i].y + 10);
}
}
}
void moveAction() {
char key;
key = _getch();
switch (key)
{
case 'W':
case 'w':
if (r1.xy.y > 0) {
r1.xy.y -= 10;
}
break;
case 72:
if (r2.xy.y > 0) {
r2.xy.y -= 10;
}
break;
case 'A':
case 'a':
if (r1.xy.x > 0) {
r1.xy.x -= 10;
}
break;
case 75:
if (r2.xy.x > 0) {
r2.xy.x -= 10;
}
break;
case 'D':
case 'd':
if (r1.xy.x < 600) {
r1.xy.x += 10;
}
break;
case 77:
if (r2.xy.x < 600) {
r2.xy.x += 10;
}
break;
case 'S':
case 's':
if (r1.xy.y < 600) {
r1.xy.y += 10;
}
break;
case 80:
if (r2.xy.y < 600) {
r2.xy.y += 10;
}
break;
default:
break;
}
}
int lessCount() {
char grade1[100] = { 0 };
char grade2[100] = { 0 };
sprintf(grade1, "%d", r1.score);
sprintf(grade2, "%d", r2.score);
setbkmode(TRANSPARENT);
settextcolor(RED);
outtextxy(10, 20, "玩家1分数:");
outtextxy(90, 20, grade1);
outtextxy(500, 20, "玩家2分数:");
outtextxy(580, 20, grade2);
int i;
for (i = 0;i < 9;i++) {
if ((c.xy[i].x == r1.xy.x && c.xy[i].y == r1.xy.y) && temp[i]==0) {
temp[i] = 1;
//setlinecolor(WHITE);
//setfillcolor(WHITE);
//fillrectangle(r1.xy.x, r1.xy.y, r1.xy.x + 10, r1.xy.y + 10);
r1.score += 10;
return 1;
}
else if ((c.xy[i].x == r2.xy.x && c.xy[i].y == r2.xy.y) && temp[i] == 0) {
temp[i] = 1;
//setlinecolor(WHITE);
//setfillcolor(WHITE);
//fillrectangle(r2.xy.x, r2.xy.y, r2.xy.x + 10, r2.xy.y + 10);
r2.score += 10;
return 1;
}
}
return 0;
}
int main() {
int flag;
srand((unsigned int)time(NULL));
hwnd = initgraph(610,610);
MessageBox(hwnd, "玩家 1 WSAD键移动---玩家 2 方向键移动", "游戏操作", 0);
MessageBox(hwnd, "玩家之间比较双方的手速,吃方块多、分数高获胜", "游戏目标", 0);
setbkcolor(WHITE);
cleardevice();
initRole();
initCount();
drawCount();
flag = 0;
while (1) {
cleardevice();
drawRole();
drawCount();
while (_kbhit()) {
moveAction();
}
if (lessCount()) {
flag++;
}
Sleep(100);
if (flag >= 9) {
break;
}
}
if (r1.score > r2.score) {
MessageBox(hwnd, "您完成了游戏!玩家 1 获胜", "游戏结束", 0);
}
if (r1.score < r2.score) {
MessageBox(hwnd, "完成了游戏!玩家 2 获胜", "游戏结束", 0);
}
closegraph();
system("pause");
return 0;
}