- #include <SDL2/SDL.h>
- #include <stdbool.h>
- #define SCREEN_WIDTH 800
- #define SCREEN_HEIGHT 600
- typedef struct {
- int x, y;
- int width, height;
- int speed;
- } Ship;
- typedef struct {
- int x, y;
- int speed;
- bool active;
- } Bullet;
- void initShip(Ship* ship) {
- ship->x = SCREEN_WIDTH / 2;
- ship->y = SCREEN_HEIGHT - 50;
- ship->width = 40;
- ship->height = 40;
- ship->speed = 5;
- }
- void initBullet(Bullet* bullet) {
- bullet->x = 0;
- bullet->y = 0;
- bullet->speed = 10;
- bullet->active = false;
- }
- void moveShip(Ship* ship, const Uint8* keystates) {
- if (keystates[SDL_SCANCODE_LEFT] && ship->x > 0) {
- ship->x -= ship->speed;
- }
- if (keystates[SDL_SCANCODE_RIGHT] && ship->x < SCREEN_WIDTH - ship->width) {
- ship->x += ship->speed;
- }
- if (keystates[SDL_SCANCODE_UP] && ship->y > 0) {
- ship->y -= ship->speed;
- }
- if (keystates[SDL_SCANCODE_DOWN] && ship->y < SCREEN_HEIGHT - ship->height) {
- ship->y += ship->speed;
- }
- }
- void shootBullet(Bullet* bullet, Ship* ship) {
- if (!bullet->active) {
- bullet->x = ship->x + ship->width / 2;
- bullet->y = ship->y;
- bullet->active = true;
- }
- }
- void updateBullet(Bullet* bullet) {
- if (bullet->active) {
- bullet->y -= bullet->speed;
- if (bullet->y < 0) {
- bullet->active = false;
- }
- }
- }
- void renderShip(SDL_Renderer* renderer, Ship* ship) {
- SDL_Rect rect = { ship->x, ship->y, ship->width, ship->height };
- SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
- SDL_RenderFillRect(renderer, &rect);
- }
- void renderBullet(SDL_Renderer* renderer, Bullet* bullet) {
- if (bullet->active) {
- SDL_Rect rect = { bullet->x, bullet->y, 5, 10 };
- SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
- SDL_RenderFillRect(renderer, &rect);
- }
- }
- int main(int argc, char* args[]) {
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
- return -1;
- }
- SDL_Window* window = SDL_CreateWindow("Thunder Fighter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (window == NULL) {
- printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
- return -1;
- }
- SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- Ship ship;
- Bullet bullet;
- initShip(&ship);
- initBullet(&bullet);
- bool quit = false;
- SDL_Event e;
- while (!quit) {
- const Uint8* keystates = SDL_GetKeyboardState(NULL);
- while (SDL_PollEvent(&e) != 0) {
- if (e.type == SDL_QUIT) {
- quit = true;
- }
- if (e.type == SDL_KEYDOWN) {
- if (e.key.keysym.sym == SDLK_SPACE) {
- shootBullet(&bullet, &ship);
- }
- }
- }
- moveShip(&ship, keystates);
- updateBullet(&bullet);
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
- renderShip(renderer, &ship);
- renderBullet(renderer, &bullet);
- SDL_RenderPresent(renderer);
- SDL_Delay(16); // 60 frames per second
- }
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- return 0;
- }