Skip to content
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
31 changes: 31 additions & 0 deletions Header/Core/GameLoop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Core/GameWindowManager.h"
#include "../../Header/Event/EventManager.h"
#include "../../Header/Gameplay/GameplayManager.h"
#include "../../Header/Gameplay/Paddle/Paddle.h"
#include "../../Header/Gameplay/Ball/Ball.h"

using namespace sf;
using namespace Core;
using namespace Event;
using namespace std;
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();
};
}
26 changes: 26 additions & 0 deletions Header/Core/GameWindowManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#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 clearGameWindow();
void displayGameWindow();
//void render();
};
}
14 changes: 14 additions & 0 deletions Header/Event/EventManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;

namespace Event
{
class EventManager
{
public:
void pollEvents(RenderWindow* game_window); // Process all events
bool isKeyPressed(sf::Keyboard::Key key); // Check specific key
bool isLeftMouseButtonClicked();
};
}
70 changes: 70 additions & 0 deletions Header/Gameplay/Ball/Ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Gameplay/Paddle/Paddle.h"
using namespace sf;
using namespace std;

namespace Gameplay
{
enum class BallState
{
Idle,
Moving
};

class Ball
{
private:

float ball_speed = 5.0f;
Vector2f velocity = Vector2f(ball_speed, ball_speed);

const float speed_multiplier = 100.0f;

Texture pong_ball_texture;
Sprite pong_ball_sprite;

string texture_path = "Assets/Textures/Ball.png";

const float scale_x = 0.06f;
const float scale_y = 0.06f;

const float position_x = 615.0f;
const float position_y = 325.0f;

const float top_boundary = 20.0f;
const float bottom_boundary = 700.0f;

const float left_boundary = 0.0f;
const float right_boundary = 1280.0f;

//Center Position
const float center_position_x = 615.0f;
const float center_position_y = 325.0f;

float delay_duration = 2.0f;
float elapsed_delay_time = 0.0f;
BallState current_state = BallState::Idle;

void move(TimeService* timeService);
void updateDelayTime(float deltaTime);

void loadTexture();
void initializeVariables();

void reset();

public:

Ball();

void update(Paddle* player1, Paddle* player2, TimeService* time_service);
void onCollision(Paddle* player1, Paddle* player2);
void render(RenderWindow* game_window);

void handlePaddleCollision(Paddle* player1, Paddle* player2);
void handleBoudaryCollision();
void handleOutofBoundCollision();

};
}
56 changes: 56 additions & 0 deletions Header/Gameplay/Boundary/Boundary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;

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

const float horizontal_boundary_width = 1280.0f;
const float horizontal_boundary_height = 20.0f;

const float top_position_x = 0.0f;
const float top_position_y = 0.0f;

const float bottom_position_x = 0.0f;
const float bottom_position_y = 700.0f;

const float vertical_boundary_width = 20.0f;
const float vertical_boundary_height = 720.0f;

const float left_position_x = 0.0f;
const float left_position_y = 0.0f;

const float right_position_x = 1260.0f;
const float right_position_y = 0.0f;

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;

const Color boundary_color = Color::Blue;
const Color center_line_color = Color::White;

void createTopBoundary();
void createBottomBoundary();
void createLeftBoundary();
void createRightBoundary();

void createCenterLine();

public:
Boundary();

void update();
void render(RenderWindow* game_window);
};
}
40 changes: 40 additions & 0 deletions Header/Gameplay/GameplayManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include "../../Header/Gameplay/Paddle/Paddle.h"
#include "../../Header/Gameplay/Ball/Ball.h"
#include "../../Header/Core/GameWindowManager.h"
#include "../../Header/Event/EventManager.h"
#include "../../Header/Gameplay/Boundary/Boundary.h"
#include"../../Header/Utility/TimeService.h"

using namespace Event;
using namespace Utility;


namespace Gameplay
{
class GameplayManager
{

private:
EventManager* event_manager;
TimeService* time_service;

float player1_position_x = 40.0f;
float player1_position_y = 300.0f;

float player2_postion_x = 1210.0f;
float player2_postion_y = 300.0f;

Boundary* boundary = new Boundary;
Ball* ball = new Ball();
Paddle* player1 = new Paddle(player1_position_x, player1_position_y);
Paddle* player2 = new Paddle(player2_postion_x, player2_postion_y);

public:
GameplayManager(EventManager* manager);


void update();
void render(RenderWindow* game_window);
};
}
36 changes: 36 additions & 0 deletions Header/Gameplay/Paddle/Paddle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <SFML/Graphics.hpp>
#include"../../Header/Utility/TimeService.h"

using namespace Utility;
using namespace sf;

namespace Gameplay
{
class Paddle
{
private:
RectangleShape paddle_sprite;

float speedMultiplier = 100.0f;

const float paddle_width = 20.0f;
const float paddle_height = 140.0f;

float paddleSpeed = 5.0f;
float topBoundary = 20.0f;
float bottomBoundary = 700.0f;

void createPaddle(float position_x, float position_y);
void movePaddle(bool move_up_key_pressed, bool move_down_key_pressed, TimeService* time_service);

public:
Paddle(float position_x, float position_y);

RectangleShape getPaddleSprite();
void reset(float position_x, float position_y);

void update(bool move_up_key_pressed, bool move_down_key_pressed, TimeService* time_service);
void render(RenderWindow* game_window);
};
}
22 changes: 22 additions & 0 deletions Header/Utility/TimeService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <chrono>

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

void updateDeltaTime();
float calculateDeltaTime();
void updatePreviousTime(); // Update previous_time to the current time

public:

void initialize();
void update();
float getDeltaTime();
};
}
30 changes: 15 additions & 15 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include <iostream>
#include <SFML/Graphics.hpp>
#include "../../Header/Core/GameLoop.h"
using namespace sf;
using namespace Core;

int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
// Step 1: Create the GameLoop object
GameLoop* game_loop_manager = new GameLoop();

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// Step 2: Initialize the game environment
game_loop_manager->initialize();

window.clear();
window.draw(shape);
window.display();
// Step 3: Run the game loop
while (game_loop_manager->isGameRunning())
{
game_loop_manager->pollEvent();
game_loop_manager->update();
game_loop_manager->render();
}

return 0;
}
18 changes: 18 additions & 0 deletions Pong-SFML.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Main.cpp" />
<ClCompile Include="Source\Core\GameLoop.cpp" />
<ClCompile Include="Source\Core\GameWindowManager.cpp" />
<ClCompile Include="Source\Event\EventManager.cpp" />
<ClCompile Include="Source\Gameplay\Ball\Ball.cpp" />
<ClCompile Include="Source\Gameplay\Boundary\Boundary.cpp" />
<ClCompile Include="Source\Gameplay\GameplayManager.cpp" />
<ClCompile Include="Source\Gameplay\Paddle\Paddle.cpp" />
<ClCompile Include="Source\Utility\TimeService.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Header\Core\GameLoop.h" />
<ClInclude Include="Header\Core\GameWindowManager.h" />
<ClInclude Include="Header\Event\EventManager.h" />
<ClInclude Include="Header\Gameplay\Ball\Ball.h" />
<ClInclude Include="Header\Gameplay\Boundary\Boundary.h" />
<ClInclude Include="Header\Gameplay\GameplayManager.h" />
<ClInclude Include="Header\Gameplay\Paddle\Paddle.h" />
<ClInclude Include="Header\Utility\TimeService.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Loading