init
This commit is contained in:
commit
7d79795002
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.vscode/
|
||||
main.o
|
||||
sfml-app
|
BIN
assets/Arial.ttf
Normal file
BIN
assets/Arial.ttf
Normal file
Binary file not shown.
BIN
assets/background.png
Normal file
BIN
assets/background.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
BIN
assets/damage.ogg
Normal file
BIN
assets/damage.ogg
Normal file
Binary file not shown.
BIN
assets/enemy.png
Normal file
BIN
assets/enemy.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.4 KiB |
50
enemy.cpp
Normal file
50
enemy.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include "enemy.h"
|
||||
|
||||
Enemy::Enemy(int e) {
|
||||
energy = e;
|
||||
}
|
||||
|
||||
bool Enemy::performSetup() {
|
||||
if (!enemyTexture.loadFromFile("assets/enemy.png")) {
|
||||
std::cout << "Couldn't load enemy image" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
enemySprite.setTexture(enemyTexture);
|
||||
enemySprite.setPosition(sf::Vector2f(225, 400));
|
||||
enemySprite.scale(sf::Vector2f(2, 2));
|
||||
|
||||
if (!attackSoundBuffer.loadFromFile("assets/damage.ogg")) {
|
||||
std::cout << "Couldn't load enemy audio" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
attackSound.setBuffer(attackSoundBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Enemy::draw(sf::RenderWindow *window) {
|
||||
window->draw(enemySprite);
|
||||
}
|
||||
|
||||
bool Enemy::checkIfHit(sf::Vector2i mousePos) {
|
||||
float enemyMinX = enemySprite.getGlobalBounds().left;
|
||||
float enemyMaxX = enemySprite.getGlobalBounds().width + enemyMinX;
|
||||
|
||||
float enemyMinY = enemySprite.getGlobalBounds().top;
|
||||
float enemyMaxY = enemySprite.getGlobalBounds().height + enemyMinY;
|
||||
|
||||
float mouseX = mousePos.x;
|
||||
float mouseY = mousePos.y;
|
||||
|
||||
return mouseX >= enemyMinX && mouseX <= enemyMaxX && mouseY >= enemyMinY && mouseY <= enemyMaxY;
|
||||
}
|
||||
|
||||
bool Enemy::takeDamage(int damage) {
|
||||
energy -= damage;
|
||||
attackSound.play();
|
||||
|
||||
return energy <= 0;
|
||||
}
|
26
enemy.h
Normal file
26
enemy.h
Normal file
@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#ifndef ENEMY_H
|
||||
#define ENEMY_H
|
||||
|
||||
class Enemy {
|
||||
sf::Texture enemyTexture;
|
||||
sf::Sprite enemySprite;
|
||||
sf::SoundBuffer attackSoundBuffer;
|
||||
sf::Sound attackSound;
|
||||
|
||||
public:
|
||||
int energy;
|
||||
Enemy(int);
|
||||
|
||||
bool performSetup();
|
||||
bool checkIfHit(sf::Vector2i);
|
||||
bool takeDamage(int);
|
||||
void draw(sf::RenderWindow *);
|
||||
};
|
||||
|
||||
#endif
|
72
gameWorld.cpp
Normal file
72
gameWorld.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "gameWorld.h"
|
||||
#include "enemy.cpp"
|
||||
#include "texts.cpp"
|
||||
|
||||
GameWorld::GameWorld(): enemy(100), texts() {
|
||||
damage = 10;
|
||||
}
|
||||
|
||||
bool GameWorld::loadBackground() {
|
||||
if (!backgroundTexture.loadFromFile("assets/background.png")) {
|
||||
std::cout << "Couldn't load background image" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
background.setTexture(backgroundTexture);
|
||||
background.scale(sf::Vector2f(1.6, 2.25));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GameWorld::performSetup() {
|
||||
isGameOver = false;
|
||||
|
||||
enemy = Enemy(100);
|
||||
texts = Texts();
|
||||
|
||||
return loadBackground() && enemy.performSetup() && texts.performSetup();
|
||||
}
|
||||
|
||||
bool GameWorld::runGame() {
|
||||
sf::RenderWindow window(sf::VideoMode(1000, 1000), "Point and click game!");
|
||||
sf::Clock clock;
|
||||
|
||||
while (window.isOpen()) {
|
||||
if (!isGameOver) {
|
||||
time = clock.getElapsedTime();
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event))
|
||||
{
|
||||
if (event.type == sf::Event::Closed) {
|
||||
window.close();
|
||||
return false;
|
||||
} else if (event.type == sf::Event::MouseButtonPressed) {
|
||||
if (!isGameOver) {
|
||||
if (enemy.checkIfHit(sf::Mouse::getPosition(window))) {
|
||||
isGameOver = enemy.takeDamage(damage);;
|
||||
}
|
||||
}
|
||||
} else if (event.type == sf::Event::KeyPressed) {
|
||||
if (isGameOver && sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.clear();
|
||||
window.draw(background);
|
||||
|
||||
if (!isGameOver) {
|
||||
enemy.draw(&window);
|
||||
texts.drawInGameText(&window, time, enemy.energy);
|
||||
} else {
|
||||
texts.drawEndGameText(&window, time);
|
||||
}
|
||||
|
||||
window.display();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
32
gameWorld.h
Normal file
32
gameWorld.h
Normal file
@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
|
||||
#include "enemy.h"
|
||||
#include "texts.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
#ifndef GAMEWORLD_H
|
||||
#define GAMEWORLD_H
|
||||
|
||||
class GameWorld {
|
||||
bool isGameOver;
|
||||
int damage;
|
||||
|
||||
sf::Texture backgroundTexture;
|
||||
sf::Sprite background;
|
||||
sf::Time time;
|
||||
|
||||
Enemy enemy;
|
||||
Texts texts;
|
||||
|
||||
bool loadBackground();
|
||||
|
||||
public:
|
||||
GameWorld();
|
||||
bool performSetup();
|
||||
bool runGame();
|
||||
};
|
||||
|
||||
#endif
|
20
main.cpp
Normal file
20
main.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include "gameWorld.cpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
while (true) {
|
||||
GameWorld world = GameWorld();
|
||||
|
||||
if (!world.performSetup()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!world.runGame()) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
7
runningSFML.sh
Executable file
7
runningSFML.sh
Executable file
@ -0,0 +1,7 @@
|
||||
#! /bin/bash
|
||||
|
||||
SFML=/usr/local/SFML
|
||||
|
||||
g++ -c main.cpp -I $SFML/include
|
||||
g++ main.o -o sfml-app -L $SFML/lib -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system
|
||||
export LD_LIBRARY_PATH=$SFML/lib && ./sfml-app
|
48
texts.cpp
Normal file
48
texts.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include "texts.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
Texts::Texts() {
|
||||
endGameWonText.setString("You won!");
|
||||
endGameSpaceText.setString("Press SPACE to play again.");
|
||||
}
|
||||
|
||||
void Texts::setupText(sf::Text *text, sf::Vector2f position) {
|
||||
text->setFont(font);
|
||||
text->setCharacterSize(50);
|
||||
text->setFillColor(sf::Color::White);
|
||||
text->setStyle(sf::Text::Bold);
|
||||
text->setPosition(position);
|
||||
}
|
||||
|
||||
bool Texts::performSetup() {
|
||||
if (!font.loadFromFile("assets/Arial.ttf")) {
|
||||
cout << "Couldn't load font file" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
setupText(&energyText, sf::Vector2f(650, 800));
|
||||
setupText(&timeText, sf::Vector2f(650, 900));
|
||||
setupText(&endGameWonText, sf::Vector2f(400, 600));
|
||||
setupText(&endGameTimeText, sf::Vector2f(400, 700));
|
||||
setupText(&endGameSpaceText, sf::Vector2f(200, 800));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texts::drawInGameText(sf::RenderWindow *window, sf::Time time, int energy) {
|
||||
energyText.setString("Energy: " + to_string(energy));
|
||||
timeText.setString("Time: " + to_string((int) time.asSeconds()) + "s");
|
||||
|
||||
window->draw(energyText);
|
||||
window->draw(timeText);
|
||||
}
|
||||
|
||||
void Texts::drawEndGameText(sf::RenderWindow *window, sf::Time time) {
|
||||
endGameTimeText.setString("Time: " + to_string((int) time.asSeconds()) + "s");
|
||||
|
||||
window->draw(endGameWonText);
|
||||
window->draw(endGameTimeText);
|
||||
window->draw(endGameSpaceText);
|
||||
}
|
24
texts.h
Normal file
24
texts.h
Normal file
@ -0,0 +1,24 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#ifndef TEXTS_H
|
||||
#define TEXTS_H
|
||||
|
||||
class Texts{
|
||||
sf::Font font;
|
||||
|
||||
void setupText(sf::Text *, sf::Vector2f);
|
||||
|
||||
public:
|
||||
sf::Text energyText;
|
||||
sf::Text timeText;
|
||||
sf::Text endGameWonText;
|
||||
sf::Text endGameTimeText;
|
||||
sf::Text endGameSpaceText;
|
||||
Texts();
|
||||
|
||||
bool performSetup();
|
||||
void drawInGameText(sf::RenderWindow *, sf::Time, int);
|
||||
void drawEndGameText(sf::RenderWindow *, sf::Time);
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user