在子弹类中,我必须改变游戏的角度,我在移动方法中增加了角度,事实是图像在移动,它没有改变角度。我借助渲染中的 STD“cout”子弹角度重定向了控制台中设置角度的结果:“bullets[i]->getangle()”。在我看来,它给出了正确的结果,但我不知道为什么图像不会改变角度。
//bullet.h
#ifndef BULLET_H_INCLUDED
#define BULLET_H_INCLUDED
#include <SDL/SDL.h>
#include <iostream>
class bullet
{
SDL_Rect box;
int xvel,yvel,angle;
SDL_Surface *image,*bull;
public:
bullet ();
bullet(SDL_Surface* img,int x,int y,int xvel,int yvel,int angle);
void move();
void show(SDL_Surface* screen);
SDL_Rect* getRect();
SDL_Surface* load_image(const char* filename,SDL_Surface* screen);
int getangle();
void setangle(int angle);
};
#endif // BULLET_H_INCLUDED
//bullet.cpp
#include "bullet.h"
#include "SDL_rotozoom.h"
//first construc
bullet::bullet(SDL_Surface* img,int x,int y,int xVel,int yVel,int ang)
{
box.x=x;
box.y=y;
image=img;
box.w=image->w;
box.h=image->h;
xvel=xVel;
yvel=yVel;
angle=ang;
}
//second construc
bullet::bullet()
{
angle+=30;
}
//move methode
void bullet::move()
{
box.x+=xvel;
box.y+=yvel;
angle+=20;
}
//get the angle
int bullet::getangle()
{
return angle;
}
// set the angle
void bullet::setangle(int agl)
{
angle = ((((angle + agl) % 360) + 360) % 360)
}
//methode to show bullet
void bullet::show(SDL_Surface* screen)
{
SDL_BlitSurface(image,NULL,screen,&box);
}
// methode to get bullet rectangle
SDL_Rect* bullet::getRect()
{
return &box ;
}
// methode that load the image
SDL_Surface* bullet::load_image(const char* filename,SDL_Surface* screen)
{
SDL_Surface* tmp=SDL_LoadBMP(filename);
SDL_Surface* tmp2=SDL_DisplayFormat(tmp);
SDL_SetColorKey(tmp2,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));
SDL_Surface* rotozoomimage=rotozoomSurface(tmp2,angle,1.0,0);
SDL_FreeSurface(tmp);
SDL_FreeSurface(tmp2);
return rotozoomimage;
}
// loadimage methode
SDL_Surface* bullet::load_image(const char* filename,SDL_Surface* screen)
{
SDL_Surface* tmp=SDL_LoadBMP(filename);
SDL_Surface* tmp2=SDL_DisplayFormat(tmp);
SDL_SetColorKey(tmp2,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));
SDL_Surface* rotozoomimage=rotozoomSurface(tmp2,angle,1.0,0);
SDL_FreeSurface(tmp);
SDL_FreeSurface(tmp2);
return rotozoomimage;
}
// move methode
void bullet::move()
{
box.x+=xvel;
box.y+=yvel;
angle+=20;
}
// getter and setter
int bullet::getangle()
{
return angle;
}
void bullet::setangle(int agl)
{
if (angle>=360)
{
angle-=360;
}
else if (angle<=-360)
{
angle+=360;
}
angle+=agl;
}
//class game
//logique part
// game.cpp
// logique
.
.
.
for (int i=0;i<bullets.size();i++)
{
bullets[i]->setangle(20);
std::cout<<"bullet angle :"<<bullets[i]->getangle()<<std::endl;
bullets[i]->move();
}
//render part
for (int i=0;i<bullets.size();i++)
{
bullets[i]->show(screen);
std::cout<<"bullet angle in render :"<<bullets[i]->getangle()<<std::endl;
}
SDL_Flip(screen);