C和SDL如何在屏幕上移动一个点

C and SDL how to move a point in the screen

我正在使用 SDL 和 c 我想构建一个简单的游戏,让小点在屏幕上移动屏幕上是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <malloc.h>
#include <math.h>
#include "SDL/include/SDL.h"
#include "SDL/include/SDL_render.h"

#define WIDTH 800.0f
#define HEIGHT 600.0f

int main()
{
  bool quit = false;

  SDL_Event event;

  // init SDL
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window * window = SDL_CreateWindow("SDL2 line drawing",
      SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, 0);

  SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);

  // handle events
  while (!quit)
  {
      SDL_Delay(10);
      SDL_PollEvent(&event);

      switch (event.type)
      {
          case SDL_QUIT:
              quit = true;
              break;
      }

      SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
      SDL_RenderClear(renderer);

      SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

      SDL_RenderDrawLine(renderer, 100, 100, 500, 500);

      /*for (int i = 0; i < 100; i++) {*/
        /*SDL_RenderDrawPoint(renderer, 150 + i, 150);*/
        /*sleep(1);*/
        /*SDL_RenderPresent(renderer);*/
      /*}*/

      // render window
      SDL_RenderPresent(renderer);
  }

  // cleanup 
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}


我想要发生的是一个改变其在屏幕上位置的点如何删除该点并在屏幕上的其他地方重新绘制它?

为每个 while (!quit) 渲染你的点一次,而不是为你想要动画的整个运动渲染所有 100 次。您注释代码的方式是在每个循环中获得所有 100 个位置,即每个 SDL_RenderClear(renderer);.