打地鼠游戏设计 C++ 代码实现
打地鼠游戏是一种经典的休闲游戏,玩家需要用鼠标点击冒出头的地鼠,每击中一个地
鼠得一分,游戏结束时,得分越高越好。下面是一个简单的打地鼠游戏设计 C++ 代码
实现。
c
复制代码
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int WINDOW_WIDTH = 40;
const int WINDOW_HEIGHT = 15;
const int MOLE_SPEED = 500;
const int MOLE_SCORE = 10;
int mole_x[2] = {-1, -1};
int mole_y[2] = {-1, -1};
int score = 0;
int delay = MOLE_SPEED;
void init() {
score = 0;
delay = MOLE_SPEED;
mole_x[0] = rand() % (WINDOW_WIDTH - 2) + 1;
mole_y[0] = rand() % (WINDOW_HEIGHT - 2) + 1;
mole_x[1] = (mole_x[0] > WINDOW_WIDTH / 2) ? mole_x[0] - 1 : mole_x[0]
+ 1;
mole_y[1] = mole_y[0];
}
void draw() {
system("cls");
for (int i = 0; i < WINDOW_WIDTH; i++) {
for (int j = 0; j < WINDOW_HEIGHT; j++) {
if (i == mole_x[0] && j == mole_y[0]) {
cout << "O";
} else if (i == mole_x[1] && j == mole_y[1]) {