[Plain Text] J →→→→→进入此内容的聊天室

来自 J, 2024-10-25, 写在 Plain Text, 查看 53 次. [paste_expire] 1 秒.
URL http://www.code666.cn/view/9493705a
  1. #include <SDL2/SDL.h>
  2. #include <stdbool.h>
  3.  
  4. #define SCREEN_WIDTH 800
  5. #define SCREEN_HEIGHT 600
  6.  
  7. typedef struct {
  8.     int x, y;
  9.     int width, height;
  10.     int speed;
  11. } Ship;
  12.  
  13. typedef struct {
  14.     int x, y;
  15.     int speed;
  16.     bool active;
  17. } Bullet;
  18.  
  19. void initShip(Ship* ship) {
  20.     ship->x = SCREEN_WIDTH / 2;
  21.     ship->y = SCREEN_HEIGHT - 50;
  22.     ship->width = 40;
  23.     ship->height = 40;
  24.     ship->speed = 5;
  25. }
  26.  
  27. void initBullet(Bullet* bullet) {
  28.     bullet->x = 0;
  29.     bullet->y = 0;
  30.     bullet->speed = 10;
  31.     bullet->active = false;
  32. }
  33.  
  34. void moveShip(Ship* ship, const Uint8* keystates) {
  35.     if (keystates[SDL_SCANCODE_LEFT] && ship->x > 0) {
  36.         ship->x -= ship->speed;
  37.     }
  38.     if (keystates[SDL_SCANCODE_RIGHT] && ship->x < SCREEN_WIDTH - ship->width) {
  39.         ship->x += ship->speed;
  40.     }
  41.     if (keystates[SDL_SCANCODE_UP] && ship->y > 0) {
  42.         ship->y -= ship->speed;
  43.     }
  44.     if (keystates[SDL_SCANCODE_DOWN] && ship->y < SCREEN_HEIGHT - ship->height) {
  45.         ship->y += ship->speed;
  46.     }
  47. }
  48.  
  49. void shootBullet(Bullet* bullet, Ship* ship) {
  50.     if (!bullet->active) {
  51.         bullet->x = ship->x + ship->width / 2;
  52.         bullet->y = ship->y;
  53.         bullet->active = true;
  54.     }
  55. }
  56.  
  57. void updateBullet(Bullet* bullet) {
  58.     if (bullet->active) {
  59.         bullet->y -= bullet->speed;
  60.         if (bullet->y < 0) {
  61.             bullet->active = false;
  62.         }
  63.     }
  64. }
  65.  
  66. void renderShip(SDL_Renderer* renderer, Ship* ship) {
  67.     SDL_Rect rect = { ship->x, ship->y, ship->width, ship->height };
  68.     SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  69.     SDL_RenderFillRect(renderer, &rect);
  70. }
  71.  
  72. void renderBullet(SDL_Renderer* renderer, Bullet* bullet) {
  73.     if (bullet->active) {
  74.         SDL_Rect rect = { bullet->x, bullet->y, 5, 10 };
  75.         SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
  76.         SDL_RenderFillRect(renderer, &rect);
  77.     }
  78. }
  79.  
  80. int main(int argc, char* args[]) {
  81.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  82.         printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  83.         return -1;
  84.     }
  85.  
  86.     SDL_Window* window = SDL_CreateWindow("Thunder Fighter", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  87.     if (window == NULL) {
  88.         printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
  89.         return -1;
  90.     }
  91.  
  92.     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  93.  
  94.     Ship ship;
  95.     Bullet bullet;
  96.     initShip(&ship);
  97.     initBullet(&bullet);
  98.  
  99.     bool quit = false;
  100.     SDL_Event e;
  101.  
  102.     while (!quit) {
  103.         const Uint8* keystates = SDL_GetKeyboardState(NULL);
  104.         while (SDL_PollEvent(&e) != 0) {
  105.             if (e.type == SDL_QUIT) {
  106.                 quit = true;
  107.             }
  108.             if (e.type == SDL_KEYDOWN) {
  109.                 if (e.key.keysym.sym == SDLK_SPACE) {
  110.                     shootBullet(&bullet, &ship);
  111.                 }
  112.             }
  113.         }
  114.  
  115.         moveShip(&ship, keystates);
  116.         updateBullet(&bullet);
  117.  
  118.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  119.         SDL_RenderClear(renderer);
  120.  
  121.         renderShip(renderer, &ship);
  122.         renderBullet(renderer, &bullet);
  123.  
  124.         SDL_RenderPresent(renderer);
  125.  
  126.         SDL_Delay(16); // 60 frames per second
  127.     }
  128.  
  129.     SDL_DestroyRenderer(renderer);
  130.     SDL_DestroyWindow(window);
  131.     SDL_Quit();
  132.  
  133.     return 0;
  134. }
  135.  

回复 "J"

这儿你可以回复上面这条便签

captcha