Я создаю игру с бесконечной трассой. Трасса генерируется не так, как хотелось бы
На картинке изображена трасса. Управлением змейкой пока не занималась.
Нужно, чтобы повороты соединялись и трасса не изменялась внезапно (на картинке это показать невозможно). Также желательно убрать мерцание (если это возможно)
Весь код игры на данный момент (проблема в функции game):
#include <iostream>
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Screen dimension constants
constexpr int SCREEN_WIDTH { 700 };
constexpr int SCREEN_HEIGHT{ 635 };
SDL_Window* window{ nullptr };
SDL_Surface* screenSurface{ nullptr };
SDL_Surface* mainScreen{ nullptr };
SDL_Surface* gameScreen{ nullptr };
SDL_Surface* snake{ nullptr };
SDL_Surface* road0{ nullptr };
SDL_Surface* road1{ nullptr };
SDL_Surface* road2{ nullptr };
SDL_Surface* road3{ nullptr };
SDL_Surface* road4{ nullptr };
SDL_Surface* road5{ nullptr };
SDL_Surface* road0_left_center{ nullptr };
SDL_Surface* road1_left_center{ nullptr };
SDL_Surface* road2_left_center{ nullptr };
SDL_Surface* road3_left_center{ nullptr };
SDL_Surface* road4_left_center{ nullptr };
SDL_Surface* road5_left_center{ nullptr };
SDL_Surface* road0_right_center{ nullptr };
SDL_Surface* road1_right_center{ nullptr };
SDL_Surface* road2_right_center{ nullptr };
SDL_Surface* road3_right_center{ nullptr };
SDL_Surface* road4_right_center{ nullptr };
SDL_Surface* road5_right_center{ nullptr };
SDL_Surface* road0_left{ nullptr };
SDL_Surface* road1_left{ nullptr };
SDL_Surface* road2_left{ nullptr };
SDL_Surface* road3_left{ nullptr };
SDL_Surface* road4_left{ nullptr };
SDL_Surface* road5_left{ nullptr };
SDL_Surface* road0_right{ nullptr };
SDL_Surface* road1_right{ nullptr };
SDL_Surface* road2_right{ nullptr };
SDL_Surface* road3_right{ nullptr };
SDL_Surface* road4_right{ nullptr };
SDL_Surface* road5_right{ nullptr };
SDL_Surface* bg0{ nullptr };
SDL_Surface* bg1{ nullptr };
SDL_Surface* bg2{ nullptr };
SDL_Surface* bg3{ nullptr };
SDL_Surface* bg4{ nullptr };
SDL_Surface* bg5{ nullptr };
SDL_Surface* bg0_left_center{ nullptr };
SDL_Surface* bg1_left_center{ nullptr };
SDL_Surface* bg2_left_center{ nullptr };
SDL_Surface* bg3_left_center{ nullptr };
SDL_Surface* bg4_left_center{ nullptr };
SDL_Surface* bg5_left_center{ nullptr };
SDL_Surface* bg0_right_center{ nullptr };
SDL_Surface* bg1_right_center{ nullptr };
SDL_Surface* bg2_right_center{ nullptr };
SDL_Surface* bg3_right_center{ nullptr };
SDL_Surface* bg4_right_center{ nullptr };
SDL_Surface* bg5_right_center{ nullptr };
SDL_Surface* bg0_left{ nullptr };
SDL_Surface* bg1_left{ nullptr };
SDL_Surface* bg2_left{ nullptr };
SDL_Surface* bg3_left{ nullptr };
SDL_Surface* bg4_left{ nullptr };
SDL_Surface* bg5_left{ nullptr };
SDL_Surface* bg0_right{ nullptr };
SDL_Surface* bg1_right{ nullptr };
SDL_Surface* bg2_right{ nullptr };
SDL_Surface* bg3_right{ nullptr };
SDL_Surface* bg4_right{ nullptr };
SDL_Surface* bg5_right{ nullptr };
bool init();
bool loadMedia();
void close();
using namespace std;
int game()
{
//Apply the image
SDL_BlitSurface(gameScreen, nullptr, screenSurface, nullptr);
//Update the surface
SDL_UpdateWindowSurface(window);
int y0 = 635;
int y1 = 495;
int y2 = 355;
int y3 = 215;
int y4 = 75;
int y5 = -65;
int x0 = 280;
int x1 = 280;
int x2 = 280;
int x3 = 280;
int x4 = 280;
bool is1 = false;
bool has_turn = false;
int a0 = 0;
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a = 3;
SDL_Rect dstrect_road0;
SDL_Rect dstrect_road1;
SDL_Rect dstrect_road2;
SDL_Rect dstrect_road3;
SDL_Rect dstrect_road4;
SDL_Rect dstrect_road5;
while (true)
{
//Apply the image
dstrect_road0.x = x2;
dstrect_road0.y = y0;
if (y0 > y1) {
if(a == 1) {
dstrect_road0.x = dstrect_road0.x + 140;
}
if(a == 2) {
dstrect_road0.x = dstrect_road0.x - 140;
}
if (a == 0) {
if (dstrect_road1.x != x2) {
dstrect_road0.x = dstrect_road1.x;
}
}
}
SDL_BlitSurface(road0, nullptr, screenSurface, &dstrect_road0);
if (y0 == -65) {
a = rand() % 3;
}
if (has_turn == true)
{
if (x2 == 280)
{
if (a == 1)
{
x2 = 140;
SDL_Rect dstrect_road5_left_center;
dstrect_road5_left_center.x = x2;
dstrect_road5_left_center.y = y0;
SDL_BlitSurface(road5_left_center, nullptr, screenSurface, &dstrect_road5_left_center);
}
if (a == 2)
{
x2 = 420;
SDL_Rect dstrect_road0_right_center;
dstrect_road0_right_center.x = x2;
dstrect_road0_right_center.y = y0;
SDL_BlitSurface(road0_right_center, nullptr, screenSurface, &dstrect_road0_right_center);
}
}
if (x2 == 420)
{
if (a == 1)
{
x2 = 280;
SDL_Rect dstrect_road0;
dstrect_road0.x = x2;
dstrect_road0.y = y0;
SDL_BlitSurface(road0, nullptr, screenSurface, &dstrect_road0);
}
if (a == 2)
{
x2 = 560;
SDL_Rect dstrect_road0_right;
dstrect_road0_right.x = x2;
dstrect_road0_right.y = y0;
SDL_BlitSurface(road0_right, nullptr, screenSurface, &dstrect_road0_right);
}
}
if (x2 == 140)
{
if (a == 1)
{
x2 = 0;
SDL_Rect dstrect_road0_left;
dstrect_road0_left.x = x2;
dstrect_road0_left.y = y0;
SDL_BlitSurface(road0_left, nullptr, screenSurface, &dstrect_road0_left);
}
if (a == 2)
{
x2 = 280;
SDL_Rect dstrect_road0;
dstrect_road0.x = x2;
dstrect_road0.y = y0;
SDL_BlitSurface(road0, nullptr, screenSurface, &dstrect_road0);
}
}
if (x2 == 560)
{
if (a == 1)
{
x2 = 420;
SDL_Rect dstrect_road0_right_center;
dstrect_road0_right_center.x = x2;
dstrect_road0_right_center.y = y0;
SDL_BlitSurface(road0_right_center, nullptr, screenSurface, &dstrect_road0_right_center);
}
}
if (x2 == 0)
{
if (a == 2)
{
x2 = 140;
SDL_Rect dstrect_road0_left_center;
dstrect_road0_left_center.x = x2;
dstrect_road0_left_center.y = y0;
SDL_BlitSurface(road0_left_center, nullptr, screenSurface, &dstrect_road0_left_center);
}
}
}
//Apply the image
dstrect_road1.x = x2;
dstrect_road1.y = y1;
if (y1 > y2) {
if (a == 1) {
dstrect_road1.x = dstrect_road1.x + 140;
}
if (a == 2) {
dstrect_road1.x = dstrect_road1.x - 140;
}
if (a == 0) {
if (dstrect_road2.x != x2) {
dstrect_road1.x = dstrect_road2.x;
}
}
}
SDL_BlitSurface(road1, nullptr, screenSurface, &dstrect_road1);
if (y1 == -65) {
a = rand() % 3;
}
if (has_turn == true)
{
if (x2 == 280)
{
if (a == 1)
{
x2 = 140;
SDL_Rect dstrect_road1_left_center;
dstrect_road1_left_center.x = x2;
dstrect_road1_left_center.y = y1;
SDL_BlitSurface(road1_left_center, nullptr, screenSurface, &dstrect_road1_left_center);
}
if (a == 2)
{
x2 = 420;
SDL_Rect dstrect_road1_right_center;
dstrect_road1_right_center.x = x2;
dstrect_road1_right_center.y = y1;
SDL_BlitSurface(road1_right_center, nullptr, screenSurface, &dstrect_road1_right_center);
}
}
if (x2 == 420)
{
if (a == 1)
{
x2 = 280;
dstrect_road1.x = x2;
dstrect_road1.y = y1;
SDL_BlitSurface(road1, nullptr, screenSurface, &dstrect_road1);
}
if (a == 2)
{
x2 = 560;
SDL_Rect dstrect_road1_right;
dstrect_road1_right.x = x2;
dstrect_road1_right.y = y1;
SDL_BlitSurface(road1_right, nullptr, screenSurface, &dstrect_road1_right);
}
}
if (x2 == 140)
{
if (a == 1)
{
x2 = 0;
SDL_Rect dstrect_road1_left;
dstrect_road1_left.x = x2;
dstrect_road1_left.y = y1;
SDL_BlitSurface(road1_left, nullptr, screenSurface, &dstrect_road1_left);
}
if (a == 2)
{
x2 = 280;
dstrect_road1.x = x2;
dstrect_road1.y = y1;
SDL_BlitSurface(road1, nullptr, screenSurface, &dstrect_road1);
}
}
if (x2 == 560)
{
if (a == 1)
{
x2 = 420;
SDL_Rect dstrect_road1_right_center;
dstrect_road1_right_center.x = x2;
dstrect_road1_right_center.y = y1;
SDL_BlitSurface(road1_right_center, nullptr, screenSurface, &dstrect_road1_right_center);
}
}
if (x2 == 0)
{
if (a == 2)
{
x2 = 140;
SDL_Rect dstrect_road1_left_center;
dstrect_road1_left_center.x = x2;
dstrect_road1_left_center.y = y1;
SDL_BlitSurface(road1_left_center, nullptr, screenSurface, &dstrect_road1_left_center);
}
}
}
//Apply the image
dstrect_road2.x = x2;
dstrect_road2.y = y2;
if (y2 > y2) {
if (a == 1) {
dstrect_road2.x = dstrect_road2.x + 140;
}
if (a == 2) {
dstrect_road2.x = dstrect_road2.x - 140;
}
if (a == 0) {
if (dstrect_road3.x != x2) {
dstrect_road2.x = dstrect_road3.x;
}
}
}
SDL_BlitSurface(road2, nullptr, screenSurface, &dstrect_road2);
if (y2 == -65) {
a = rand() % 3;
}
if (has_turn == true)
{
if (x2 == 280)
{
if (a == 1)
{
x2 = 140;
SDL_Rect dstrect_road2_left_center;
dstrect_road2_left_center.x = x2;
dstrect_road2_left_center.y = y2;
SDL_BlitSurface(road2_left_center, nullptr, screenSurface, &dstrect_road2_left_center);
}
if (a == 2)
{
x2 = 420;
SDL_Rect dstrect_road2_right_center;
dstrect_road2_right_center.x = x2;
dstrect_road2_right_center.y = y2;
SDL_BlitSurface(road2_right_center, nullptr, screenSurface, &dstrect_road2_right_center);
}
}
if (x2 == 420)
{
if (a == 1)
{
x2 = 280;
dstrect_road2.x = x2;
dstrect_road2.y = y2;
SDL_BlitSurface(road2, nullptr, screenSurface, &dstrect_road2);
}
if (a == 2)
{
x2 = 560;
SDL_Rect dstrect_road2_right;
dstrect_road2_right.x = x2;
dstrect_road2_right.y = y2;
SDL_BlitSurface(road2_right, nullptr, screenSurface, &dstrect_road2_right);
}
}
if (x2 == 140)
{
if (a == 1)
{
x2 = 0;
SDL_Rect dstrect_road2_left;
dstrect_road2_left.x = x2;
dstrect_road2_left.y = y2;
SDL_BlitSurface(road2_left, nullptr, screenSurface, &dstrect_road2_left);
}
if (a == 2)
{
x2 = 280;
dstrect_road2.x = x2;
dstrect_road2.y = y2;
SDL_BlitSurface(road2, nullptr, screenSurface, &dstrect_road2);
}
}
if (x2 == 560)
{
if (a == 1)
{
x2 = 420;
SDL_Rect dstrect_road2_right_center;
dstrect_road2_right_center.x = x2;
dstrect_road2_right_center.y = y2;
SDL_BlitSurface(road2_right_center, nullptr, screenSurface, &dstrect_road2_right_center);
}
}
if (x2 == 0)
{
if (a == 2)
{
x2 = 140;
SDL_Rect dstrect_road2_left_center;
dstrect_road2_left_center.x = x2;
dstrect_road2_left_center.y = y2;
SDL_BlitSurface(road2_left_center, nullptr, screenSurface, &dstrect_road2_left_center);
}
}
}
//Apply the image
dstrect_road3.x = x2;
dstrect_road3.y = y3;
if (y3 > y4) {
if (a == 1) {
dstrect_road3.x = dstrect_road3.x + 140;
}
if (a == 2) {
dstrect_road3.x = dstrect_road3.x - 140;
}
if (a == 0) {
if (dstrect_road4.x != x2) {
dstrect_road3.x = dstrect_road4.x;
}
}
}
SDL_BlitSurface(road3, nullptr, screenSurface, &dstrect_road3);
if (y3 == -65) {
a = rand() % 3;
}
if (has_turn == true)
{
if (x2 == 280)
{
if (a == 1)
{
x2 = 140;
SDL_Rect dstrect_road3_left_center;
dstrect_road3_left_center.x = x2;
dstrect_road3_left_center.y = y3;
SDL_BlitSurface(road3_left_center, nullptr, screenSurface, &dstrect_road3_left_center);
}
if (a == 2)
{
x2 = 420;
SDL_Rect dstrect_road3_right_center;
dstrect_road3_right_center.x = x2;
dstrect_road3_right_center.y = y3;
SDL_BlitSurface(road3_right_center, nullptr, screenSurface, &dstrect_road3_right_center);
}
}
if (x2 == 420)
{
if (a == 1)
{
x2 = 280;
dstrect_road3.x = x2;
dstrect_road3.y = y3;
SDL_BlitSurface(road3, nullptr, screenSurface, &dstrect_road3);
}
if (a == 2)
{
x2 = 560;
SDL_Rect dstrect_road3_right;
dstrect_road3_right.x = x2;
dstrect_road3_right.y = y3;
SDL_BlitSurface(road3_right, nullptr, screenSurface, &dstrect_road3_right);
}
}
if (x2 == 140)
{
if (a == 1)
{
x2 = 0;
SDL_Rect dstrect_road3_left;
dstrect_road3_left.x = x2;
dstrect_road3_left.y = y3;
SDL_BlitSurface(road3_left, nullptr, screenSurface, &dstrect_road3_left);
}
if (a == 2)
{
x2 = 280;
dstrect_road3.x = x2;
dstrect_road3.y = y3;
SDL_BlitSurface(road3, nullptr, screenSurface, &dstrect_road3);
}
}
if (x2 == 560)
{
if (a == 1)
{
x2 = 420;
SDL_Rect dstrect_road3_right_center;
dstrect_road3_right_center.x = x2;
dstrect_road3_right_center.y = y3;
SDL_BlitSurface(road3_right_center, nullptr, screenSurface, &dstrect_road3_right_center);
}
}
if (x2 == 0)
{
if (a == 2)
{
x2 = 140;
SDL_Rect dstrect_road3_left_center;
dstrect_road3_left_center.x = x2;
dstrect_road3_left_center.y = y3;
SDL_BlitSurface(road3_left_center, nullptr, screenSurface, &dstrect_road3_left_center);
}
}
}
//Apply the image
dstrect_road4.x = x2;
dstrect_road4.y = y4;
if (y4 > y5) {
if (a == 1) {
dstrect_road4.x = dstrect_road4.x + 140;
}
if (a == 2) {
dstrect_road4.x = dstrect_road4.x - 140;
}
if (a == 0) {
if (dstrect_road5.x != x2) {
dstrect_road4.x = dstrect_road5.x;
}
}
}
SDL_BlitSurface(road4, nullptr, screenSurface, &dstrect_road4);
if (y4 == -65) {
a = rand() % 3;
}
if (has_turn == true)
{
if (x2 == 280)
{
if (a == 1)
{
x2 = 140;
SDL_Rect dstrect_road4_left_center;
dstrect_road4_left_center.x = x2;
dstrect_road4_left_center.y = y4;
SDL_BlitSurface(road4_left_center, nullptr, screenSurface, &dstrect_road4_left_center);
}
if (a == 2)
{
x2 = 420;
SDL_Rect dstrect_road4_right_center;
dstrect_road4_right_center.x = x2;
dstrect_road4_right_center.y = y4;
SDL_BlitSurface(road4_right_center, nullptr, screenSurface, &dstrect_road4_right_center);
}
}
if (x2 == 420)
{
if (a == 1)
{
x2 = 280;
dstrect_road4.x = x2;
dstrect_road4.y = y4;
SDL_BlitSurface(road4, nullptr, screenSurface, &dstrect_road4);
}
if (a == 2)
{
x2 = 560;
SDL_Rect dstrect_road4_right;
dstrect_road4_right.x = x2;
dstrect_road4_right.y = y4;
SDL_BlitSurface(road4_right, nullptr, screenSurface, &dstrect_road4_right);
}
}
if (x2 == 140)
{
if (a == 1)
{
x2 = 0;
SDL_Rect dstrect_road4_left;
dstrect_road4_left.x = x2;
dstrect_road4_left.y = y4;
SDL_BlitSurface(road4_left, nullptr, screenSurface, &dstrect_road4_left);
}
if (a == 2)
{
x2 = 280;
dstrect_road4.x = x2;
dstrect_road4.y = y4;
SDL_BlitSurface(road4, nullptr, screenSurface, &dstrect_road4);
}
}
if (x2 == 560)
{
if (a == 1)
{
x2 = 420;
SDL_Rect dstrect_road4_right_center;
dstrect_road4_right_center.x = x2;
dstrect_road4_right_center.y = y4;
SDL_BlitSurface(road4_right_center, nullptr, screenSurface, &dstrect_road4_right_center);
}
}
if (x2 == 0)
{
if (a == 2)
{
x2 = 140;
SDL_Rect dstrect_road4_left_center;
dstrect_road4_left_center.x = x2;
dstrect_road4_left_center.y = y4;
SDL_BlitSurface(road4_left_center, nullptr, screenSurface, &dstrect_road4_left_center);
}
}
}
//Apply the image
dstrect_road5.x = x2;
dstrect_road5.y = y5;
if (y5 > y0) {
if (a == 1) {
dstrect_road5.x = dstrect_road5.x + 140;
}
if (a == 2) {
dstrect_road5.x = dstrect_road5.x - 140;
}
if (a == 0) {
if (dstrect_road0.x != x2) {
dstrect_road5.x = dstrect_road0.x;
}
}
}
SDL_BlitSurface(road5, nullptr, screenSurface, &dstrect_road5);
if (y5 == -65) {
a = rand() % 3;
}
if (has_turn == true)
{
if (x2 == 280)
{
if (a == 1)
{
x2 = 140;
SDL_Rect dstrect_road5_left_center;
dstrect_road5_left_center.x = x2;
dstrect_road5_left_center.y = y5;
SDL_BlitSurface(road5_left_center, nullptr, screenSurface, &dstrect_road5_left_center);
}
if (a == 2)
{
x2 = 420;
SDL_Rect dstrect_road5_right_center;
dstrect_road5_right_center.x = x2;
dstrect_road5_right_center.y = y5;
SDL_BlitSurface(road5_right_center, nullptr, screenSurface, &dstrect_road5_right_center);
}
}
if (x2 == 420)
{
if (a == 1)
{
x2 = 280;
dstrect_road5.x = x2;
dstrect_road5.y = y5;
SDL_BlitSurface(road5, nullptr, screenSurface, &dstrect_road5);
}
if (a == 2)
{
x2 = 560;
SDL_Rect dstrect_road5_right;
dstrect_road5_right.x = x2;
dstrect_road5_right.y = y5;
SDL_BlitSurface(road5_right, nullptr, screenSurface, &dstrect_road5_right);
}
}
if (x2 == 140)
{
if (a == 1)
{
x2 = 0;
SDL_Rect dstrect_road5_left;
dstrect_road5_left.x = x2;
dstrect_road5_left.y = y5;
SDL_BlitSurface(road5_left, nullptr, screenSurface, &dstrect_road5_left);
}
if (a == 2)
{
x2 = 280;
dstrect_road5.x = x2;
dstrect_road5.y = y5;
SDL_BlitSurface(road5, nullptr, screenSurface, &dstrect_road5);
}
}
if (x2 == 560)
{
if (a == 1)
{
x2 = 420;
SDL_Rect dstrect_road5_right_center;
dstrect_road5_right_center.x = x2;
dstrect_road5_right_center.y = y5;
SDL_BlitSurface(road5_right_center, nullptr, screenSurface, &dstrect_road5_right_center);
}
}
if (x2 == 0)
{
if (a == 2)
{
x2 = 140;
SDL_Rect dstrect_road5_left_center;
dstrect_road5_left_center.x = x2;
dstrect_road5_left_center.y = y5;
SDL_BlitSurface(road5_left_center, nullptr, screenSurface, &dstrect_road5_left_center);
}
}
}
//Apply the image
/*SDL_Rect dstrect_snake;
dstrect_snake.x = 285;
dstrect_snake.y = 424;
SDL_BlitSurface(snake, nullptr, screenSurface, &dstrect_snake);*/
//Update the surface
SDL_UpdateWindowSurface(window);
SDL_Delay(20);
//Apply the image
/*SDL_Rect dstrect_bg0;
dstrect_bg0.x = 280;
dstrect_bg0.y = y0;
SDL_BlitSurface(bg0, nullptr, screenSurface, &dstrect_bg0);
//Apply the image
SDL_Rect dstrect_bg1;
dstrect_bg1.x = 280;
dstrect_bg1.y = y1;
SDL_BlitSurface(bg1, nullptr, screenSurface, &dstrect_bg1);
//Apply the image
SDL_Rect dstrect_bg2;
dstrect_bg2.x = 280;
dstrect_bg2.y = y2;
SDL_BlitSurface(bg2, nullptr, screenSurface, &dstrect_bg2);
//Apply the image
SDL_Rect dstrect_bg3;
dstrect_bg3.x = 280;
dstrect_bg3.y = y3;
SDL_BlitSurface(bg3, nullptr, screenSurface, &dstrect_bg3);
//Apply the image
SDL_Rect dstrect_bg4;
dstrect_bg4.x = 280;
dstrect_bg4.y = y4;
SDL_BlitSurface(bg4, nullptr, screenSurface, &dstrect_bg4);*/
//Apply the image
SDL_BlitSurface(gameScreen, nullptr, screenSurface, nullptr);
//Apply the image
SDL_Rect dstrect_snake1;
dstrect_snake1.x = 285;
dstrect_snake1.y = 424;
SDL_BlitSurface(snake, nullptr, screenSurface, &dstrect_snake1);
//Update the surface
SDL_UpdateWindowSurface(window);
y0++;
if (y0 >= 635)
{
y0 = -65;
has_turn = true;
}
y1++;
if (y1 >= 635)
y1 = -65;
y2++;
if (y2 >= 635)
y2 = -65;
y3++;
if (y3 >= 635)
y3 = -65;
y4++;
if (y4 >= 635)
y4 = -65;
y5++;
if (y5 >= 635)
y5 = -65;
}
return 0;
}
int main(int argc, char* argv[])
{
//Start up SDL and create window
if(!init())
{
cout << "Failed to initialize!/n";
}
else
{
if (!loadMedia())
{
cout << "Failed to load media!/n";
}
else
{
//Apply the image
SDL_BlitSurface(mainScreen, nullptr, screenSurface, nullptr);
//Update the surface
SDL_UpdateWindowSurface(window);
//main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
switch (e.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_MOUSEBUTTONDOWN:
if (e.button.button == SDL_BUTTON_LEFT) {
if ((e.button.x > 189 and e.button.x < 520) and (e.button.y > 468 and e.button.y < 620)) {
game();
}
}
break;
default:
break;
}
}
}
//Apply the image
SDL_BlitSurface(mainScreen, nullptr, screenSurface, nullptr);
//Update the surface
SDL_UpdateWindowSurface(window);
//Free resources and close SDL
close();
}
}
return 0;
}
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
cout << "SDL initialization failed. SDL Error: " << SDL_GetError();
success = false;
}
else
{
// Create window
window = SDL_CreateWindow("Snake Runner",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (window == nullptr)
{
cout << "Window could not be created! SDL_Error: %s/n" << SDL_GetError();
success = false;
}
else {
//Get window surface
screenSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
mainScreen = SDL_LoadBMP("MainScreenSnake.bmp");
gameScreen = SDL_LoadBMP("GameScreenSnake.bmp");
snake = SDL_LoadBMP("snake.bmp");
road0 = SDL_LoadBMP("road.bmp");
road1 = SDL_LoadBMP("road.bmp");
road2 = SDL_LoadBMP("road.bmp");
road3 = SDL_LoadBMP("road.bmp");
road4 = SDL_LoadBMP("road.bmp");
road5 = SDL_LoadBMP("road.bmp");
road0_left_center = SDL_LoadBMP("road.bmp");
road1_left_center = SDL_LoadBMP("road.bmp");
road2_left_center = SDL_LoadBMP("road.bmp");
road3_left_center = SDL_LoadBMP("road.bmp");
road4_left_center = SDL_LoadBMP("road.bmp");
road5_left_center = SDL_LoadBMP("road.bmp");
road0_right_center = SDL_LoadBMP("road.bmp");
road1_right_center = SDL_LoadBMP("road.bmp");
road2_right_center = SDL_LoadBMP("road.bmp");
road3_right_center = SDL_LoadBMP("road.bmp");
road4_right_center = SDL_LoadBMP("road.bmp");
road5_right_center = SDL_LoadBMP("road.bmp");
road0_left = SDL_LoadBMP("road.bmp");
road1_left = SDL_LoadBMP("road.bmp");
road2_left = SDL_LoadBMP("road.bmp");
road3_left = SDL_LoadBMP("road.bmp");
road4_left = SDL_LoadBMP("road.bmp");
road5_left = SDL_LoadBMP("road.bmp");
road0_right = SDL_LoadBMP("road.bmp");
road1_right = SDL_LoadBMP("road.bmp");
road2_right = SDL_LoadBMP("road.bmp");
road3_right = SDL_LoadBMP("road.bmp");
road4_right = SDL_LoadBMP("road.bmp");
road5_right = SDL_LoadBMP("road.bmp");
bg0 = SDL_LoadBMP("bg.bmp");
bg1 = SDL_LoadBMP("bg.bmp");
bg2 = SDL_LoadBMP("bg.bmp");
bg3 = SDL_LoadBMP("bg.bmp");
bg4 = SDL_LoadBMP("bg.bmp");
bg5 = SDL_LoadBMP("bg.bmp");
bg0_left_center = SDL_LoadBMP("bg.bmp");
bg1_left_center = SDL_LoadBMP("bg.bmp");
bg2_left_center = SDL_LoadBMP("bg.bmp");
bg3_left_center = SDL_LoadBMP("bg.bmp");
bg4_left_center = SDL_LoadBMP("bg.bmp");
bg5_left_center = SDL_LoadBMP("bg.bmp");
bg0_right_center = SDL_LoadBMP("bg.bmp");
bg1_right_center = SDL_LoadBMP("bg.bmp");
bg2_right_center = SDL_LoadBMP("bg.bmp");
bg3_right_center = SDL_LoadBMP("bg.bmp");
bg4_right_center = SDL_LoadBMP("bg.bmp");
bg5_right_center = SDL_LoadBMP("bg.bmp");
bg0_left = SDL_LoadBMP("bg.bmp");
bg1_left = SDL_LoadBMP("bg.bmp");
bg2_left = SDL_LoadBMP("bg.bmp");
bg3_left = SDL_LoadBMP("bg.bmp");
bg4_left = SDL_LoadBMP("bg.bmp");
bg5_left = SDL_LoadBMP("bg.bmp");
bg0_right = SDL_LoadBMP("bg.bmp");
bg1_right = SDL_LoadBMP("bg.bmp");
bg2_right = SDL_LoadBMP("bg.bmp");
bg3_right = SDL_LoadBMP("bg.bmp");
bg4_right = SDL_LoadBMP("bg.bmp");
bg5_right = SDL_LoadBMP("bg.bmp");
if (mainScreen == nullptr)
{
cout << "Unable to load image %s! SDL Error: %s/n" << "MainScreenSnake.bmp" << SDL_GetError();
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface(mainScreen);
mainScreen = nullptr;
gameScreen = nullptr;
road0 = nullptr;
road1 = nullptr;
road2 = nullptr;
road3 = nullptr;
road4 = nullptr;
road5 = nullptr;
road0_left_center = nullptr;
road1_left_center = nullptr;
road2_left_center = nullptr;
road3_left_center = nullptr;
road4_left_center = nullptr;
road5_left_center = nullptr;
road0_right_center = nullptr;
road1_right_center = nullptr;
road2_right_center = nullptr;
road3_right_center = nullptr;
road4_right_center = nullptr;
road5_right_center = nullptr;
road0_left = nullptr;
road1_left = nullptr;
road2_left = nullptr;
road3_left = nullptr;
road4_left = nullptr;
road5_left = nullptr;
road0_right = nullptr;
road1_right = nullptr;
road2_right = nullptr;
road3_right = nullptr;
road4_right = nullptr;
road5_right = nullptr;
bg0 = nullptr;
bg1 = nullptr;
bg2 = nullptr;
bg3 = nullptr;
bg4 = nullptr;
bg5 = nullptr;
bg0_left_center = nullptr;
bg1_left_center = nullptr;
bg2_left_center = nullptr;
bg3_left_center = nullptr;
bg4_left_center = nullptr;
bg5_left_center = nullptr;
bg0_right_center = nullptr;
bg1_right_center = nullptr;
bg2_right_center = nullptr;
bg3_right_center = nullptr;
bg4_right_center = nullptr;
bg5_right_center = nullptr;
bg0_left = nullptr;
bg1_left = nullptr;
bg2_left = nullptr;
bg3_left = nullptr;
bg4_left = nullptr;
bg5_left = nullptr;
bg0_right = nullptr;
bg1_right = nullptr;
bg2_right = nullptr;
bg3_right = nullptr;
bg4_right = nullptr;
bg5_right = nullptr;
//Destroy window
SDL_DestroyWindow(window);
window = nullptr;
//Quit SDL subsystems
SDL_Quit();
}