请用c++代码实现打飞机小游戏
时间: 2023-08-29 13:06:32 浏览: 150
c++星球大战 打飞机游戏 源代码 适合初学者.pdf
好的,下面是一个简单的打飞机小游戏的代码示例,使用C++和SFML库实现。
```c++
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int PLAYER_SIZE = 50;
const int PLAYER_SPEED = 10;
const int PLAYER_LIVES = 3;
const int BULLET_SIZE = 10;
const int BULLET_SPEED = 20;
const int ENEMY_SIZE = 50;
const int ENEMY_SPEED = 5;
const int ENEMY_SPAWN_TIME = 30;
const int SCORE_POS_X = 10;
const int SCORE_POS_Y = 10;
const int GAME_OVER_POS_X = 200;
const int GAME_OVER_POS_Y = 250;
const string FONT_PATH = "font.ttf";
const string BACKGROUND_MUSIC_PATH = "music.ogg";
const string SHOOT_SOUND_PATH = "shoot.ogg";
const string EXPLOSION_SOUND_PATH = "explosion.ogg";
class Player
{
public:
sf::RectangleShape shape;
int lives;
int score;
Player()
{
shape.setSize(sf::Vector2f(PLAYER_SIZE, PLAYER_SIZE));
shape.setFillColor(sf::Color::White);
shape.setOrigin(PLAYER_SIZE / 2, PLAYER_SIZE / 2);
shape.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT - PLAYER_SIZE / 2);
lives = PLAYER_LIVES;
score = 0;
}
void moveLeft()
{
if (shape.getPosition().x > PLAYER_SIZE / 2)
{
shape.move(-PLAYER_SPEED, 0);
}
}
void moveRight()
{
if (shape.getPosition().x < WINDOW_WIDTH - PLAYER_SIZE / 2)
{
shape.move(PLAYER_SPEED, 0);
}
}
void moveUp()
{
if (shape.getPosition().y > PLAYER_SIZE / 2)
{
shape.move(0, -PLAYER_SPEED);
}
}
void moveDown()
{
if (shape.getPosition().y < WINDOW_HEIGHT - PLAYER_SIZE / 2)
{
shape.move(0, PLAYER_SPEED);
}
}
void shoot(vector<sf::CircleShape>& bullets, sf::Sound& shootSound)
{
bullets.push_back(sf::CircleShape(BULLET_SIZE));
bullets.back().setFillColor(sf::Color::Yellow);
bullets.back().setOrigin(BULLET_SIZE / 2, BULLET_SIZE / 2);
bullets.back().setPosition(shape.getPosition());
shootSound.play();
}
void update(vector<sf::CircleShape>& bullets)
{
for (int i = 0; i < bullets.size(); i++)
{
if (bullets[i].getPosition().y < 0)
{
bullets.erase(bullets.begin() + i);
i--;
}
else
{
bullets[i].move(0, -BULLET_SPEED);
}
}
}
};
class Enemy
{
public:
sf::RectangleShape shape;
Enemy()
{
shape.setSize(sf::Vector2f(ENEMY_SIZE, ENEMY_SIZE));
shape.setFillColor(sf::Color::Red);
shape.setOrigin(ENEMY_SIZE / 2, ENEMY_SIZE / 2);
shape.setPosition(rand() % WINDOW_WIDTH, -ENEMY_SIZE / 2);
}
void update()
{
shape.move(0, ENEMY_SPEED);
}
};
int main()
{
srand(time(NULL));
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML Game");
window.setFramerateLimit(60);
sf::Font font;
if (!font.loadFromFile(FONT_PATH))
{
cout << "Error loading font" << endl;
return -1;
}
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(30);
scoreText.setFillColor(sf::Color::White);
scoreText.setPosition(SCORE_POS_X, SCORE_POS_Y);
sf::Text gameOverText;
gameOverText.setFont(font);
gameOverText.setCharacterSize(50);
gameOverText.setFillColor(sf::Color::White);
gameOverText.setPosition(GAME_OVER_POS_X, GAME_OVER_POS_Y);
gameOverText.setString("GAME OVER");
sf::Music backgroundMusic;
if (!backgroundMusic.openFromFile(BACKGROUND_MUSIC_PATH))
{
cout << "Error loading music" << endl;
return -1;
}
backgroundMusic.setVolume(50);
backgroundMusic.setLoop(true);
backgroundMusic.play();
sf::SoundBuffer shootBuffer;
if (!shootBuffer.loadFromFile(SHOOT_SOUND_PATH))
{
cout << "Error loading sound" << endl;
return -1;
}
sf::Sound shootSound(shootBuffer);
sf::SoundBuffer explosionBuffer;
if (!explosionBuffer.loadFromFile(EXPLOSION_SOUND_PATH))
{
cout << "Error loading sound" << endl;
return -1;
}
sf::Sound explosionSound(explosionBuffer);
Player player;
vector<sf::CircleShape> bullets;
vector<Enemy> enemies;
int enemySpawnTimer = ENEMY_SPAWN_TIME;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
if (player.lives > 0)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
player.moveLeft();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
player.moveRight();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
player.moveUp();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
player.moveDown();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
player.shoot(bullets, shootSound);
}
player.update(bullets);
if (enemySpawnTimer > 0)
{
enemySpawnTimer--;
}
else
{
enemies.push_back(Enemy());
enemySpawnTimer = ENEMY_SPAWN_TIME;
}
for (int i = 0; i < enemies.size(); i++)
{
enemies[i].update();
if (enemies[i].shape.getGlobalBounds().intersects(player.shape.getGlobalBounds()))
{
explosionSound.play();
enemies.erase(enemies.begin() + i);
i--;
player.lives--;
}
else
{
for (int j = 0; j < bullets.size(); j++)
{
if (enemies[i].shape.getGlobalBounds().intersects(bullets[j].getGlobalBounds()))
{
explosionSound.play();
enemies.erase(enemies.begin() + i);
i--;
bullets.erase(bullets.begin() + j);
j--;
player.score++;
}
}
}
}
scoreText.setString("Score: " + to_string(player.score) + "\nLives: " + to_string(player.lives));
}
window.clear();
for (int i = 0; i < bullets.size(); i++)
{
window.draw(bullets[i]);
}
for (int i = 0; i < enemies.size(); i++)
{
window.draw(enemies[i].shape);
}
window.draw(player.shape);
window.draw(scoreText);
if (player.lives <= 0)
{
window.draw(gameOverText);
}
window.display();
}
return 0;
}
```
这个代码示例使用了SFML库来创建窗口和处理用户输入,同时实现了玩家、子弹和敌机的移动和碰撞检测,以及游戏得分和生命值的显示,还有背景音乐和音效的播放。你可以根据自己的需要和喜好修改代码并添加更多功能。
阅读全文