0

我用 SDL 和 c++ 开发了游戏“mario bross”来简化事情,我有两个矩形,我必须改变其中一个坐标,其中一个向上三步(-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);
4

0 回答 0