飞机大战c语言easyx
时间: 2023-11-26 13:06:20 浏览: 80
以下是一个简单的飞机大战C语言EasyX的例子:
```c++
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 480
#define HEIGHT 700
#define ENEMY_NUM 5
#define BULLET_NUM 10
IMAGE bg, plane, enemy, bullet;
int score = 0;
int plane_x = WIDTH / 2, plane_y = HEIGHT - 100;
int enemy_x[ENEMY_NUM], enemy_y[ENEMY_NUM];
int bullet_x[BULLET_NUM], bullet_y[BULLET_NUM];
bool bullet_state[BULLET_NUM] = { false };
void init() {
initgraph(WIDTH, HEIGHT);
loadimage(&bg, _T("bg.jpg"));
loadimage(&plane, _T("plane.png"));
loadimage(&enemy, _T("enemy.png"));
loadimage(&bullet, _T("bullet.png"));
srand((unsigned)time(NULL));
for (int i = 0; i < ENEMY_NUM; i++) {
enemy_x[i] = rand() % (WIDTH - 50);
enemy_y[i] = rand() % 200 - 200;
}
}
void draw() {
putimage(0, 0, &bg);
putimage(plane_x, plane_y, &plane);
for (int i = 0; i < ENEMY_NUM; i++) {
putimage(enemy_x[i], enemy_y[i], &enemy);
}
for (int i = 0; i < BULLET_NUM; i++) {
if (bullet_state[i]) {
putimage(bullet_x[i], bullet_y[i], &bullet);
}
}
settextcolor(WHITE);
settextstyle(20, 0, _T("Consolas"));
TCHAR str[50];
_stprintf_s(str, _T("Score: %d"), score);
outtextxy(10, 10, str);
}
void updateWithoutInput() {
for (int i = 0; i < ENEMY_NUM; i++) {
enemy_y[i] += 2;
if (enemy_y[i] > HEIGHT) {
enemy_x[i] = rand() % (WIDTH - 50);
enemy_y[i] = rand() % 200 - 200;
}
}
for (int i = 0; i < BULLET_NUM; i++) {
if
阅读全文