你好我尝试在我的程序中有一个真正的重力,所以当我不按一个键时,较小的矩形应该减慢。这是我试过的代码,但没有发生任何事情(在这方面):
#include <stdio.h>
#include <SDL2/SDL.h>
int main()
{
//Request successful execute
if(SDL_Init(SDL_INIT_EVERYTHING)!=0)
{
printf("fehler");
return 1;
}
//create window 800x600
SDL_Window *win;
win = SDL_CreateWindow("test",100,100,800,600,SDL_WINDOW_SHOWN);
//create renderwindow with vysnc enabled
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(ren==NULL)
{
printf("Fehler");
SDL_Quit();
return 1;
}
//boolean data type not supported
int gameRunning = 1;
SDL_Event event;
const Uint8 *keystate=SDL_GetKeyboardState(NULL);
//create rectangle which is the whole window
SDL_Rect rect={
0,0,800,600
};
//smaller rectangle
SDL_Rect rect2={
10,10,50,50
};
float v_x = 0;
float v_y = 0;
float speedup = 1.f;
//while loop to hold the window opened
while (gameRunning)
{
if (SDL_PollEvent(&event))//close the window
{
if (event.type == SDL_QUIT)
{
gameRunning = 0;
}
}
//color of the first rectangle(red)
SDL_SetRenderDrawColor(ren, 255,0,0,255);
SDL_RenderClear(ren);
SDL_RenderFillRect( ren, &rect );
//second rectangle (blue)
SDL_SetRenderDrawColor(ren, 0,0,255,255);
SDL_RenderFillRect( ren, &rect2 );
//change/update the render
SDL_RenderPresent(ren);
if(keystate[SDL_SCANCODE_LSHIFT])
{
if (keystate[SDL_SCANCODE_A] )
{
v_x -= speedup;
}
if (keystate[SDL_SCANCODE_D] )
{
v_x += speedup;
}
if (keystate[SDL_SCANCODE_S] )
{
v_y += speedup;
}
if (keystate[SDL_SCANCODE_W] )
{
v_y -= speedup;
}
}
else if(SDL_KEYDOWN)
{
if (keystate[SDL_SCANCODE_A] )
{
v_x -= 0.1f;
}
if (keystate[SDL_SCANCODE_D] )
{
v_x += 0.1f;
}
if (keystate[SDL_SCANCODE_S] )
{
v_y += 0.1f;
}
if (keystate[SDL_SCANCODE_W] )
{
v_y -= 0.1f;
}
}
else
{ //make slower(doesnt work)
v_x= -v_x * 1.f;
v_y= -v_y * 1.f;
if(v_x<0.005f || v_y<0.005f)
{
v_x=0;
v_y=0;
}
}
rect2.x += v_x;
rect2.y += v_y;
if(rect2.x<0)
v_x= -v_x * 0.8f;
else if(rect2.x>=(800-50))
v_x= -v_x * 0.8f;
if(rect2.y<0)
v_y= -v_y * 0.8f;
else if(rect2.y>=(600-50))
v_y= -v_y * 0.8f;
}
//give heap free
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
我的想法是每帧覆盖重力v_x和v_y。有什么问题?当rect命中边界时,v_x / v_y变慢,这是相同的代码。 PS:我使用C和lib SDL2。
当前代码:
else if(event.type==SDL_KEYDOWN)
{
if (keystate[SDL_SCANCODE_A] )
{
v_x -= 0.1f;
}
if (keystate[SDL_SCANCODE_D] )
{
v_x += 0.1f;
}
if (keystate[SDL_SCANCODE_S] )
{
v_y += 0.1f;
}
if (keystate[SDL_SCANCODE_W] )
{
v_y -= 0.1f;
}
}
//make slower(doesnt work)
else
{
v_x-=0.0000005;
v_y-=0.0000005;
if(v_x<0.000005f || v_y<0.000005f)
{
v_x=0;
v_y=0;
}
}
答案 0 :(得分:0)
你遇到的第一个问题是:
else if (SDL_KEYDOWN)
始终为true,因此从未到达else
分支。这应该是:
else if (event.type == SDL_KEYDOWN)
下一个问题是你的速度减慢代码(注意:空格是免费的并且更容易阅读):
v_x -= 0.0000005;
v_y -= 0.0000005;
if (v_x < 0.000005f || v_y < 0.000005f)
{
v_x = 0;
v_y = 0;
}
这仅在初始速度为正时才有效。如果任一速度为负,则立即将两者都设置为0.有几种方法可以正确执行,但最简单的只是明确检查:
const float MIN_SPEED = 0.000005f; // Add to top of function or as a global
...
if (v_x > MIN_SPEED)
v_x -= MIN_SPEED;
else if (v_x < -MIN_SPEED)
v_x += MIN_SPEED;
else
v_x = 0;
并对v_y
执行相同的检查。