同时移动乒乓球拍

时间:2015-11-30 18:18:44

标签: c++ sdl

好吧,我已经开始了一个关于c ++的图形项目,现在我需要一些帮助。 这是一款乒乓球比赛,有2个球拍;一个通过UP和DOWN键上下移动,另一个通过S和W键移动。

我需要做的事情是两位球员都可以同时移动他们的球拍。

抱歉我的英语不好!

#include <iostream>
#include "SDL/SDL.h"
#include <SDL/SDL_gfxPrimitives.h>

using namespace std;

int main()
{
    SDL_Surface* screen = SDL_SetVideoMode(1200, 800 ,32, 0);

        int i=0,j = 0;
        bool a = 0 ;
         while(true)
        {
        boxRGBA(screen, 1000, 200+j, 1050, 350+j, 0, 0, 0, 255);
        boxRGBA(screen, 100, 200+i,150,350+i,0,0,0,255);
        SDL_Event event;
                if(!SDL_PollEvent(&event));
                {
                        if(event.type == SDL_QUIT)
                                return 0;
                        if(event.type == SDL_KEYDOWN)

                       {

                                if(event.key.keysym.sym == SDLK_UP)
                                    j -=10;
                                if(event.key.keysym.sym == SDLK_DOWN)
                                    j +=10;
                                if(event.key.keysym.sym == SDLK_w)
                                    i-=10;
                                if(event.key.keysym.sym == SDLK_s)
                                    i+=10;
                        }
                        else if (event.type == SDL_KEYUP)
                        a=1;
                }

            //if(a)
                //j += b;

 boxRGBA(screen, 1000, 200+j, 1050, 350+j, 255, 50, 0, 255);
 boxRGBA(screen, 100, 200+i,150,350+i,50,0,255,255);
        SDL_Flip(screen);
        SDL_Delay(2);
    }

   SDL_Delay(5000);
    return 0;
}

2 个答案:

答案 0 :(得分:2)

if(!SDL_PollEvent(&event)); // what's ; doing here? Why are you negating the return value?

尝试将其更改为while

while(SDL_PollEvent(&event)) { ... }

这样它将轮询队列中的所有可用事件。

答案 1 :(得分:1)

SDL_GetKeyboardState为您提供键盘当前状态的快照。

const Uint8 *state = SDL_GetKeyboardState(NULL);

if (state[SDL_SCANCODE_UP])
    j -=10;
if (state[SDL_SCANCODE_DOWN])
    j +=10;
if (state[SDL_SCANCODE_W])
    i -=10;
if (state[SDL_SCANCODE_S])
    i +=10;