Skip to content

Commit

Permalink
feat: 🚀 Done project, Added Controls and ReadME
Browse files Browse the repository at this point in the history
  • Loading branch information
Polaris66 committed Jul 29, 2023
1 parent 1290e85 commit 53c77fb
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 8 deletions.
13 changes: 13 additions & 0 deletions Cell.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <SFML/Graphics.hpp>
#include <iostream>

#include "Headers/Grid.hpp"
#include "Headers/Cell.hpp"
Expand Down Expand Up @@ -35,6 +36,18 @@ void Cell::setNeighbors(int _neighbors)
neighbors = _neighbors;
}

void Cell::toggle()
{
alive = !alive;
render();
}

void Cell::kill()
{
alive = false;
render();
}

void Cell::update()
{

Expand Down
Binary file modified Cell.o
Binary file not shown.
Binary file added Demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 16 additions & 5 deletions Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ void Grid::update()
}
}

void Grid::clear()
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
getCell(i, j)->kill();
}
}
}

void Grid::toggle(int xpos, int ypos)
{
getCell(xpos / size, ypos / size)->toggle();
}

void Grid::render(sf::RenderWindow &window)
{
for (int i = 0; i < x; i++)
Expand All @@ -75,9 +91,4 @@ void Grid::render(sf::RenderWindow &window)
window.draw(cell->shape);
}
}
}

Grid::~Grid()
{
free(cells);
}
Binary file modified Grid.o
Binary file not shown.
3 changes: 2 additions & 1 deletion Headers/Cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Cell
bool alive;
void render();
void update();

void toggle();
void kill();
void setNeighbors(int _neighbors);

Cell();
Expand Down
3 changes: 2 additions & 1 deletion Headers/Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class Grid

public:
Grid(int _size, int base_x, int base_y, int _x, int _y);
~Grid();

void update();
void render(sf::RenderWindow &window);
void toggle(int xpos, int ypos);
void clear();

Cell *getCell(int xpos, int ypos);
void setNeighbors(int xpos, int ypos);
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Game of Life

A simulation based on Conway's Game of Life.
Implemented in C++ using SFML Library.

![img](Demo.png)

## Rules

It's an example of Cellular Automata and follows the following rules:

- Any cell with 3 or more neighbors will die (Overpopulation).
- Any cell with 2 or less neighbors will die (Underpopulation).
- Any dead cell with 3 neighbors will come alive (Reproduction).

## Download Instructions

Download the run file and run it.

## Controls

Left Mouse Button - Toggle Cells
P - Pause/Play
C - Clear
R - Randomize
Right Arrow Key - Speed Up
Left Arrow Key - Slow Down
33 changes: 32 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int main()

// Create Clock
sf::Clock clock;
double timeBetweenFrames = 1.0;
// Main Loop
while (1 == window.isOpen())
{
Expand All @@ -61,6 +62,36 @@ int main()
{
paused = !paused;
}
if (event.key.code == sf::Keyboard::R)
{
grid = Grid(size, sideBarGap, titleHeight, x, y);
}
if (event.key.code == sf::Keyboard::C)
{
grid.clear();
}
if (event.key.code == sf::Keyboard::Right)
{
timeBetweenFrames /= 2;
}
if (event.key.code == sf::Keyboard::Left)
{
timeBetweenFrames *= 2;
}
}

if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
int xpos = event.mouseButton.x;
int ypos = event.mouseButton.y;

if ((xpos >= sideBarGap) && (xpos <= sideBarGap + width) && (ypos >= titleHeight) && (ypos <= titleHeight + height))
{
grid.toggle(xpos - sideBarGap, ypos - titleHeight);
}
}
}
}

Expand All @@ -69,7 +100,7 @@ int main()
// Draw Text
window.draw(title);

if (clock.getElapsedTime().asSeconds() > 1.0f && !paused)
if (clock.getElapsedTime().asSeconds() > timeBetweenFrames && !paused)
{
grid.update();
clock.restart();
Expand Down
Binary file modified main.o
Binary file not shown.
Binary file modified run
Binary file not shown.

0 comments on commit 53c77fb

Please sign in to comment.