Skip to content

Commit 9938e82

Browse files
committed
Created Player class files and moved relevant code from main into it
1 parent 0d2bc5d commit 9938e82

File tree

7 files changed

+195
-77
lines changed

7 files changed

+195
-77
lines changed

.vscode/settings.json

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,59 @@
1616
"xstring": "cpp",
1717
"iterator": "cpp",
1818
"type_traits": "cpp",
19-
"xutility": "cpp"
19+
"xutility": "cpp",
20+
"algorithm": "cpp",
21+
"array": "cpp",
22+
"bit": "cpp",
23+
"cctype": "cpp",
24+
"charconv": "cpp",
25+
"chrono": "cpp",
26+
"clocale": "cpp",
27+
"cmath": "cpp",
28+
"codecvt": "cpp",
29+
"compare": "cpp",
30+
"concepts": "cpp",
31+
"cstddef": "cpp",
32+
"cstdint": "cpp",
33+
"cstdio": "cpp",
34+
"cstdlib": "cpp",
35+
"cstring": "cpp",
36+
"ctime": "cpp",
37+
"cwchar": "cpp",
38+
"exception": "cpp",
39+
"map": "cpp",
40+
"string": "cpp",
41+
"unordered_map": "cpp",
42+
"unordered_set": "cpp",
43+
"format": "cpp",
44+
"functional": "cpp",
45+
"initializer_list": "cpp",
46+
"iomanip": "cpp",
47+
"iosfwd": "cpp",
48+
"iostream": "cpp",
49+
"istream": "cpp",
50+
"limits": "cpp",
51+
"locale": "cpp",
52+
"memory": "cpp",
53+
"new": "cpp",
54+
"ostream": "cpp",
55+
"ratio": "cpp",
56+
"sstream": "cpp",
57+
"stdexcept": "cpp",
58+
"streambuf": "cpp",
59+
"tuple": "cpp",
60+
"typeinfo": "cpp",
61+
"utility": "cpp",
62+
"variant": "cpp",
63+
"xfacet": "cpp",
64+
"xlocale": "cpp",
65+
"xlocbuf": "cpp",
66+
"xlocinfo": "cpp",
67+
"xlocmon": "cpp",
68+
"xlocnum": "cpp",
69+
"xloctime": "cpp",
70+
"xmemory": "cpp",
71+
"xstddef": "cpp",
72+
"xtr1common": "cpp"
2073
}
2174
}

Player.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "Player.hpp"
2+
#include <SFML/Window/Keyboard.hpp>
3+
4+
Player::Player()
5+
: texture{},
6+
sprite( // Create a player sprite
7+
[this]() {
8+
if (!texture.loadFromFile("spritesheet.png"))
9+
throw std::runtime_error("Failed to load sprite sheet");
10+
return sf::Sprite(texture);
11+
}()
12+
)
13+
{
14+
sprite.setTexture(texture);
15+
sprite.setTextureRect({{485,1}, {240,240}});
16+
sprite.setOrigin({sprite.getTextureRect().size.x / 2.0f, sprite.getTextureRect().size.y / 2.0f});
17+
sf::Vector2<float> position(275.f, 200.f); // Set coordinates
18+
sprite.setPosition(position); // Place sprite at coordinates
19+
sprite.setScale({1.0f,1.0f});
20+
21+
// Default movement speed
22+
verticalSpeed = 2.0f;
23+
horizontalSpeed = 2.0f;
24+
25+
// Coordinates for current texture in spritesheet
26+
textureX = 1;
27+
textureY = 1;
28+
29+
// Timer for animations and delays
30+
timer = 0.0f;
31+
timerMax = 0.5f;
32+
33+
// When x coordinates have reached the final column
34+
finalColumn = 1211;
35+
36+
// Start and end coordinates for animation textures in spritesheet
37+
walkingDownXStart = 485;
38+
walkingDownYStart = 727;
39+
walkingDownXEnd = 727;
40+
walkingDownYEnd = 969;
41+
}
42+
43+
void Player::handleInput() {
44+
45+
// If left key pressed then move character left
46+
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Left)) {
47+
sprite.move({-horizontalSpeed, 0.0f});
48+
}
49+
// If right key pressed then move character right
50+
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right)) {
51+
sprite.move({horizontalSpeed, 0.0f});
52+
}
53+
// If up key pressed then move character up
54+
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up)) {
55+
sprite.move({0.0f, -verticalSpeed});
56+
}
57+
// If down key pressed then move character down
58+
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down)) {
59+
sprite.move({0.0f, verticalSpeed});
60+
61+
// Walking down animation
62+
timer += 0.1f;
63+
// Will wait for set amount of time
64+
if (timer >= timerMax) {
65+
textureX += 242;
66+
// If behind walking down textures, go to start
67+
if(textureY == 727) {
68+
if(textureX < 485) {
69+
textureX = 485;
70+
}
71+
}
72+
// If gone past walking down textures, go back to start
73+
if(textureY == 969) {
74+
if(textureX > 727) {
75+
textureY = 727;
76+
textureX = 485;
77+
}
78+
}
79+
// If not within walking down textures, go to start
80+
if(textureY < 727 || textureY > 969) {
81+
textureX = 485;
82+
textureY = 727;
83+
}
84+
// If texture width reached end of spritesheet
85+
if(textureX > 1211) {
86+
textureX = 1;
87+
textureY += 242;
88+
}
89+
// If texture width valid, then change current sprite texture
90+
if(textureX <= 1211) {
91+
sprite.setTextureRect({{textureX,textureY},{240,240}});
92+
}
93+
timer = 0.0f;
94+
}
95+
}
96+
}
97+
98+
void Player::update() {
99+
100+
}
101+
102+
void Player::draw(sf::RenderWindow& window) {
103+
window.draw(sprite);
104+
}

