乒乓球 sdl 游戏球拍移动
ping-pong sdl game racket moving
我一直在尝试通过 sdl 库制作一个简单的乒乓球游戏,但现在我遇到了一个问题!
游戏有 2 个玩家,一个球拍通过上下键移动,另一个通过 s 和 w 键移动。
问题是例如我用up键,两个球拍都动了,我不知道问题出在哪里is.Considering这个规则是两个球员必须能够同时移动他们的球拍。
我在 Ubuntu 上编译了这段代码。
如果有人帮助我,那将是完美的!
提前致谢!
#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;
while(true)
{
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 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 += -5;
if(event.key.keysym.sym == SDLK_DOWN)
j += 5;
}
}
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 255, 50, 0, 255);
SDL_Flip(screen);
SDL_Delay(20);
boxRGBA(screen, 100, 200+j, 50, 350+j, 0, 0, 0, 255);
SDL_Event event2;
if(!SDL_PollEvent(&event2));
{
if(event2.type == SDL_QUIT)
return 0;
if(event2.type == SDL_KEYDOWN)
{
if(event2.key.keysym.sym == SDLK_w)
j += -5;
if(event2.key.keysym.sym == SDLK_s)
j += 5;
}
}
boxRGBA(screen, 100, 200+j, 50, 350+j, 0,0, 255, 255);
SDL_Flip(screen);
SDL_Delay(20);
}
//////////////////////////////////////////////////////////
SDL_Delay(2000);
return 0;
}
括号的位置让我很讨厌。但是,移植到 SO 时可能已经修改了。
您初始化了整数 i 和 j,但只使用了变量 j。
我一直在尝试通过 sdl 库制作一个简单的乒乓球游戏,但现在我遇到了一个问题! 游戏有 2 个玩家,一个球拍通过上下键移动,另一个通过 s 和 w 键移动。 问题是例如我用up键,两个球拍都动了,我不知道问题出在哪里is.Considering这个规则是两个球员必须能够同时移动他们的球拍。 我在 Ubuntu 上编译了这段代码。 如果有人帮助我,那将是完美的! 提前致谢!
#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;
while(true)
{
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 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 += -5;
if(event.key.keysym.sym == SDLK_DOWN)
j += 5;
}
}
boxRGBA(screen, 1000, 200+j, 1050, 350+j, 255, 50, 0, 255);
SDL_Flip(screen);
SDL_Delay(20);
boxRGBA(screen, 100, 200+j, 50, 350+j, 0, 0, 0, 255);
SDL_Event event2;
if(!SDL_PollEvent(&event2));
{
if(event2.type == SDL_QUIT)
return 0;
if(event2.type == SDL_KEYDOWN)
{
if(event2.key.keysym.sym == SDLK_w)
j += -5;
if(event2.key.keysym.sym == SDLK_s)
j += 5;
}
}
boxRGBA(screen, 100, 200+j, 50, 350+j, 0,0, 255, 255);
SDL_Flip(screen);
SDL_Delay(20);
}
//////////////////////////////////////////////////////////
SDL_Delay(2000);
return 0;
}
括号的位置让我很讨厌。但是,移植到 SO 时可能已经修改了。
您初始化了整数 i 和 j,但只使用了变量 j。