我正在使用 C++ 自学 SDL、OpenGL 等。在线教程帮助我创建了一个在屏幕上移动的盒子和一个盒子不能通过的窗台......
我没有成功找到一个教程来解释和展示如何让盒子跳跃或跳跃。理想情况下,我希望我的盒子能够移动并跳到类似于老式马里奥的壁架上。
我不确定这是否可以通过 SDL 完成?我猜需要添加一些东西才能在按下 up 后让盒子回到地面......我已经尝试了up
/down
键并试图让盒子在按下后返回地面但它没有工作。我试过把盒子放在地上,但是向上键不起作用。我也尝试过,一旦按下并且盒子已经移动以使盒子下降相同的距离......我有点明白它需要如何实现(我认为),我只是不知道足够的 C++写它,或者它甚至可以完成。
目前我可以向左或向右和向上移动,但他仍然漂浮在空中:(
这是我的代码:
#include "SDL.h"
#include "SDL_image.h"
#include <string>
const int SCREEN_WIDTH = 480;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 20;
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;
SDL_Surface *square = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Rect wall;
class Square{
private:
SDL_Rect box;
int xVel, yVel;
public:
Square();
void handle_input();
void move();
void show();};
class Timer{
private:
int startTicks;
int pausedTicks;
bool paused;
bool started;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();};
SDL_Surface *load_image( std::string filename ){
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if( optimizedImage != NULL )
{
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 237, 145, 33 ) );//optimizedImage->format, 0, 0xFF, 0xFF
}
}
return optimizedImage;}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, clip, destination, &offset );}
bool check_collision( SDL_Rect A, SDL_Rect B ){
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
if( bottomA <= topB ){return false;}
if( topA >= bottomB ){return false;}
if( rightA <= leftB ){return false;}
if( leftA >= rightB ){return false;}
return true;}
bool init(){
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ){return false;}
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL ){return false;}
SDL_WM_SetCaption( "Move the Square", NULL );
return true;}
bool load_files(){
square = load_image( "square.bmp" );
if( square == NULL ){return false;}
return true;}
void clean_up(){
SDL_FreeSurface( square );
SDL_Quit();}
Square::Square(){
box.x = 50;
box.y = 360;
box.w = SQUARE_WIDTH;
box.h = SQUARE_HEIGHT;
xVel = 0;
yVel = 0;}
void Square::handle_input(){
if( event.type == SDL_KEYDOWN ){
switch( event.key.keysym.sym ){
case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
//case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;}}
else if( event.type == SDL_KEYUP ){
switch( event.key.keysym.sym ){
case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
//case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;}}}
void Square::move(){
box.x += xVel;
if( ( box.x < 0 ) || ( box.x + SQUARE_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) ){
box.x -= xVel;}
box.y += yVel;
if( ( box.y < 0 ) || ( box.y + SQUARE_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) ){
box.y -= yVel;}}
void Square::show(){
apply_surface( box.x, box.y, square, screen );}
Timer::Timer(){
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;}
void Timer::start(){
started = true;
paused = false;
startTicks = SDL_GetTicks();}
void Timer::stop(){
started = false;
paused = false;}
void Timer::pause(){
if( ( started == true ) && ( paused == false ) ){
paused = true;
pausedTicks = SDL_GetTicks() - startTicks;}}
void Timer::unpause(){
if( paused == true ){
paused = false;
startTicks = SDL_GetTicks() - pausedTicks;
pausedTicks = 0;}}
int Timer::get_ticks(){
if( started == true ){
if( paused == true ){
return pausedTicks;}
else{return SDL_GetTicks() - startTicks;}
}return 0;}
bool Timer::is_started(){return started;}
bool Timer::is_paused(){return paused;}
int main( int argc, char* args[] ){
bool quit = false;
Square mySquare;
Timer fps;
if( init() == false ){return 1;}
if( load_files() == false ){return 1;}
wall.x = 130;
wall.y = 300;
wall.w = 220;
wall.h = 20;
while( quit == false ){
fps.start();
while( SDL_PollEvent( &event ) ){
mySquare.handle_input();
if( event.type == SDL_QUIT ){
quit = true;}}
mySquare.move();
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 1, 1, 1 ) );
SDL_FillRect( screen, &wall, SDL_MapRGB( screen->format, 237, 145, 33 ) );
mySquare.show();
if( SDL_Flip( screen ) == -1 ){return 1;}
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ){
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );}}
clean_up();
return 0;}