我正在使用SDL库来创建乒乓球游戏。我正要在键盘上按字母w时桨向上和向下移动的位置。我试图在按下w时移动矩形。但它行不通。以前可以使用,但是它只是在渲染器上进行绘画,而不是移动整个矩形。
#include <iostream>
#include<SDL.h>
#include<fstream>
using namespace std;
SDL_Window *window = NULL;
SDL_Surface *screen = NULL;
Uint32 white;
Uint32 black;
SDL_Renderer *renderer;
const int HEIGHT = 700;
const int WIDTH = 800;
const int padH = 95;
const int padW = 25;
SDL_Event e;
SDL_Rect player;
SDL_Rect player2;
SDL_Rect Ball;
void init()
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Pong",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
screen = SDL_GetWindowSurface(window);
white = SDL_MapRGB(screen->format, 255, 255, 255);
black= SDL_MapRGB(screen->format, 0,0,0);
player.h = padH;
player.w = padW;
player2.h = padH;
player2.w = padW;
player.x = 20;
player.y = HEIGHT / 2;
player2.x = WIDTH-40;
player2.y = HEIGHT / 2;
Ball.h = 20;
Ball.w = 20;
Ball.x = WIDTH / 2;
Ball.y = HEIGHT / 2;
}
void draw()
{
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderFillRect(renderer, &Ball);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &player);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &player2);
SDL_RenderPresent(renderer);
}
void gamelogic()
{
while (SDL_PollEvent(&e) !=0)
{
if (e.type = SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_w:
SDL_RenderFillRect(renderer, &player);
player.y = player.y + 1;
}
}
}
}
int main(int arg, char*argv[]) {
bool done = true;
init();
draw();
gamelogic();
while (done)
{
SDL_PollEvent(&e);
if (e.type == SDL_QUIT)
{
done = false;
}
}
return 0;
}
当我在while循环中按下按键时,它只是保持绘画而不是移动矩形并将其刷过
答案 0 :(得分:0)
您的主循环基本上是不存在的-它只会泵出事件队列并在最后一个窗口关闭时退出(或在此之前的任何时间,因为您从不检查是否有事件但仍会对其进行反应-可能是任何事情) 。主循环应在每次迭代中检查事件,对事件做出反应并重绘。您不需要在事件处理代码中绘制任何内容,并且if (e.type = SDL_KEYDOWN)
是分配的,而不是比较的-我建议启用编译器警告。
总结起来,完整的例子(还有很大的改进余地):
#include <iostream>
#include <SDL.h>
#include <fstream>
using namespace std;
SDL_Window *window = NULL;
SDL_Surface *screen = NULL;
Uint32 white;
Uint32 black;
SDL_Renderer *renderer;
const int HEIGHT = 700;
const int WIDTH = 800;
const int padH = 95;
const int padW = 25;
SDL_Event e;
SDL_Rect player;
SDL_Rect player2;
SDL_Rect Ball;
bool done = false;
void init()
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Pong",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
screen = SDL_GetWindowSurface(window);
white = SDL_MapRGB(screen->format, 255, 255, 255);
black= SDL_MapRGB(screen->format, 0,0,0);
player.h = padH;
player.w = padW;
player2.h = padH;
player2.w = padW;
player.x = 20;
player.y = HEIGHT / 2;
player2.x = WIDTH-40;
player2.y = HEIGHT / 2;
Ball.h = 20;
Ball.w = 20;
Ball.x = WIDTH / 2;
Ball.y = HEIGHT / 2;
}
void draw()
{
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderFillRect(renderer, &Ball);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &player);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &player2);
SDL_RenderPresent(renderer);
}
void gamelogic()
{
while (SDL_PollEvent(&e) !=0)
{
if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_w:
player.y = player.y + 1;
}
}
else if (e.type == SDL_QUIT)
{
done = true;
}
}
}
int main(int arg, char*argv[]) {
init();
while (!done)
{
gamelogic();
draw();
}
return 0;
}