-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
93 lines (69 loc) · 2.07 KB
/
Game.cpp
File metadata and controls
93 lines (69 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Game.hpp"
Game::Game() : window("Sav's Game") {
deltaTime = clock.restart().asSeconds();
vikingTexture.loadFromFile(workingDir.Get() + "Valkyrie.png");
vikingSprite.setTexture(vikingTexture);
vikingSprite.setTextureRect(sf::IntRect(1, 1, 30, 28));
vikingSprite.scale(sf::Vector2f(5,5));
chestTexture.loadFromFile(workingDir.Get() + "treasure.png");
chestSprite.setTexture(chestTexture);
chestSprite.scale(sf::Vector2f(3,3));
if (!Music1.openFromFile("fairy.ogg"))
{
// Error...
std::cout<<"Error...";
}
Music1.play();
}
void Game::Update() {
window.Update();
// const sf::Vector2f& backPos = backgroundSprite.getPosition();
const sf::Vector2f& spritePos = vikingSprite.getPosition();
const sf::Vector2f& chestPos = chestSprite.getPosition();
const int moveSpeed = 100;
int xMove = 0;
if(input.IsKeyPressed(Input::Key::Left)) // 1
{
xMove = -moveSpeed; // 2
}
else if(input.IsKeyPressed(Input::Key::Right))
{
xMove = moveSpeed;
}
int yMove = 0;
if(input.IsKeyPressed(Input::Key::Up))
{
yMove = -moveSpeed;
}
else if(input.IsKeyPressed(Input::Key::Down))
{
yMove = moveSpeed;
}
float xFrameMove = xMove * deltaTime;
float yFrameMove = yMove * deltaTime;
vikingSprite.setPosition(spritePos.x + xFrameMove, spritePos.y + yFrameMove);
chestSprite.setPosition(200, 300);
// backgroundSprite.setPosition(0,0);
while ( vikingSprite.getGlobalBounds().intersects(chestSprite.getGlobalBounds()) ){
chestSprite.setPosition(chestPos.x + xFrameMove, chestPos.y + yFrameMove );
}
}
void Game::LateUpdate(){
}
void Game::Draw(){
window.BeginDraw();
// window.Draw(backgroundSprite);
window.Draw(vikingSprite); // Draw the sprite.
window.Draw(chestSprite); // Draw the sprite.
window.EndDraw();
}
bool Game::IsRunning() const {
return window.IsOpen();
}
void Game::CalculateDeltaTime() {
deltaTime = clock.restart().asSeconds();
}
void Game::CaptureInput()
{
input.Update();
}