#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int screenWidth = 60;
const int screenHeight = 20;
const int playerWidth = 5;
const int playerHeight = 3;
const int enemyWidth = 5;
const int enemyHeight = 3;
int playerX, playerY;
int enemyX, enemyY;
int bulletX, bulletY;
bool isBulletActive;
void Setup() {
playerX = screenWidth / 2;
playerY = screenHeight - playerHeight - 1;
enemyX = screenWidth / 2;
enemyY = 0;
bulletX = 0;
bulletY = 0;
isBulletActive = false;
}
void Draw() {
system("cls");
for (int i = 0; i < screenWidth; i++) {
cout << "#";
}
cout << endl;
for (int y = 0; y < screenHeight; y++) {
for (int x = 0; x < screenWidth; x++) {
if (x == 0 || x == screenWidth - 1) {
cout << "#";
}
else if ((x >= playerX && x < playerX + playerWidth) && (y >= playerY && y < playerY + playerHeight)) {
cout << "@";
}
else if ((x >= enemyX && x < enemyX + enemyWidth) && (y >= enemyY && y < enemyY + enemyHeight)) {
cout << "*";
}
else if ((x == bulletX && y == bulletY) && isBulletActive) {
cout << "|";
}
else {
cout << " ";
}
}
cout << endl;
}
for (int i = 0; i < screenWidth; i++) {
cout << "#";
}
cout << endl;
}
void Input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
if (playerX > 1) {
playerX--;
}
break;
case 'd':
if (playerX < screenWidth - playerWidth) {
playerX++;
}
break;
case ' ':
if (!isBulletActive) {
bulletX = playerX + playerWidth / 2;
bulletY = playerY - 1;
isBulletActive = true;
}
break;
case 'q':
exit(0);
}
}
}
void Logic() {
if (isBulletActive) {
bulletY--;
if (bulletY <= 0) {
isBulletActive = false;
}
else if (bulletX >= enemyX && bulletX < enemyX + enemyWidth && bulletY >= enemyY && bulletY < enemyY + enemyHeight) {
isBulletActive = false;
enemyX = rand() % (screenWidth - enemyWidth);
enemyY = 0;
}
}
enemyY++;
if (enemyY >= screenHeight - enemyHeight - 1) {
enemyX = rand() % (screenWidth - enemyWidth);
enemyY = 0;
}
if (playerX >= enemyX && playerX < enemyX + enemyWidth && playerY >= enemyY && playerY < enemyY + enemyHeight) {
cout << "游戏结束!" << endl;
exit(0);
}
}
int main() {
Setup();
while (true) {
Draw();
Input();
Logic();
Sleep(10); }
return 0;
}
共 2 条回复
六逼