простые движущиеся объекты с библиотекой sdl и переполнением стека

Я разработал игру «Mario Bross» с SDL и C ++, чтобы упростить вещи, у меня есть два прямоугольника, и мне пришлось изменить координаты, один из них три шага вверх (-3) и после трех шагов вниз (+3), где когда-либо два прямоугольника пройти через столкновение.

проблема в том, что когда два прямоугольника проходят через столкновение, один из них движется вверх (-3), но он не перемещается вниз (+3). Я перенаправляю координаты прямоугольника, который он меняет вверх и вниз, но он только рендерит движение вверх.

// surprise.h
#ifndef SURPRISE_H_INCLUDED
#define SURPRISE_H_INCLUDED
#include <SDL/SDL.h>
#include <vector>
#include "base.h"class surprise:public baseclass
{
SDL_Rect box;
int xvel,yvel;
SDL_Surface* image;
bool ground;
double frame;
SDL_Rect clips[4];
public:
surprise(SDL_Surface*,int x,int y,int xvel,int yvel);
void show(SDL_Surface*);
void setFrame(double);
void move(int xvel);
double getFrame();
SDL_Rect* getRect();
int getyvel();
void setyvel(int y);
};
#endif // SURPRISE_H_INCLUDED

//surprise.cpp
#include"surprise.h"#include <SDL/SDL.h>
surprise::surprise(SDL_Surface* img ,int x,int y,int xVel,int yVel)
{
image=img;
box.x=x;
box.y=y;
box.w=image->w/2;
box.h=image->h;
xvel=xVel;
yvel=yVel;
ground=0;
for(int i=0;i<3;i++)
{
clips[i].x=i*30;
clips[i].y=0;
clips[i].w=30;
clips[i].h=30;
}
frame=0.0;
}
double  surprise::getFrame()
{
return frame;
}
void surprise::setFrame(double fr)
{
frame=fr;
}
void surprise::show(SDL_Surface* screen)
{
SDL_Rect tmp={box.x-coord.x,box.y,30,30};
if (frame>=2)
{
frame=0.1;
SDL_BlitSurface(image,&clips[(int)(frame+0.5)],screen,&tmp);
}
else
{
SDL_BlitSurface(image,&clips[(int)(frame+0.5)],screen,&tmp);
}
frame+=0.1;
}
SDL_Rect* surprise::getRect()
{
return &box;
}
int surprise::getyvel()
{
return box.y;
}
void surprise::setyvel(int y)
{
box.y+=y;
// part of game .cpp
// the surprise is the first box the player is the second box
//logique part
// i put the surprise class into vector
for (int i=0;i<surprises.size();i++)
{
SDL_Rect tmprect ={surprises[i]->getRect()->x-baseclass::coord.x,surprises[i]->getRect()->y,30,30};
if(collision(&tmprect,player1->getRect()))
{
for (int j=0;j<3;j++)
{
surprises[i]->setyvel(-1);
std::cout<<"increase the y coordinates  "<<surprises[i]->getyvel()->y<<std::endl;
}
for (int k=0;k<3;k++)
{
surprises[i]->setyvel(+1);
std::cout<<"decrease the y coordinates "<<surprises[i]->getRect()->y<<std::endl;
}
}
}
//render part
SDL_Flip(screen);

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]