Player.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef PLAYER_HPP
2+
#define PLAYER_HPP
3+
4+
#include <SFML/Graphics.hpp>
5+
6+
class Player {
7+
public:
8+
Player();
9+
void handleInput();
10+
void update();
11+
void draw(sf::RenderWindow& window);
12+
private:
13+
sf::Texture texture;
14+
sf::Sprite sprite;
15+
16+
int textureX, textureY;
17+
float timer, timerMax;
18+
float verticalSpeed, horizontalSpeed;
19+
int finalColumn;
20+
int walkingDownXStart, walkingDownYStart, walkingDownXEnd, walkingDownYEnd;
21+
};
22+
23+
#endif // PLAYER_HPP

Player.obj

81.4 KB
Binary file not shown.

main.cpp

Lines changed: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,41 @@
66
#include <SFML/Audio.hpp>
77
#include <SFML/Network.hpp>
88

9+
#include "Player.cpp"
10+
#include "Player.hpp"
11+
912
int main()
1013
{
1114
// Create a player sprite
12-
sf::Texture texture;
13-
if (!texture.loadFromFile("spritesheet.png")) {
14-
// Handle error
15-
}
16-
17-
sf::Sprite sprite(texture);
18-
19-
int texWidth;
20-
int texHeight;
21-
22-
sprite.setTextureRect({{485,1}, {240,240}});
23-
sprite.setOrigin({sprite.getTextureRect().size.x / 2.0f, sprite.getTextureRect().size.y / 2.0f});
24-
sf::Vector2<float> position(275.f, 200.f); // Set coordinates
25-
sprite.setPosition(position); // Place sprite at coordinates
26-
sprite.setScale({1.0f,1.0f});
27-
28-
float timer = 0.0f;
29-
float timerMax = 0.5f;
15+
Player player;
3016

17+
// Create game window
3118
sf::RenderWindow window(sf::VideoMode({800, 600}), "2D Game", sf::Style::Titlebar | sf::Style::Close);
3219
window.setFramerateLimit(60);
3320

34-
// Game loop
21+
// Ensures window closes properly when closed
3522
const auto onClose = [&window](const sf::Event::Closed&) {
3623
window.close();
3724
};
38-
25+
// Ensure window is closed when Escape key is pressed
3926
const auto onKeyPressed = [&window](const sf::Event::KeyPressed& keyPressed) {
4027
if (keyPressed.scancode == sf::Keyboard::Scancode::Escape)
4128
window.close();
4229
};
4330

31+
// Game loop
4432
while (window.isOpen()) {
4533
window.handleEvents(onClose, onKeyPressed);
4634

47-
// Remainder of main loop
35+
// Handle player controls
36+
player.handleInput();
37+
player.update();
4838

4939
// Create new window with sprite drawn in
5040
window.clear(sf::Color::White);
51-
window.draw(sprite);
41+
player.draw(window);
5242
window.display();
53-
54-
// If left key pressed then move character left
55-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
56-
sprite.move({-2.f, 0.f});
57-
}
58-
// If right key pressed then move character right
59-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right))
60-
{
61-
sprite.move({2.f, 0.f});
62-
}
63-
// If down key pressed then move character down
64-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Down))
65-
{
66-
sprite.move({0.f, 2.f});
67-
68-
// Walking down animation
69-
timer += 0.1f;
70-
if(timer >= timerMax) {
71-
texWidth += 242;
72-
// If behind walking down textures, go to start
73-
if(texHeight == 727) {
74-
if(texWidth < 485) {
75-
texWidth = 485;
76-
}
77-
}
78-
// If gone past walking down textures, go back to start
79-
if(texHeight == 969) {
80-
if(texWidth > 727) {
81-
texHeight = 727;
82-
texWidth = 485;
83-
}
84-
}
85-
// If not within walking down textures, go to start
86-
if(texHeight < 727 || texHeight > 969) {
87-
texWidth = 485;
88-
texHeight = 727;
89-
}
90-
// If texture width reached end of spritesheet
91-
if(texWidth > 1211) {
92-
texWidth = 1;
93-
texHeight += 242;
94-
}
95-
// If texture width valid, then change current sprite texture
96-
if(texWidth <= 1211) {
97-
sprite.setTextureRect({{texWidth,texHeight},{240,240}});
98-
}
99-
timer = 0.0f;
100-
}
101-
}
102-
// If up key pressed then move character up
103-
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Up))
104-
{
105-
sprite.move({0.f, -2.f});
106-
}
10743
}
44+
45+
return 0;
10846
}

main.exe

512 Bytes
Binary file not shown.

main.obj

2.81 KB
Binary file not shown.

0 commit comments

Comments
 (0)