SDL Pong运动Bug

时间:2018-01-13 11:55:19

标签: c++ c++14 sdl-2

我的代码存在的问题是我正在使用c ++在SDL 2.0中制作乒乓球游戏。我做了一切,直到创造运动。当玩家桨移动时,它留下与桨相同颜色的轨迹。我在YouTube上观看了一些视频,但当他们进行动作时,它很好而且清晰,而且我可以解决这个问题,但是每次玩家移动时我都需要重新着色背景,这使得它变得华而不实,如果我按下按钮我就不会根本看不到桨。

 #include<iostream>
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
#include<windows.h>

#define width 800
#define height 600

using namespace std;


bool run = true;



class Player{
private:
SDL_Window* window = SDL_CreateWindow("Pong!", SDL_WINDOWPOS_CENTERED, 
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);
SDL_Surface* Screen = SDL_GetWindowSurface(window);
Uint32 screen_color = SDL_MapRGB(Screen->format, 0, 0, 0);
Uint32 In_game_RGB = SDL_MapRGB(Screen->format, 255, 255, 255);

SDL_Rect Pl;
SDL_Rect AI;
SDL_Rect Ball;
SDL_Rect ClearP;
SDL_Rect ClearAI;

public:
Player(){
//Player parameters
Pl.x = 60;Pl.y = 225;Pl.w = 25;Pl.h = 200;
//AI parameters
AI.x = 720;AI.y = 225;AI.w = 25;AI.h = 200;
//Ball parameters
Ball.x = width/2;Ball.y = height/2+10;Ball.w = 25;Ball.h = 25;
//Recoloring parameters
ClearP.x = 0;ClearP.y = 0; ClearP.w = 375;ClearP.h = height;
ClearAI.x = 425;ClearAI.y = 0;ClearAI.w = 375;ClearAI.h = height;
//Make the screen color black
SDL_FillRect(Screen, NULL, screen_color);
}

void scrUpdate(){
 SDL_UpdateWindowSurface(window);

}
void drawPlayer(){
SDL_FillRect(Screen, &Pl, In_game_RGB);
}
void drawComputer(){
SDL_FillRect(Screen, &AI, In_game_RGB);
}
void ball(){
SDL_FillRect(Screen, &Ball, In_game_RGB);
}

void Movement(){

if(GetAsyncKeyState(VK_DOWN)){
Pl.y += 2;
SDL_FillRect(Screen,&ClearP,screen_color);
}
if(GetAsyncKeyState(VK_UP)){
SDL_FillRect(Screen,&ClearP,screen_color);
Pl.y -= 2;
}
}
};

void EventCheck(){
SDL_Event event;
if(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
run = false;
}
}
}



int main( int argc, char *argv[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
Player Play;
//Player Computer();

while(run){
Play.scrUpdate();
Play.drawPlayer();
Play.drawComputer();
Play.ball();
Play.Movement();
EventCheck();

}

SDL_Quit();
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:-1)

显示一些代码或您正在做的事情的示例,或者您正在观看的视频的链接将有助于: Youtube tutorial

但我建议看看: screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);SDL_Flip(screen)与屏幕缓冲和绘图有关。

另一种可能性是您运行的是过时的SDL版本,或者与当前系统不兼容的版本。

为了能够提供更完整和正确的答案,我强烈建议您添加有关代码,结果屏幕截图以及SDL和操作系统版本的更多信息。

另外,你说握住桨时它很华丽。我认为必须是你正在执行你的逻辑移动桨,并且一旦它仍然是你重绘桨。如果您不断重绘整个屏幕,请考虑双缓冲。