1#ifndef SNAKE_GAME_LAYER_H
2#define SNAKE_GAME_LAYER_H
50 void Reset()
override;
68 bool eventFilter(QObject *watched, QEvent *event)
override;
SecurityLayer(QWidget *parent=nullptr)
Constructs a SecurityLayer.
Definition security_layer.h:20
void Initialize() override
Initializes the layer for display. Resets the game state, initializes the game logic (snake position,...
Definition snake_game_layer.cpp:89
bool IsCollision(int x, int y) const
Checks if the given coordinates (x, y) represent a collision. Considers collisions with walls (respec...
Definition snake_game_layer.cpp:424
Direction direction_
Current direction the snake is moving.
Definition snake_game_layer.h:213
void HandleInput(int key)
Processes user input (arrow keys) to change the snake's direction. Prevents the snake from reversing ...
Definition snake_game_layer.cpp:438
BorderSide exit_side_
Which side (Left or Right) the exit is on.
Definition snake_game_layer.h:224
void MoveSnake()
Moves the snake one step in the current direction_. Calculates the new head position,...
Definition snake_game_layer.cpp:290
void keyPressEvent(QKeyEvent *event) override
Handles key press events for controlling the snake. Passes the key code to HandleInput() and accepts ...
Definition snake_game_layer.cpp:469
QVector< QPair< int, int > > snake_
Vector storing the coordinates of each segment of the snake (head is first).
Definition snake_game_layer.h:209
Direction
Enum representing the possible movement directions of the snake.
Definition snake_game_layer.h:102
@ Down
Definition snake_game_layer.h:105
@ Up
Definition snake_game_layer.h:103
@ Right
Definition snake_game_layer.h:104
@ Left
Definition snake_game_layer.h:106
bool eventFilter(QObject *watched, QEvent *event) override
Filters key press events specifically for the game_board_ QLabel. Ensures that arrow key presses are ...
Definition snake_game_layer.cpp:474
Direction last_processed_direction_
Direction the snake was moving in the last processed game tick (used to prevent 180 turns).
Definition snake_game_layer.h:215
QPair< int, int > apple_
Coordinates of the current apple.
Definition snake_game_layer.h:211
bool IsExitPoint(int x, int y) const
Checks if the given coordinates (x, y) correspond to the open exit location. Used only when game_stat...
Definition snake_game_layer.cpp:560
SnakeGameLayer(QWidget *parent=nullptr)
Constructs a SnakeGameLayer. Initializes the UI elements (title, instructions, game board,...
Definition snake_game_layer.cpp:13
void UpdateGame()
Slot called periodically by game_timer_ to update the game state. Calls MoveSnake() if the game is no...
Definition snake_game_layer.cpp:483
QLabel * score_label_
QLabel displaying the current score (apples collected).
Definition snake_game_layer.h:195
bool game_over_
Flag indicating if the game is over (due to collision or successful exit).
Definition snake_game_layer.h:219
void OpenBorder()
Initiates the "exit open" state after collecting 4 apples. Changes the game state,...
Definition snake_game_layer.cpp:531
int animation_progress_
Progress counter for the border opening animation.
Definition snake_game_layer.h:228
int parts_exited_
Counter for how many snake segments have successfully exited the board.
Definition snake_game_layer.h:230
QTimer * border_animation_timer_
Timer controlling the animation of the border opening.
Definition snake_game_layer.h:199
TranslationManager * translator_
Pointer to the translation manager for handling UI translations.
Definition snake_game_layer.h:234
GameState
Enum representing the current state of the game.
Definition snake_game_layer.h:123
@ Completed
Snake successfully exited the board.
Definition snake_game_layer.h:126
@ ExitOpen
Apples collected, exit is open, snake needs to leave.
Definition snake_game_layer.h:125
@ Playing
Normal gameplay, collecting apples.
Definition snake_game_layer.h:124
@ Failed
Snake collided, game over.
Definition snake_game_layer.h:127
CellType grid_[kGridSize][kGridSize]
2D array representing the game board state.
Definition snake_game_layer.h:207
CellType
Enum representing the possible types of content within a grid cell.
Definition snake_game_layer.h:112
@ Apple
An apple to be collected.
Definition snake_game_layer.h:116
@ Empty
Empty cell.
Definition snake_game_layer.h:113
@ SnakeHead
The head of the snake.
Definition snake_game_layer.h:115
@ Snake
Part of the snake's body.
Definition snake_game_layer.h:114
static constexpr int kCellSize
Size of each cell in pixels.
Definition snake_game_layer.h:204
BorderSide
Enum representing which border (left or right) the exit will appear on.
Definition snake_game_layer.h:133
void RenderGame() const
Renders the current state of the game onto the game_board_ QLabel. Draws the grid,...
Definition snake_game_layer.cpp:166
QTimer * game_timer_
Timer controlling the main game loop (snake movement).
Definition snake_game_layer.h:197
static constexpr int kGridSize
Size of the game grid (width and height in cells).
Definition snake_game_layer.h:202
GameState game_state_
Current state of the game (Playing, ExitOpen, etc.).
Definition snake_game_layer.h:222
void InitializeGame()
Sets up the initial state of the game board and snake. Clears the grid, places the snake in the cente...
Definition snake_game_layer.cpp:140
void UpdateBorderAnimation()
Slot called periodically by border_animation_timer_ to animate the border opening....
Definition snake_game_layer.cpp:553
void Reset() override
Resets the layer to its initial state. Stops timers, resets game variables (score,...
Definition snake_game_layer.cpp:106
int apples_collected_
The number of apples collected so far.
Definition snake_game_layer.h:217
int exit_position_
The Y-coordinate (row index) of the exit cell.
Definition snake_game_layer.h:226
~SnakeGameLayer() override
Destructor. Stops and deletes the game and border animation timers.
Definition snake_game_layer.cpp:75
QVector< QPair< int, int > > exit_points_
Store the coordinates considered as valid exit points (just outside the border).
Definition snake_game_layer.h:232
QLabel * game_board_
QLabel used as the canvas for rendering the game board.
Definition snake_game_layer.h:193
void FinishGame(bool success)
Handles the end of the game (success or failure). Stops timers. On success, updates UI to green,...
Definition snake_game_layer.cpp:489
void GenerateApple()
Generates a new apple at a random empty cell on the grid.
Definition snake_game_layer.cpp:403
Manages the loading and delivery of translations for applications.
Definition translation_manager.h:15