0% found this document useful (0 votes)
0 views4 pages

Class Level1

The document contains a C++ program using SFML to create a simple 2D game level. It includes a player character that can move within defined boundaries, collect a key, and interact with obstacles. The game updates and renders the scene based on user input and maintains a timer for gameplay duration.

Uploaded by

mhbusman8214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Class Level1

The document contains a C++ program using SFML to create a simple 2D game level. It includes a player character that can move within defined boundaries, collect a key, and interact with obstacles. The game updates and renders the scene based on user input and maintains a timer for gameplay duration.

Uploaded by

mhbusman8214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <SFML/Graphics.

hpp>
#include <vector>
#include <stack>
#include <iostream>
#include <sstream>

using namespace sf;


using namespace std;

// Generic movement function


void genericMove(Sprite& sprite, float offsetX, float offsetY, float deltaTime, const
FloatRect& bounds, bool& facingRight) {
Vector2f newPosition = sprite.getPosition() + Vector2f(offsetX * deltaTime, offsetY *
deltaTime);

if (offsetX != 0) {
bool movingRight = offsetX > 0;
if (movingRight != facingRight) {
facingRight = movingRight;
sprite.setScale((facingRight ? 0.7f : -0.7f), 0.7f);
}
}

float spriteWidth = sprite.getGlobalBounds().width;


float spriteHeight = sprite.getGlobalBounds().height;

newPosition.x = max(bounds.left, min(newPosition.x, bounds.width - spriteWidth));


newPosition.y = max(bounds.top, min(newPosition.y, bounds.height - spriteHeight));

sprite.setPosition(newPosition);
}

// Level1 Class
class Level1 {
public:
Level1(const RenderWindow& window)
: currentFrame(0), frameTime(seconds(0.2f)), facingRight(true), glowing(false),
timer(20.0f) {
// Load background
if (!backgroundTexture.loadFromFile("room.png")) {
throw runtime_error("Failed to load background image");
}
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setScale(0.87f, 0.87f);

// Load player textures


vector<string> playerImagePaths = {
"player1.png", "player2.png", "player3.png", "player4.png", "player5.png"
};
for (const auto& path : playerImagePaths) {
Texture texture;
if (!texture.loadFromFile(path)) {
throw runtime_error("Failed to load player image: " + path);
}
playerTextures.push_back(texture);
}

// Player setup
playerSprite.setTexture(playerTextures[0]);
playerSprite.setScale(0.7f, 0.7f);
playerSprite.setPosition(0.0f, window.getSize().y - 70.0f);

// Load key texture


if (!keyTexture.loadFromFile("key.png")) {
throw runtime_error("Failed to load key image");
}
keySprite.setTexture(keyTexture);
keySprite.setPosition(150, 75);
keySprite.setScale(0.18f, 0.18f);

// Load obstacle textures and setup


vector<string> obstaclePaths = { "obstacle1.png", "obstacle2.png", "obstacle3.png"
};
for (const auto& path : obstaclePaths) {
Texture texture;
if (!texture.loadFromFile(path)) {
throw runtime_error("Failed to load obstacle: " + path);
}
obstacleTextures.push_back(texture);
}

setupObstacles();
}

void update(float offsetX, float offsetY, float deltaTime, const RenderWindow& window)
{
FloatRect bounds(0, 0, window.getSize().x, window.getSize().y);
genericMove(playerSprite, offsetX, offsetY, deltaTime, bounds, facingRight);

if (clock.getElapsedTime() > frameTime) {


currentFrame = (currentFrame + 1) % playerTextures.size();
playerSprite.setTexture(playerTextures[currentFrame]);
clock.restart();
}

playerSprite.setColor(glowing ? Color(255, 255, 0) : Color::White);

// Check obstacle collisions


for (auto& obstacle : obstacles) {
if (playerSprite.getGlobalBounds().intersects(obstacle.getGlobalBounds())) {
obstacle.setScale(0.0f, 0.0f); // Hide obstacle
}
}

// Check key collection


if (playerSprite.getGlobalBounds().intersects(keySprite.getGlobalBounds())) {
keySprite.setColor(Color::Transparent); // Hide key
collectKey();
glowing = true;
}

// Decrease the timer


timer -= deltaTime;
}

void draw(RenderWindow& window) {


window.draw(backgroundSprite); // Draw background
window.draw(keySprite); // Draw key
for (const auto& obstacle : obstacles) {
window.draw(obstacle); // Draw obstacles
}
window.draw(playerSprite); // Draw player
}

bool isTimeUp() const {


return timer <= 0;
}

private:
Texture backgroundTexture;
Sprite backgroundSprite;

vector<Texture> playerTextures;
Sprite playerSprite;

Texture keyTexture;
Sprite keySprite;

vector<Texture> obstacleTextures;
vector<Sprite> obstacles;

int currentFrame;
Clock clock;
Time frameTime;
bool facingRight;
bool glowing;
float timer;

stack<string> keysCollected;

void collectKey() {
keysCollected.push("key");
}

void setupObstacles() {
vector<Vector2f> positions = { {670, 440}, {460, 350}, {130, 50} };
for (size_t i = 0; i < obstacleTextures.size(); ++i) {
Sprite obstacle(obstacleTextures[i]);
obstacle.setPosition(positions[i]);
if (i == 2) obstacle.setScale(0.12f, 0.12f); // Adjust scale for smaller obstacle
else obstacle.setScale(0.3f, 0.3f);
obstacles.push_back(obstacle);
}
}
};

// Main function
int main() {
RenderWindow window(VideoMode(800, 600), "Level 1 Animation with Boundaries");

try {
Level1 level1(window);
Clock deltaClock;
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
}
}

float deltaTime = deltaClock.restart().asSeconds();

// Handle input
float moveX = 0.0f, moveY = 0.0f;
const float movementSpeed = 100.0f;

if (Keyboard::isKeyPressed(Keyboard::W)) moveY -= movementSpeed;


if (Keyboard::isKeyPressed(Keyboard::S)) moveY += movementSpeed;
if (Keyboard::isKeyPressed(Keyboard::A)) moveX -= movementSpeed;
if (Keyboard::isKeyPressed(Keyboard::D)) moveX += movementSpeed;

level1.update(moveX, moveY, deltaTime, window);

// Check if time is up
if (level1.isTimeUp()) {
window.close();
}

// Render
window.clear();
level1.draw(window);
window.display();
}
}
catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
return -1;
}

return 0;
}

You might also like