我正在尝试在C + SDL2中制作一个简单的乒乓球游戏时,在Games From Scratch页面上。我试图让球移动,但发现它总是移动太快,即使在跟随此之后。
以下是有问题的代码:
// Update ball position
prevTime = curTime;
curTime = SDL_GetTicks() - prevTime;
delta = curTime / 1000;
ball.y = (int) (ball.y - (BALL_SPD * delta));
想知道是否有人可以帮我解决这个问题?我只是试图让球慢慢移动到屏幕顶部,但它几乎立刻就过快了。
这里是完整的代码:
#include <SDL.h>
#include <stdio.h>
#include <stdbool.h>
#define SCR_W 1280
#define SCR_H 720
#define BALL_SPD 5
int setUpWindowAndRenderer(SDL_Window **w, SDL_Renderer **r);
void setUpPaddles(SDL_Rect *player1, SDL_Rect *player2);
void setUpBoard(SDL_Rect *dash);
int main(int argc, char *args[]){
// SDL Initialisation and creation of window and renderer
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
if(setUpWindowAndRenderer(&window, &renderer) < 0){
printf("setUpWindowAndRenderer() failed! Exiting...\n");
exit(EXIT_FAILURE);
}
// SDL_SetRelativeMouseMode(SDL_TRUE);
// Stores user input
SDL_Event e;
// Tests to see if game has been quit in main loop
bool quit = false;
// Set up both player paddles
SDL_Rect player1;
SDL_Rect player2;
setUpPaddles(&player1, &player2);
// Set up ball
SDL_Rect ball;
ball.x = SCR_W / 4;
ball.y = SCR_H / 2;
ball.w = 25;
ball.h = 25;
// Set up delta/timing
float delta = 0, curTime = 0, prevTime = 0;
// Mouse y-position
int mY;
// Central line initial rect
SDL_Rect lineRect = {(SCR_W / 2) - 5, SCR_H, 10, 25};
// Main game loop
while(!quit){
// Check user input
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
quit = true;
} else if(e.type == SDL_KEYDOWN){
switch(e.key.keysym.sym){
case SDLK_w:
// GO THE FUCK UP
break;
case SDLK_s:
// GO DOWN TO THE GROUND BOI
break;
default:
printf("Invalid key.\n");
break;
}
}
}
// Update Player 1 Paddle
// Update P1 paddle to mouse y position
SDL_GetMouseState(NULL, &mY);
player1.y = mY - (player1.h / 2);
// Limit position if paddle y pos goes below zero or above window height
if(player1.y < 0){
player1.y = 0;
}
if(player1.y + player1.h > SCR_H){
player1.y = SCR_H - player1.h;
}
// Update ball position
prevTime = curTime;
curTime = SDL_GetTicks() - prevTime;
delta = curTime / 1000;
ball.y = (int) (ball.y - (BALL_SPD * delta));
// Limit position if ball y pos goes below zero or above window height
if(ball.y < 0){
ball.y = 0;
}
if(ball.y + ball.h > SCR_H){
ball.y = SCR_H;
}
// Clear screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
// Draw central line
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
while(lineRect.y <= SCR_H && lineRect.y >= 0){
SDL_RenderFillRect(renderer, &lineRect);
lineRect.y -= 50;
}
lineRect.y = SCR_H;
// Draw paddles and ball
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &player1);
SDL_RenderFillRect(renderer, &player2);
SDL_RenderFillRect(renderer, &ball);
SDL_RenderPresent(renderer);
}
SDL_Quit();
return 0;
}
int setUpWindowAndRenderer(SDL_Window **w, SDL_Renderer **r){
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("SDL_Init() failed! %s\n", SDL_GetError());
return -1;
}
if((*w = SDL_CreateWindow("Pong Clone", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCR_W, SCR_H, 0)) == NULL){
printf("SDL_CreateWindow() failed! %s\n", SDL_GetError());
return -1;
}
if((*r = SDL_CreateRenderer(*w, -1, 0)) == NULL){
printf("SDL_CreateRenderer() failed! %s\n", SDL_GetError());
return -1;
}
return 0; // success
}
void setUpPaddles(SDL_Rect *player1, SDL_Rect *player2){
// Define paddle width and height
int pW = 30;
int pH = 100;
player1->x = pW * 2; // Position P1 paddle 2 paddle-widths away from screen edge
player1->y = (SCR_H / 2) - (pH / 2); // Position Paddle in direct centre
player1->w = pW; // Paddle Width
player1->h = pH; // Paddle Height
player2->x = SCR_W - (pW * 3); // Position P2 paddle 2 paddle-widths away from screen edge
player2->y = (SCR_H / 2) - (pH / 2); // Position Paddle in direct centre
player2->w = pW; // Paddle Width
player2->h = pH; // Paddle Height
}