-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.cpp
More file actions
79 lines (70 loc) · 2.67 KB
/
Copy pathgame.cpp
File metadata and controls
79 lines (70 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "grid.h"
#include <SFML/Graphics.hpp>
struct game
{
int width, height, cellwidth, genskip;
bool paused;
grid* g;
sf::RenderWindow* window;
game(int w, int h, int t = 16, int gen = 1) : width(w), height(h), cellwidth(t), genskip(gen), paused(true)
{
g = new grid(width / cellwidth, height / cellwidth);
window = new sf::RenderWindow(sf::VideoMode(width, height), "Conway's Game of Life");
}
void start()
{
while (window->isOpen()) {
sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed)
window->close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Space) paused = !paused;
if (event.key.code == sf::Keyboard::Escape) window->close();
if (event.key.code == sf::Keyboard::C) g->clear();
if (event.key.code == sf::Keyboard::R) g->randomize();
if (event.key.code == sf::Keyboard::Equal) ++genskip;
if (event.key.code == sf::Keyboard::Dash && genskip > 0) --genskip;
if (event.key.code == sf::Keyboard::Return) genskip = 1;
}
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
int x = event.mouseButton.x;
int y = event.mouseButton.y;
if (x >= 0 && x < width && y >= 0 && y < height)
g->cells[x / (width / g->width)][y / (height / g->height)].flip_state();
}
}
}
window->clear();
draw();
window->display();
if (!paused) {
for (int i = 0; i < genskip; ++i)
g->update();
}
}
}
void draw()
{
for (int i = 0; i < g->width; ++i){
for (int j = 0; j < g->height; ++j) {
sf::RectangleShape square(sf::Vector2f(width / g->width, height / g->height));
square.setPosition(cellwidth * i, cellwidth * j);
square.setOutlineThickness(1);
square.setOutlineColor(sf::Color(49, 79, 79));
if (g->cells[i][j].is_alive())
square.setFillColor(sf::Color(0, 0, 0));
else
square.setFillColor(sf::Color(255, 255, 255));
window->draw(square);
}
}
}
};
int main()
{
game g(1920, 1080, 12);
g.start();
return 0;
}