[Windows8.1 + dev-cpp5.11]
功能描述:
在开始编写一个程序之前,一定要先想好想要的功能,这个项目的描述大概是这样的:
这个游戏是一个外星人入侵的游戏。
玩家的飞船可以通过左右移动,发射炮弹以及使用全屏炸弹,来杀死敌人。
敌方飞船会不停的从屏幕顶端向下掉落,当掉落到底端时,减少一条命。玩家每次可以有三条命。
当玩家的命全部被扣光时,游戏结束。
准备工作:
两个重要函数:
void color(int a) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
这个函数用来设置文本颜色。
void gotoxy(int x, int y) {
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
这个函数用来设置光标的位置。
以上需要引入头文件<Windows.h>
编写代码:
编写一个类,用来控制玩家飞机的移动:
struct Plane {
int x = WIDTH / 2, y = HEIGHT;
char pic = OPIC;
void clear(bool left = false) {
gotoxy(x, y);
color(NCOLOR);
printf(" ");
if (left) {
gotoxy(x + 1, y);
color(NCOLOR);
printf(" ");
}
}
void blit() {
gotoxy(x, y);
color(OCOLOR);
printf("%c", this->pic);
}
};
其中clear()函数是用来清空,飞机当前所在位置。
blit()函数是用来在飞机当前位置绘制一个飞机。
读取玩家的指令是很重要的。
我们用<conio.h>中的getch()来获取玩家按下的按键。但是很不幸getch()会暂停游戏,直到玩家按下一个按键。
于是我们使用分线程来解决这个问题。
void check_event() {
while (programe_active) {
if (Event.size() < QUEUE_MAX_LEN) {
Event.push(getch());
}
}
return;
}
然后是子弹:
struct Bullet {
int x, y;
bool active = false;
char pic = BPIC;
void __init__(int x) {
if (!this->active) {
this->x = x;
this->y = HEIGHT;
this->active = true;
}
}
void clear() {
gotoxy(this->x, this->y);
color(NCOLOR);
printf(" ");
}
void stop() {
this->active = false;
this->clear();
}
void blit() {
if (this->active) {
gotoxy(this->x, this->y);
color(NCOLOR);
printf(" ");
if (this->y > 1) {
this->y--;
gotoxy(this->x, this->y);
color(OCOLOR);
printf("%c", pic);
} else {
this->stop();
}
}
}
};
Bullet bullet_group[MAX_BULLET];
还有敌方飞船:
struct Eship {
int x, y = 1;
bool active = false;
char pic = EPIC;
void __init__(int x) {
this->active = true;
this->x = x;
this->y = 1;
}
void clear() {
gotoxy(x, y);
color(NCOLOR);
printf(" ");
}
bool crash() {
if (this->active) {
for (int i = 0; i < MAX_BULLET; i++) {
if (bullet_group[i].x == this->x \
&& bullet_group[i].y == this->y \
&& bullet_group[i].active) {
this->active = false;
clear();
printf("\a");
return true;
}
}
}
return false;
}
bool Ewin() {
if (this->y > HEIGHT) {
programe_active = false;
}
}
void blit() {
if (this->active) {
this->clear();
this->y++;
gotoxy(this->x, this->y);
color(ECOLOR);
printf("%c", pic);
}
}
};
完整的代码就在这里给出来:
#include <conio.h>
#include <windows.h>
#include <stack>
#include <vector>
#include <thread>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <ctime>
#include <iostream>
#define BPIC '^'
#define OPIC 24
#define EPIC 25
#define MAX_BULLET 5
#define ONE_PLANE_SCORE 50
#define QUEUE_MAX_LEN 5
#define MAX_ESHIP 120
#define OCOLOR 2
#define NCOLOR 15
#define ECOLOR 9
#define WIDTH 50
#define HEIGHT 30
#define BWIDTH 72
#define LETTER_TIME 50
using namespace std;
typedef long long Score_n;
const string BG_COLOR = "0f";
const string LOST_MESSAGE = "You lost.\nAn Eship had landed on earth.\n";
bool programe_active = true;
queue<char> Event;
Score_n score = 0;
void gotoxy(int x, int y) {
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
struct Bullet {
int x, y;
bool active = false;
char pic = BPIC;
void __init__(int x) {
if (!this->active) {
this->x = x;
this->y = HEIGHT;
this->active = true;
}
}
void clear() {
gotoxy(this->x, this->y);
color(NCOLOR);
printf(" ");
}
void stop() {
this->active = false;
this->clear();
}
void blit() {
if (this->active) {
gotoxy(this->x, this->y);
color(NCOLOR);
printf(" ");
if (this->y > 1) {
this->y--;
gotoxy(this->x, this->y);
color(OCOLOR);
printf("%c", pic);
} else {
this->stop();
}
}
}
};
Bullet bullet_group[MAX_BULLET];
struct Eship {
int x, y = 1;
bool active = false;
char pic = EPIC;
void __init__(int x) {
this->active = true;
this->x = x;
this->y = 1;
}
void clear() {
gotoxy(x, y);
color(NCOLOR);
printf(" ");
}
bool crash() {
if (this->active) {
for (int i = 0; i < MAX_BULLET; i++) {
if (bullet_group[i].x == this->x \
&& bullet_group[i].y == this->y \
&& bullet_group[i].active) {
this->active = false;
clear();
printf("\a");
return true;
}
}
}
return false;
}
bool Ewin() {
if (this->y > HEIGHT) {
programe_active = false;
}
}
void blit() {
if (this->active) {
this->clear();
this->y++;
gotoxy(this->x, this->y);
color(ECOLOR);
printf("%c", pic);
}
}
};
struct Plane {
int x = WIDTH / 2, y = HEIGHT;
char pic = OPIC;
void clear(bool left = false) {
gotoxy(x, y);
color(NCOLOR);
printf(" ");
if (left) {
gotoxy(x + 1, y);
color(NCOLOR);
printf(" ");
}
}
void blit() {
gotoxy(x, y);
color(OCOLOR);
printf("%c", this->pic);
}
};
/*
void board() {
Sleep(100);
while (programe_active) {
gotoxy(WIDTH + 2, 1);
color(8);
printf("score: %d\n", score);
Sleep(500);
}
} */
void check_event() {
while (programe_active) {
if (Event.size() < QUEUE_MAX_LEN) {
Event.push(getch());
}
}
return;
}
void start_s() {
system("cls");
system("color 0f");
for (int i = 0; i < BWIDTH; i++) {
printf("-");
}
printf("\n");
for (int i = 0; i < HEIGHT; i++) {
printf("|");
for (int j = 0; j < WIDTH; j++) {
printf(" ");
}
printf(" |");
for (int j = 0; j < BWIDTH - WIDTH - 3; j++) {
printf(" ");
}
printf("|\n");
}
for (int i = 0; i < BWIDTH; i++) {
printf("-");
}
printf("\n");
}
void write(string m) {
for (int i = 0; i < m.size(); i++) {
printf("%c", m[i]);
Sleep(LETTER_TIME);
}
}
void gohint(char l) {
while (getch() != l);
}
void hints() {
write("hints?");
if (getch() == 'n') return;
system("cls");
write("Welcome to Ewar!");
Sleep(1200);
write("\nEarth is in danger.You need to protect it by damage Eships.");
Sleep(2400);
write("\nHere is a great new that only your ship can fire.");
Sleep(2400);
system("cls");
write("Press letter 'd' to