使用 C 中的 CSFML 库进行跳跃
Make a jump with the CSFML library in C
对于一个项目,我必须让精灵跳跃,但我不知道该怎么做我已经测试过了
while(sfRenderWindow_pollEvent(window, &event)) {
if (event.type == sfEvtClosed) {
sfRenderWindow_close(window);
}
if (event.type == sfEvtKeyPressed) {
if (event.key.code == sfKeySpace || sfKeyUp)
jumping = 1;
}
}
if (jumping) {
while (clock < 60) {
printf("%f\n", dino_pos.y);
if (clock < 30) {
dino_pos.y -= 2;
sfSprite_setPosition(dino, dino_pos);
clock++;
} else if (clock < 60) {
dino_pos.y += 2;
sfSprite_setPosition(dino, dino_pos);
clock++;
}
}
if (clock >= 60)
clock = 0;
printf("Jump\n");
}
jumping = 0;
但他不起作用,我的程序是 60 fps,所以我希望跳跃持续 1 秒。 Printf 在这里查看我的恐龙的位置,但我猜跳跃是即时的所以我没有时间看跳跃。你有跳伞的想法吗?
您的程序逻辑应如下所示:
注意:这只是一个简化版本,不是一个工作示例。它应该会提示您如何操作。
int main()
{
openWindow();
while (active) {
getTime();
processMessages();
doLogic();
renderScene();
swapBuffers();
}
closeWindow();
}
void getTime()
{
// you have to know the time unit (sec, ms, us, ns, etc.)
current_time = systemTime();
}
void processMessages()
{
while (nextEvent()) {
if (EventType == EvtClosed) {
active = false;
}
if ((EventType == KeyPressed) && (KeyType == KeySpace) && !jumping) {
jump_start = current_time;
jumping = true;
}
}
}
void doLogic()
{
if (jumping) {
// time_frac has to be in range [0.0..1.0]
time_frac = (current_time - jump_start) / time_units_per_second;
// linear interpolation based on time fraction
pos = start_pos * (1 - time_frac) + end_pos * time_frac;
}
}
void renderScene()
{
printf("%f\n", pos);
}
对于一个项目,我必须让精灵跳跃,但我不知道该怎么做我已经测试过了
while(sfRenderWindow_pollEvent(window, &event)) {
if (event.type == sfEvtClosed) {
sfRenderWindow_close(window);
}
if (event.type == sfEvtKeyPressed) {
if (event.key.code == sfKeySpace || sfKeyUp)
jumping = 1;
}
}
if (jumping) {
while (clock < 60) {
printf("%f\n", dino_pos.y);
if (clock < 30) {
dino_pos.y -= 2;
sfSprite_setPosition(dino, dino_pos);
clock++;
} else if (clock < 60) {
dino_pos.y += 2;
sfSprite_setPosition(dino, dino_pos);
clock++;
}
}
if (clock >= 60)
clock = 0;
printf("Jump\n");
}
jumping = 0;
但他不起作用,我的程序是 60 fps,所以我希望跳跃持续 1 秒。 Printf 在这里查看我的恐龙的位置,但我猜跳跃是即时的所以我没有时间看跳跃。你有跳伞的想法吗?
您的程序逻辑应如下所示:
注意:这只是一个简化版本,不是一个工作示例。它应该会提示您如何操作。
int main()
{
openWindow();
while (active) {
getTime();
processMessages();
doLogic();
renderScene();
swapBuffers();
}
closeWindow();
}
void getTime()
{
// you have to know the time unit (sec, ms, us, ns, etc.)
current_time = systemTime();
}
void processMessages()
{
while (nextEvent()) {
if (EventType == EvtClosed) {
active = false;
}
if ((EventType == KeyPressed) && (KeyType == KeySpace) && !jumping) {
jump_start = current_time;
jumping = true;
}
}
}
void doLogic()
{
if (jumping) {
// time_frac has to be in range [0.0..1.0]
time_frac = (current_time - jump_start) / time_units_per_second;
// linear interpolation based on time fraction
pos = start_pos * (1 - time_frac) + end_pos * time_frac;
}
}
void renderScene()
{
printf("%f\n", pos);
}