gpt4 book ai didi

c - SDL2/C 中放慢矩形速度

转载 作者:行者123 更新时间:2023-11-30 17:26:25 31 4
gpt4 key购买 nike

你好,我尝试在我的程序中拥有真正的重力,因此当我不按键时,较小的矩形应该减慢速度。这是我尝试过的代码,但什么也没发生(在这方面):

 #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。有什么问题?当矩形碰到边界时,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;
}
}

最佳答案

你的第一个问题是:

else if (SDL_KEYDOWN)

这始终是正确的,因此从未到达 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 进行相同的检查。

关于c - SDL2/C 中放慢矩形速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807774/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com