Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 8 score system #9

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Header/Core/GameLoop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "SFML/Graphics.hpp"
#include "GameWindowManager.h"
#include "../Event/EventManager.h"
#include "../Gameplay/GameplayManager.h"

using namespace sf;
using namespace Core;
using namespace Eventmanager;
using namespace Gameplay;


namespace Core
{
class GameLoop
{
private:
GameWindowManager* game_window_manager;
EventManager* event_manager;
GameplayManager* gameplay_manager;


public:
void initialize();

bool isGameRunning();
void pollEvent();
void update();
void render();
};
}

29 changes: 29 additions & 0 deletions Header/Core/GameWindowManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <SFML/Graphics.hpp>

using namespace sf;

namespace Core
{
class GameWindowManager
{
private:
int game_window_width = 1280;
int game_window_height = 720;
std::string game_title = "SFML-Pong!";

RenderWindow* game_window;

void createGameWindow();

public:
void initialize();
RenderWindow* getGameWindow();
bool isGameRunning();
void clearWindow();
void displayWindow();
//void render();
};
}

16 changes: 16 additions & 0 deletions Header/Event/EventManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "SFML/Graphics.hpp"

using namespace sf;

namespace Eventmanager
{
class EventManager
{
public:
void pollEvents(RenderWindow* game_window); // Process all events
bool isKeyPressed(sf::Keyboard::Key key); // Check specific key
};
}

52 changes: 52 additions & 0 deletions Header/Gameplay/Ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "SFML/Graphics.hpp"
#include "Paddle.h"

using namespace sf;
using namespace std;

namespace Gameplay
{
class Ball
{
private:
Texture pong_ball_texture;
Sprite pong_ball_sprite;

const string texture_path = "Assets/Textures/Ball.png";
const float scale_x = 0.03f;
const float scale_y = 0.03f;
const float position_x = 615.0f;
const float position_y = 335.0f;
const float top_boundary = 20.f;
const float bottom_boundary = 700.f;
const float right_boundary = 1260.f;
const float left_boundary = 20.f;

bool had_left_collision = false;
bool had_right_collision = false;

float ball_speed = 7.f;
Vector2f velocity = Vector2f(ball_speed, ball_speed);

void loadTexture();
void initializeVariables();

void handlePaddleCollision(Paddle* p1, Paddle* p2);
void handleBoundaryCollision();
void handleScoreCollisions();
void reset();

public:
Ball();
bool isLeftCollisionOccurred();
void updateLeftCollision(bool value);
bool isRightCollisionOccurred();
void updateRightCollision(bool value);
void update(Paddle* p1, Paddle* p2);
void render(RenderWindow* game_window);
void move();
};
}

59 changes: 59 additions & 0 deletions Header/Gameplay/Boundary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once
#include "SFML/Graphics.hpp"

using namespace sf;

namespace Gameplay
{
class Boundary
{
private:
//horizontal
RectangleShape topBoundary;
RectangleShape bottomBoundary;
//vertical
RectangleShape leftBoundary;
RectangleShape rightBoundary;
RectangleShape centerLine;

// Horizontal boundary dimensions (top and bottom)
const float horizontal_boundary_width = 1280.0f;
const float horizontal_boundary_height = 20.0f;

// Vertical boundaries dimensions (left and right)
const float vertical_boundary_width = 20.0f;
const float vertical_boundary_height = 720.0f;

// Top boundary starts at the window's top-left corner
const float top_position_x = 0.0f;
const float top_position_y = 0.0f;

// Left boundary also starts at top-left
const float left_position_x = 0.0f;
const float left_position_y = 0.0f;

//Boundary Colors
const Color boundary_color = Color::Blue;
const Color center_line_color = Color::White;

//center lines properties
const float center_line_width = 10.0f;
const float center_line_height = 680.0f;

const float center_line_position_x = 640.0f;
const float center_line_position_y = 20.0f;

//create boundaries and the center line
void createTopBoundary();
void createBottomBoundary();
void createLeftBoundary();
void createRightBoundary();

void createCenterLine();

public:
Boundary();
void render(RenderWindow* game_window);
};
}

43 changes: 43 additions & 0 deletions Header/Gameplay/GameplayManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "Paddle.h"
#include "Ball.h"
#include "../Event/EventManager.h"
#include "../Utility/TimeManager.h"
#include "../UI/UIService.h"
#include "Boundary.h"

