Renew Projects
This commit is contained in:
48
Huajisheblockade/Apple.cpp
Normal file
48
Huajisheblockade/Apple.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "Apple.h"
|
||||
|
||||
// Use a static generator to avoid seeding every time Spawn() is called
|
||||
static std::mt19937 apple_gen(static_cast<unsigned int>(std::time(nullptr)));
|
||||
|
||||
Apple::Apple(int screenWidth, int screenHeight, int segSize = 25)
|
||||
: screenW(screenWidth), screenH(screenHeight), segmentSize(segSize) {
|
||||
Spawn(); // Initialize with a random position
|
||||
}
|
||||
|
||||
void Apple::Spawn() {
|
||||
int maxGridX = (screenW / segmentSize) - 1;
|
||||
int maxGridY = (screenH / segmentSize) - 1;
|
||||
|
||||
std::uniform_int_distribution<int> distX(2, maxGridX - 2);
|
||||
std::uniform_int_distribution<int> distY(2, maxGridY - 2);
|
||||
|
||||
pos.x = distX(apple_gen);
|
||||
pos.y = distY(apple_gen);
|
||||
}
|
||||
|
||||
|
||||
void Apple::Render(SDL_Renderer* renderer) {
|
||||
SDL_SetRenderDrawColor(renderer, 255, 50, 50, 255);
|
||||
SDL_FRect bodyRect = {
|
||||
(float)pos.x * segmentSize + 2,
|
||||
(float)pos.y * segmentSize + 4,
|
||||
(float)segmentSize - 4,
|
||||
(float)segmentSize - 6
|
||||
};
|
||||
SDL_RenderFillRect(renderer, &bodyRect);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 139, 69, 19, 255);
|
||||
SDL_FRect stemRect = {
|
||||
(float)pos.x * segmentSize + (segmentSize / 2) - 1,
|
||||
(float)pos.y * segmentSize + 1,
|
||||
2, 4
|
||||
};
|
||||
SDL_RenderFillRect(renderer, &stemRect);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
|
||||
SDL_FRect leafRect = {
|
||||
(float)pos.x * segmentSize + (segmentSize / 2) + 1,
|
||||
(float)pos.y * segmentSize + 1,
|
||||
4, 2
|
||||
};
|
||||
SDL_RenderFillRect(renderer, &leafRect);
|
||||
}
|
||||
Reference in New Issue
Block a user