Skip to content

Instantly share code, notes, and snippets.

@eXpl0it3r
Last active December 10, 2022 00:07
Show Gist options
  • Save eXpl0it3r/6e7606ba6e14251d1a3ed5a6c3b7d55c to your computer and use it in GitHub Desktop.
Save eXpl0it3r/6e7606ba6e14251d1a3ed5a6c3b7d55c to your computer and use it in GitHub Desktop.
ChatGPT vs SFML vs Candy Crush
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int GRID_SIZE = 8;
const int CELL_SIZE = 50;
// The game states
enum GameState
{
MainMenu,
Playing
};
// The structure for a menu button
struct Button
{
sf::RectangleShape shape;
sf::Text text;
};
// Represents a candy on the grid
struct Candy
{
sf::Color color;
sf::RectangleShape shape;
};
// The currently selected candy
int selectedI = -1;
int selectedJ = -1;
// Represents the game grid
struct Grid
{
std::vector<std::vector<Candy>> cells;
// Initialize the grid with random candy colors
void init()
{
for (int i = 0; i < GRID_SIZE; i++)
{
cells.push_back(std::vector<Candy>());
for (int j = 0; j < GRID_SIZE; j++)
{
Candy candy;
candy.color = sf::Color(rand() % 256, rand() % 256, rand() % 256);
candy.shape = sf::RectangleShape(sf::Vector2f(CELL_SIZE, CELL_SIZE));
candy.shape.setFillColor(candy.color);
candy.shape.setPosition(i * CELL_SIZE, j * CELL_SIZE);
cells[i].push_back(candy);
}
}
}
// Draw the grid to the given window
void draw(sf::RenderWindow& window)
{
for (int i = 0; i < GRID_SIZE; i++)
{
for (int j = 0; j < GRID_SIZE; j++)
{
window.draw(cells[i][j].shape);
}
}
}
// Swap the candies at the given indices
void swap(int i1, int j1, int i2, int j2)
{
Candy temp = cells[i1][j1];
cells[i1][j1] = cells[i2][j2];
cells[i2][j2] = temp;
}
};
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Candy Crush");
// Set the framerate limit to 144 Hz
window.setFramerateLimit(144);
// Set the font
sf::Font font;
if (!font.loadFromFile("fonts/arial.ttf"))
{
std::cout << "Error loading font" << std::endl;
return -1;
}
// Initialize the grid
Grid grid;
grid.init();
// Set the selected candy to null initially
Candy* selected = nullptr;
// Set the initial game state to MainMenu
GameState state = MainMenu;
// Create the buttons
Button playButton;
playButton.shape = sf::RectangleShape(sf::Vector2f(100, 50));
playButton.shape.setFillColor(sf::Color::White);
playButton.shape.setPosition(350, 250);
playButton.text = sf::Text("Play", font);
playButton.text.setCharacterSize(24);
playButton.text.setFillColor(sf::Color::Black);
playButton.text.setPosition(370, 260);
Button quitButton;
quitButton.shape = sf::RectangleShape(sf::Vector2f(100, 50));
quitButton.shape.setFillColor(sf::Color::White);
quitButton.shape.setPosition(350, 350);
quitButton.text = sf::Text("Quit", font);
quitButton.text.setCharacterSize(24);
quitButton.text.setFillColor(sf::Color::Black);
quitButton.text.setPosition(370, 360);
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed)
{
window.close();
}
// Handle input events
if (state == MainMenu)
{
// Mouse moved
if (event.type == sf::Event::MouseMoved)
{
// Get the mouse position in world coordinates
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Vector2i(event.mouseMove.x, event.mouseMove.y));
// Check if the mouse is over the "Play" button
if (playButton.shape.getGlobalBounds().contains(mousePos))
{
// Set the "Play" button color to light blue
playButton.shape.setFillColor(sf::Color(100, 149, 237));
}
else
{
// Set the "Play" button color to white
playButton.shape.setFillColor(sf::Color::White);
}
// Check if the mouse is over the "Quit" button
if (quitButton.shape.getGlobalBounds().contains(mousePos))
{
// Set the "Quit" button color to light blue
quitButton.shape.setFillColor(sf::Color(100, 149, 237));
}
else
{
// Set the "Quit" button color to white
quitButton.shape.setFillColor(sf::Color::White);
}
}
// Mouse button pressed
if (event.type == sf::Event::MouseButtonPressed)
{
// Check if the left mouse button was pressed
if (event.mouseButton.button == sf::Mouse::Left)
{
// Get the mouse position in world coordinates
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
// Check if the mouse is over the "Play" button
if (playButton.shape.getGlobalBounds().contains(mousePos))
{
// Start the game
state = Playing;
}
// Check if the mouse is over the "Quit" button
if (quitButton.shape.getGlobalBounds().contains(mousePos))
{
// Quit the game
window.close();
}
}
}
}
else if (state == Playing)
{
// Mouse button pressed
if (event.type == sf::Event::MouseButtonPressed)
{
// Check if the left mouse button was pressed
if (event.mouseButton.button == sf::Mouse::Left)
{
// Get the mouse position
int x = event.mouseButton.x;
int y = event.mouseButton.y;
// Calculate the indices of the clicked candy
int i = x / CELL_SIZE;
int j = y / CELL_SIZE;
// Check if the clicked candy is adjacent to the selected candy
if (selected && (std::abs(i - selectedI) + std::abs(j - selectedJ) == 1))
{
// Swap the selected candy with the clicked candy
grid.swap(i, j, selectedI, selectedJ);
// Set the selected candy to null
selected = nullptr;
}
else
{
// Set the selected candy
selected = &grid.cells[i][j];
selectedI = i;
selectedJ = j;
}
}
}
}
}
// Clear screen
window.clear();
// Draw the game elements
if (state == MainMenu)
{
// Draw the buttons
window.draw(playButton.shape);
window.draw(playButton.text);
window.draw(quitButton.shape);
window.draw(quitButton.text);
}
else if (state == Playing)
{
// Draw the grid
grid.draw(window);
}
// Update the window
window.display();
}
return 0;
}
# Minimum required version of CMake
cmake_minimum_required(VERSION 3.15)
# Project name
project(candy_crush)
# Download and build SFML
include(FetchContent)
FetchContent_Declare(
sfml
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 2.6.x
)
FetchContent_MakeAvailable(sfml)
# Add the game executable
add_executable(candy_crush candy_crush.cpp)
# Specify the required compiler features for the game executable
target_compile_features(candy_crush PRIVATE cxx_std_17)
# Link SFML to the game executable
target_link_libraries(candy_crush sfml-graphics sfml-window sfml-system)
# Install the game and SFML files
install(TARGETS candy_crush
DESTINATION .
)
install(FILES ${SFML_BINARIES}
DESTINATION .
)
install(FILES "arial.ttf"
DESTINATION fonts
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment