SnakeGame

This commit is contained in:
e2hang
2025-12-30 19:59:45 +08:00
parent fd77d3aca9
commit 3734ffcc35
1319 changed files with 888365 additions and 0 deletions

31
blockade/Snake.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef SNAKE_H_
#define SNAKE_H_
#include "SDL3/SDL_rect.h"
#include <deque>
#include <SDL3/SDL.h>
#include <random>
#include <ctime>
class Snake {
public:
Snake(int screenWidth, int screenHeight, int segmentSize);
void HandleInput(SDL_Keycode key);
void Update();
void Render(SDL_Renderer* renderer);
void Grow();
void Reset(int screenWidth, int screenHeight);
void RandomizeDirection();
bool CheckCollision(int screenWidth, int screenHeight);
SDL_Point GetHeadPos() const;
std::deque<SDL_Point> GetBody() const;
private:
// Using SDL_Point to store {x, y} coordinates
std::deque<SDL_Point> body;
SDL_Point direction;
int segmentSize; // Size of each block in pixels
bool growing = false; // Flag to skip pop_back when eating food
};
#endif