Prova 3 ic
Prova 3 ic
h>
#include <SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <SDL_ttf.h>
int currentLane = 8;
int targetLane = 0;
struct Character {
int x, y;
int speed;
};
struct Lane {
int x1, y1, x2, y2;
Uint8 colorR, colorG, colorB, colorA;
int numEnemies;
};
struct Enemy {
int x, y;
int speed;
Uint8 colorR, colorG, colorB, colorA;
int direction;
};
int initialize() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) { return 0; }
return 1;
}
float calculateResult(int n) {
float function = 1000.0f * (5.0f / n);
return function;
}
int loadMedia() {
SDL_Surface* surface = IMG_Load("cuelho.png");
if (surface == NULL) { return 0;}
return 1;
}
void initializeLanes() {
int laneHeight = SCREEN_HEIGHT / NUM_LANES;
lanes[i].colorR = 255;
lanes[i].colorG = 0;
lanes[i].colorB = 0;
lanes[i].colorA = 255;
lanes[i].numEnemies = 0;
}
}
if (rand() % 2 == 0) {
enemies[laneIndex][enemyIndex].x = SCREEN_WIDTH;
}
else {
enemies[laneIndex][enemyIndex].x = -ENEMY_WIDTH;
}
enemies[laneIndex][enemyIndex].speed = rand() % 10 * 1;
if (enemies[laneIndex][enemyIndex].x < 0) {
enemies[laneIndex][enemyIndex].direction = 1;
}
else {
enemies[laneIndex][enemyIndex].direction = -1;
}
enemies[laneIndex][lanes[laneIndex].numEnemies].colorR = 255;
enemies[laneIndex][lanes[laneIndex].numEnemies].colorG = 0;
enemies[laneIndex][lanes[laneIndex].numEnemies].colorB = 0;
enemies[laneIndex][lanes[laneIndex].numEnemies].colorA = 255;
if (enemies[laneIndex][lanes[laneIndex].numEnemies].direction == 1) {
enemies[laneIndex][lanes[laneIndex].numEnemies].x = -ENEMY_WIDTH;
}
else {
enemies[laneIndex][lanes[laneIndex].numEnemies].x = SCREEN_WIDTH;
}
enemies[laneIndex][lanes[laneIndex].numEnemies].y = lanes[laneIndex].y1;
lanes[laneIndex].numEnemies++;
}
}
void initializeEnemies() {
srand((unsigned int)time(NULL));
int checkCollision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
return (x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2);
}
void handleEvents(SDL_Event* e) {
if (e->type == SDL_QUIT) {
SDL_DestroyTexture(gCharacterTexture);
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
IMG_Quit();
SDL_Quit();
exit(0);
}
else if (e->type == SDL_KEYDOWN) {
if (e->key.keysym.sym == SDLK_LEFT || e->key.keysym.sym == SDLK_a) {
character.x -= 10;
clickCounter++;
}
else if (e->key.keysym.sym == SDLK_RIGHT || e->key.keysym.sym == SDLK_d) {
character.x += 10;
clickCounter++;
}
else if (e->key.keysym.sym == SDLK_DOWN || e->key.keysym.sym == SDLK_s) {
if (currentLane < NUM_LANES - 1) {
currentLane++;
character.y = lanes[currentLane].y1;
clickCounter++;
}
}
else if (e->key.keysym.sym == SDLK_UP || e->key.keysym.sym == SDLK_w) {
if (currentLane > 0) {
currentLane--;
character.y = lanes[currentLane].y1;
clickCounter++;
}
}
}
if (character.x < 0) {
character.x = 0;
}
else if (character.x > SCREEN_WIDTH - SCREEN_WIDTH / 10) {
character.x = SCREEN_WIDTH - SCREEN_WIDTH / 10;
}
}
void moveEnemies() {
for (int i = 0; i < NUM_LANES; ++i) {
int removeIndices[MAX_ENEMIES_PER_LANE];
int numToRemove = 0;
lanes[i].numEnemies -= numToRemove;
}
}
void update() {
moveEnemies();
if (currentLane == targetLane) {
clickCounter += clickEvents;
result += calculateResult(clickCounter);
clickEvents = 0;
clickCounter = 0;
currentLane = 8;
character.y = lanes[currentLane].y1;
}
}
void render() {
SDL_SetRenderDrawColor(gRenderer, 0, 128, 0, 255);
SDL_RenderClear(gRenderer);
char resultText[50];
snprintf(resultText, sizeof(resultText), "Score: %.2f", result);
SDL_Color textColor = { 255, 255, 255 };
renderText(resultText, 10, 10, 28, textColor);
SDL_RenderPresent(gRenderer);
}
initializeLanes();
initializeEnemies();
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
handleEvents(&e);
if (e.type == SDL_MOUSEBUTTONDOWN) {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
update();
render();
SDL_DestroyTexture(gCharacterTexture);
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
IMG_Quit();
SDL_Quit();
return 0;
}