using namespace Eventmanager;
using namespace Utility;

namespace Gameplay
{
class GameplayManager
{
private:
float player1_position_x = 30.0f;
float player1_position_y = 290.0f;
float player2_position_x = 1230.0f;
float player2_postion_y = 290.0f;

Boundary* boundary;
EventManager* event_manager;
Ball* ball;
Paddle* player1;
Paddle* player2;
TimeManager* time_manager;
UIService* ui_service = new UIService();
void UpdateScore();
void resetPlayers();

void initialize();

public:
GameplayManager(EventManager* manager);
//GameplayManager();
void update();
void render(RenderWindow* game_window);

};
}

28 changes: 28 additions & 0 deletions Header/Gameplay/Paddle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "SFML/Graphics.hpp"

using namespace sf;

namespace Gameplay
{
class Paddle
{
private:
RectangleShape paddle_sprite;

const float paddle_width = 20.0f;
const float paddle_height = 140.0f;
const float paddleSpeed = 7.f;

void movePaddle(bool move_up_key_pressed, bool move_down_key_pressed);
void paddleRestriction();

public:
Paddle(float position_x, float position_y);
void update(bool move_up_key_pressed, bool move_down_key_pressed);
void render(RenderWindow* game_window);
RectangleShape getPaddleSprite();
};
}

40 changes: 40 additions & 0 deletions Header/UI/UIService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include<iostream>
#include "SFML/Graphics.hpp"

using namespace sf;
using namespace std;

class UIService
{
private:
Font font;
Text right_score_text;
Text left_score_text;
string font_path = "Assets/Fonts/Aloevera-OVoWO.ttf";

int font_size = 40;
Color font_color = Color::White;
string initial_score = "00";

float left_score_pos_x = 570.f;
float left_score_pos_y = 30.f;
float right_score_pos_x = 670.f;
float right_score_pos_y = 30.f;

int player1_score = 0;
int player2_score = 0;

void loadFontTexture();
void createLeftScoreText();
void createRightScoreText();
string formatScoreText(int score);
public:
UIService();
void render(RenderWindow* game_window);
void update();
void incrementPlayer1Score();
void incrementPlayer2Score();
};

23 changes: 23 additions & 0 deletions Header/Utility/TimeManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include<chrono>

namespace Utility
{
class TimeManager
{
private:
std::chrono::steady_clock::time_point previous_time;
float delta_time;

void updateDeltaTime();
float calculateDeltaTime();
void updatePreviousTime();

public:
void initialize();
void update();
float getDeltaTime();
};
}

30 changes: 14 additions & 16 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Header/Core/GameWindowManager.h"
#include "Header/Event/EventManager.h"
#include "Header/Core/GameLoop.h"

using namespace Core;
using namespace Eventmanager;

int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
GameLoop* game_loop = new GameLoop();

window.clear();
window.draw(shape);
window.display();
}
game_loop->initialize();
while (game_loop->isGameRunning())
{
game_loop->pollEvent();
game_loop->update();
game_loop->render();
}
}
20 changes: 20 additions & 0 deletions Pong-SFML.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,27 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Source\Gameplay\Boundary.cpp" />
<ClCompile Include="Source\Gameplay\GameplayManager.cpp" />
<ClCompile Include="Source\Gameplay\Paddle.cpp" />
<ClCompile Include="Source\Gameplay\Ball.cpp" />
<ClCompile Include="Source\Core\GameLoop.cpp" />
<ClCompile Include="Source\Event\EventManager.cpp" />
<ClCompile Include="Source\Core\GameWindowManager.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Source\Utility\TimeManager.cpp" />
<ClCompile Include="Source\UI\UIService.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Header\Gameplay\Boundary.h" />
<ClInclude Include="Header\Gameplay\GameplayManager.h" />
<ClInclude Include="Header\Gameplay\Ball.h" />
<ClInclude Include="Header\Core\GameLoop.h" />
<ClInclude Include="Header\Event\EventManager.h" />
<ClInclude Include="Header\Core\GameWindowManager.h" />
<ClInclude Include="Header\Gameplay\Paddle.h" />
<ClInclude Include="Header\Utility\TimeManager.h" />
<ClInclude Include="Header\UI\UIService.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Loading