Skip to content

Feature 6 movement and collision #37

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

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

using namespace sf;
using namespace Core;
using namespace CoustomEvent;
using namespace std;
using namespace Gameplay;

namespace Core {

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

public:
void initialize();

bool isGameRunning();
void update();
void pollEvent();
void render();
};
}
24 changes: 24 additions & 0 deletions Header/Core/GameWindowManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#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 render();
void clearGameWindow();
void displayGameWindow();
};
}
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>
#include <iostream>
using namespace sf;

namespace CoustomEvent {
class EventManager {
public:
void pollEvents(RenderWindow* game_window);
bool isKeyPressed(Keyboard::Key key);
bool isLeftMouseButtonClicked();

};
}
52 changes: 52 additions & 0 deletions Header/Gameplay/Ball/Ball.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "../../Header/Gameplay/Paddle/Paddle.h"

using namespace sf;
using namespace std;

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

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

const float scale_x = 0.02f;
const float scale_y = 0.02f;

const float radius = 10.0f;
const float position_x = 615.0f;
const float position_y = 335.0f;

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

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;

void loadTexture();
void initializeVariables();
void move();
void onCollision(Paddle* player1, Paddle* player2);

public:
Ball();
void handlePaddleCollision(Paddle* player1, Paddle* player2);
void handleBoundaryCollision();
void handleOutofBoundCollision();
void reset();
void update(Paddle* player1, Paddle* player2);
void render(RenderWindow* game_window);

};
}
59 changes: 59 additions & 0 deletions Header/Gameplay/Boundary/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:
RectangleShape topBoundary;
RectangleShape leftBoundary;
RectangleShape centerLine;
RectangleShape rightBoundary;
RectangleShape bottomBoundary;

//horizontal values for boundarys
const float horizontal_boundary_width = 1280.0f;
const float horizontal_boundary_height = 20.0f;

//vertical values for boundarys
const float vertical_boundary_width = 20.0f;
const float vertical_boundary_height = 720.0f;

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

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

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

const float bottom_position_x = 0.0f;
const float bottom_position_y = 700.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 update();
void render(RenderWindow* game_window);

};
}
35 changes: 35 additions & 0 deletions Header/Gameplay/GameplayManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "Paddle/Paddle.h"
#include "Ball/Ball.h"
#include "Boundary/Boundary.h"
#include "../../Header/Event/EventManager.h"

using namespace CoustomEvent;

namespace Gameplay {
class GameplayManager {
private:
float player1_position_x = 40.0f;
float player1_position_y = 300.0f;

float player2_position_x = 1210.0f;
float player2_position_y = 300.0f;

Ball* ball;
Paddle* player1;
Paddle* player2;
EventManager* event_manager;
Boundary* boundary;

void initialize();

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


};

}
31 changes: 31 additions & 0 deletions Header/Gameplay/Paddle/Paddle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#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;

float paddle_speed = 0.1f;
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);

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);
void render(RenderWindow* game_window);
};
}
42 changes: 25 additions & 17 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#include <iostream>
#include <SFML/Graphics.hpp>
#include "../../Header/Core/GameWindowManager.h"
#include "../../Header/Event/EventManager.h"
#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);

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

window.clear();
window.draw(shape);
window.display();
CoustomEvent::EventManager eventManager;
Core::GameWindowManager gameWindowManager;
GameLoop* game_loop_manager = new GameLoop();


gameWindowManager.initialize();
game_loop_manager->initialize();



while (game_loop_manager->isGameRunning()) {

game_loop_manager->pollEvent();
game_loop_manager->update();
game_loop_manager->render();
}
}
return 0;
}


18 changes: 17 additions & 1 deletion Pong-SFML.vcxproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
Expand Down Expand Up @@ -134,6 +134,22 @@
</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" />
</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" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Loading