Skip to content

Instantly share code, notes, and snippets.

@deleter8
Created July 11, 2014 21:18
Show Gist options
  • Save deleter8/1cd0c29353acfd16e2ea to your computer and use it in GitHub Desktop.
Save deleter8/1cd0c29353acfd16e2ea to your computer and use it in GitHub Desktop.
#include <vector>
#include <SFML/Graphics.hpp>
#include <math.h>
int main()
{
int resx = 1200;
int resy = resx * 16 / 25;
const int box_count_x = 25;
const int box_count_y = (box_count_x * 16) / 25;
const int box_max_x = box_count_x - 1;
const int box_max_y = box_count_y - 1;
const int walk_speed = box_max_y / 2;
int box_dimension = floor((float)resx / box_count_x) - 2;
int box_offset = box_dimension + 2;
sf::RenderWindow window(sf::VideoMode(resx, resy), "OneHourGame");
sf::Clock clock;
std::vector<sf::Shape*> boxes;
float grid[box_count_x][box_count_y];
float temp_grid[box_count_x][box_count_y];
for(int y = 0; y < box_count_y; y++)
{
for(int x = 0; x < box_count_x; x++)
{
sf::RectangleShape * shape = new sf::RectangleShape(sf::Vector2f(box_dimension, box_dimension));
shape->setPosition(x * box_offset, y * box_offset);
shape->setFillColor(sf::Color(20,20,20));
boxes.push_back(shape);
grid[x][y] = 0;
}
}
float cursor_x = 0;
float cursor_y = 0;
bool enter_pressed = false;
while (window.isOpen())
{
int elapsed = clock.restart().asMilliseconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
cursor_y-=walk_speed*elapsed;
if(cursor_y < 0) cursor_y = (box_count_y * 1000 - 1);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
cursor_y+=walk_speed*elapsed;
if(cursor_y > (box_count_y * 1000 - 1)) cursor_y = 0;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
cursor_x-=walk_speed*elapsed;
if(cursor_x < 0) cursor_x = (box_count_x * 1000 - 1);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
cursor_x+=walk_speed*elapsed;
if(cursor_x > (box_count_x * 1000 - 1)) cursor_x = 0;
}
int grid_x = (int)(cursor_x/1000);
int grid_y = (int)(cursor_y/1000);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return)){
if(!enter_pressed)
{
grid[grid_x][grid_y] += 9000;
grid[(grid_x+box_max_x)%box_count_x][grid_y] += 1250;
grid[(grid_x+1)%box_count_x][grid_y] += 1250;
grid[grid_x][(grid_y+box_max_y)%box_count_y] += 1250;
grid[grid_x][(grid_y+1)%box_count_y] += 1250;
}
enter_pressed = true;
}else{
enter_pressed = false;
}
grid[grid_x][grid_y] += .2 * elapsed;
for(int y = 0; y < box_count_y; y++)
{
for(int x = 0; x < box_count_x; x++)
{
temp_grid[x][y] = grid[x][y];
}
}
for(int y = 0; y < box_count_y; y++)
{
for(int x = 0; x < box_count_x; x++)
{
float offset = -temp_grid[x][y] + (temp_grid[(x+box_max_x)%box_count_x][y] + temp_grid[(x+1)%box_count_x][y] + temp_grid[x][(y+box_max_y)%box_count_y] + temp_grid[x][(y+1)%box_count_y])/8;
grid[x][y] += (offset/350) * elapsed;
if(grid[x][y]>1000)grid[x][y]=1000;
if(grid[x][y]<0)grid[x][y]=0;
}
}
for(int y = 0; y < box_count_y; y++)
{
for(int x = 0; x < box_count_x; x++)
{
int red_mod = grid[x][y]*2.2;
int blue_mod = 15 - red_mod*10;
if(blue_mod < 0) blue_mod = 0;
if(red_mod > 220) red_mod = 220;
boxes[y*box_count_x+x]->setFillColor(sf::Color(red_mod +20,20,20+ blue_mod));
}
}
window.clear();
for(int i = 0; i < boxes.size(); i++)
{
window.draw(*boxes[i]);
}
window.display();
sf::sleep(sf::milliseconds(10));
}
for(int i = 0; i < boxes.size(); i++)
{
delete boxes[i];
}
boxes.clear();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment