mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-07-04 14:43:01 +02:00
feat/bug: Fixed the initial values of mIsMoving* variables so that they are bool, added player movement
This commit is contained in:
parent
5fddef2c6c
commit
9b291334f2
Binary file not shown.
10
SFMLEngine/makingAGameTick/basic.cpp
Normal file
10
SFMLEngine/makingAGameTick/basic.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef BASIC_CPP
|
||||
#define BASIC_CPP
|
||||
#include <iostream>
|
||||
|
||||
void print(const std::string s)
|
||||
{
|
||||
std::cout << s << std::endl;
|
||||
}
|
||||
|
||||
#endif // BASIC_CPP
|
||||
@ -11,9 +11,9 @@ const sf::Color PLAYER_COLOR = sf::Color::Cyan;
|
||||
|
||||
// Movement constants
|
||||
// const sf::Vector2f INITIAL_MOVEMENT (0.f, 0.f);
|
||||
const float MOVING_UP_SPEED = -1;
|
||||
const float MOVING_UP_SPEED = -0.01;
|
||||
const float MOVING_DOWN_SPEED = -MOVING_UP_SPEED;
|
||||
const float MOVING_RIGHT_SPEED = 1;
|
||||
const float MOVING_RIGHT_SPEED = 0.01;
|
||||
const float MOVING_LEFT_SPEED = -MOVING_RIGHT_SPEED;
|
||||
|
||||
#endif // CONSTANTS_HPP
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include <vector>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "constants.hpp"
|
||||
#include "basic.cpp"
|
||||
|
||||
class Game
|
||||
{
|
||||
@ -13,7 +14,10 @@ class Game
|
||||
void update(); // code that updates the game
|
||||
void render(); // code that renders the game
|
||||
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
|
||||
bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown;
|
||||
bool mIsMovingUp = false;
|
||||
bool mIsMovingRight = false;
|
||||
bool mIsMovingLeft = false;
|
||||
bool mIsMovingDown = false;
|
||||
private:
|
||||
sf::RenderWindow mWindow;
|
||||
sf::CircleShape mPlayer;
|
||||
@ -24,10 +28,6 @@ Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mPlayer()
|
||||
mPlayer.setRadius(PLAYER_RADIUS);
|
||||
mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION);
|
||||
mPlayer.setFillColor(PLAYER_COLOR);
|
||||
bool mIsMovingUp = false;
|
||||
bool mIsMovingRight = false;
|
||||
bool mIsMovingLeft = false;
|
||||
bool mIsMovingDown = false;
|
||||
}
|
||||
|
||||
void Game::run()
|
||||
@ -63,7 +63,7 @@ void Game::processEvents()
|
||||
sf::Event event;
|
||||
while (mWindow.pollEvent(event)) // mainLoop/gameLoop
|
||||
{
|
||||
// each time while loop iterates it means that wwe got a new event registered by the window.
|
||||
// each time while loop iterates it means that we got a new event registered by the window.
|
||||
switch (event.type)
|
||||
{
|
||||
case sf::Event::KeyPressed:
|
||||
@ -81,11 +81,12 @@ void Game::processEvents()
|
||||
|
||||
void Game::update()
|
||||
{
|
||||
|
||||
sf::Vector2f movement (0.f, 0.f);
|
||||
movement.y += mIsMovingUp * MOVING_UP_SPEED + mIsMovingDown * MOVING_DOWN_SPEED;
|
||||
movement.x += mIsMovingLeft * MOVING_LEFT_SPEED + mIsMovingRight * MOVING_RIGHT_SPEED;
|
||||
|
||||
mPlayer.move(movement);
|
||||
|
||||
}
|
||||
|
||||
void Game::render()
|
||||
|
||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user