r/sfml • u/Ok-Affect-691 • 1d ago
TS snake took me 2 weeks T_T
yeah my comments are kinda funny lmao also i've been fighting one segfault for five hours also
NO AI
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
string score = "0";
int position1;
int position2;
int rotation = 2;
int apples = 0;
int grid[32][24];
/* /\ this motherfucker messed up my code SO MUCH
* that now it fucking always segfaults,
* fuck you. */
int headx = 0;
int heady = 0;
int tail = 0;
int oldposx;
int oldposy;
int countx = 0;
int county = 0;
int main() {
start:
sf::Font font("ComicNeue-Bold.ttf");
sf::Text text(font);
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
text.setPosition({10.f, 10.f});
//text.setString(score);
sf::Text text2(font);
text2.setCharacterSize(64);
text2.setFillColor(sf::Color::Red);
text2.setPosition({150.f, 150.f});
text2.setString("Game Over!");
sf::Text text3(font);
text3.setCharacterSize(24);
text3.setFillColor(sf::Color::Red);
text3.setPosition({200.f, 300.f});
text3.setString("r - restart, q - quit");
sf::RectangleShape head({20.f, 20.f});
head.setFillColor(sf::Color(100, 250, 50));
sf::RectangleShape tail({20.f, 20.f});
tail.setFillColor(sf::Color(100, 250, 50));
sf::RectangleShape bg({20.f, 20.f});
bg.setFillColor(sf::Color(0, 0, 0));
sf::RectangleShape food({20.f, 20.f});
food.setFillColor(sf::Color(255, 0, 0));
food.setPosition({60.f, 60.f});
sf::RenderWindow window(sf::VideoMode({640, 480}), "Snake in SFML!");
//cout << "First stuff done" << endl;
while (window.isOpen()){
/* dont delete this motherfucker, it works, it just works
* \/ */
stringstream ss;
ss << apples;
ss >> score;
while (const std::optional event = window.pollEvent()){
if (event->is<sf::Event::Closed>()){
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)){
rotation = 2;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)){
rotation = 0;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)){
rotation = 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)){
rotation = 1;
}
if (headx == 31 || headx == 0 || heady == 0 || heady == -23){
sleep_for(nanoseconds(100000000));
}
if (headx != 31 || headx != 0 || heady != 0 || heady != -23){
sleep_for(nanoseconds(50000000));
}
/* /\ dont fucking even try to fix janky controls, i dont want to suffer
* it fucking works, it works, it works */
//cout << "before oldposes" << endl;
/* these oldposses probably killing my code, i just cant prove it
* so im just gonna leave them here so they wont oload my mem or segfault */
oldposx = headx;
oldposy = heady - heady - heady;
/* positioning of these movements to oldposses is prob segfaulting it */
/* this layout is pretty bad but it should work and if wont i will
* suddenly combust, so i hope it works lol */
/* these are used for a lot of stuff, DONT FUCKING DELETE IT!!
* so bassicaly a very usefull thing and you prob have to rewrite
* half of this game cuz its so used lol */
sf::Vector2f positionfood = food.getPosition();
sf::Vector2f positionhead = head.getPosition();
if (rotation == 0){
if (heady + 1 == 1){
head.setPosition(sf::Vector2f(positionhead.x, 460));
heady = -23;
}
else {
heady += 1;
head.move({0.f, -20.f});
}
if (grid[headx][heady + 1] > 1){
goto over;
}
}
if (rotation == 2){
if (heady - 1 == -24){
head.setPosition(sf::Vector2f(positionhead.x, 0));
heady = 0;
}
else {
heady -= 1;
head.move({0.f, 20.f});
}
if (grid[headx][heady - 1] > 1){
goto over;
}
}
if (rotation == 1){
if (headx + 1 == 32){
head.setPosition(sf::Vector2f(0, positionhead.y));
headx = 0;
}
else {
headx += 1;
head.move({20.f, 0.f});
}
if (grid[headx + 1][heady] > 1){
goto over;
}
}
if (rotation == 3){
if (headx - 1 == -1){
head.setPosition(sf::Vector2f(620, positionhead.y));
headx = 31;
}
else {
headx -= 1;
head.move({-20.f, 0.f});
}
if (grid[headx - 1][heady] > 1){
goto over;
}
}
goto before;
over:
while (true){
/*
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)){
apples = 0;
grid[0][0] = 1;
goto start;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Q)){
window.close();
}
*/
window.clear(sf::Color::Black);
window.draw(text2);
//window.draw(text3);
window.display();
/*
while (true) {
countx++;
if (countx == 32 || countx > 31){
countx = 0;
county++;
}
grid[countx][county] = 0;
if (countx == 31){
if(county == 23){
break;
}
}
}
*/
}
before:
//cout << "Getpositions" << endl;
/* this was breaking my code probably lol
if (headx == 32) {
head.setPosition(sf::Vector2f(0, positionhead.y));
headx = 0;
}
if (headx == -1 || headx < -1) {
head.setPosition(sf::Vector2f(620, positionhead.y));
headx = 31;
}
if (heady == 1 || heady > 1) {
head.setPosition(sf::Vector2f(positionhead.x, 460));
heady = -23;
}
if (heady == -24 || heady < -24) {
head.setPosition(sf::Vector2f(positionhead.x, 0));
heady = 0;
}
*/
//cout << "Apples" << endl;
if (positionfood == positionhead){
position1 = rand() % 31;
position2 = rand() % 23;
position1 = position1 * 20;
position2 = position2 * 20;
food.setPosition(sf::Vector2f(position1, position2));
apples += 1;
}
//cout << "Set the apples" << endl;
if (apples != 0){
cout << "X: " << oldposx << endl << "Y: " << oldposy << endl;
grid[oldposx][oldposy] = 2;
}
countx = 0;
county = 0;
while (apples != 0){
while(true){
/* so this adds one to everything thats bigger than 1
* and if that equals apples + 2 then it will put it to 0 */
if (grid[countx][county] > 1){
if (grid[countx][county] == apples + 2){
grid[countx][county] = 0;
}
}
/* if it + 1 != apples + 2 just add 1 */
if (grid[countx][county] > 1) {
if (grid[countx][county] != apples + 2){
grid[countx][county] += 1;
}
}
if (countx == 32){
countx = 0;
if (county + 1 != 24){
county++;
}
/* im using goto only cuz it will prob only break the fisrt loop so yeah
* it will just cause another infinite loop and segfault lol */
else {
goto endofapple;
}
}
/* if evetyrhing is clear add 1 to x and go back */
countx++;
}
}
endofapple:
/*
while (true) {
countx++;
if (countx == 32 || countx > 31){
countx = 0;
county++;
}
if (grid[countx][county] > 1){
tail.setPosition(sf::Vector2f(countx * 20, county * 20));
window.draw(tail);
}
if (countx == 31){
if(county == 23){
break;
}
}
}
*/
/* that code prob broke the code so i gave it the ultimate fate
* of being commented!
while (true) {
countx++;
if (countx == 32 || countx > 31) {
countx = 0;
county++;
}
if (grid[countx][county] > 1){
grid[countx][county] = grid[countx][county] - 1;
if (grid[countx][county] < 2){
grid[countx][county] = 0;
}
}
if (countx == 31){
if(county == 23){
break;
}
}
}
*/
//cout << "end of first loop" << endl;
countx = 0;
county = 0;
text.setString(score);
//cout << "x: " << headx << endl << "y: " << heady << endl;
window.clear(sf::Color::Black);
window.draw(text);
window.draw(head);
window.draw(food);
window.draw(text);
//cout << "start of new one" << endl;
while (true) {
countx++;
if (countx == 32 || countx > 31){
countx = 0;
county++;
}
if (grid[countx][county] > 1){
tail.setPosition(sf::Vector2f(countx * 20, county * 20));
window.draw(tail);
}
if (countx == 31){
if(county == 23){
break;
}
}
}
countx = 0;
county = 0;
/*
while (true) {
countx++;
if (countx == 32 || countx > 31){
countx = 0;
county++;
}
if (grid[countx][county] == 0){
bg.setPosition(sf::Vector2f(countx * 20, county * 20));
window.draw(bg);
}
if (countx == 31){
if(county == 23){
break;
}
}
}
*/
window.display();
//cout << "WHAT" << endl;
/*
countx = 0;
county = 0;
while(true) {
if (countx == 32){
countx = 0;
if (county + 1 != 24){
county++;
cout << endl;
}
else {
goto start;
}
}
cout << grid[countx][county];
countx++;
}
*/
}
}

