Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 渲染使用“开发”的游戏图像;C++;和SDL图书馆“;_C++ - Fatal编程技术网

C++ 渲染使用“开发”的游戏图像;C++;和SDL图书馆“;

C++ 渲染使用“开发”的游戏图像;C++;和SDL图书馆“;,c++,C++,在bullet类中,我必须改变游戏的角度,我在移动方法中增加了角度,事实是图像在移动,并且它没有改变角度。 在render中的STD“cout”项目符号角度的帮助下,我在控制台中重定向了set angle的结果:“子弹[i]->getangle()。 在我看来,它给出了正确的结果,但我不知道为什么图像不改变角度 //bullet.h #ifndef BULLET_H_INCLUDED #define BULLET_H_INCLUDED #include <

在bullet类中,我必须改变游戏的角度,我在移动方法中增加了角度,事实是图像在移动,并且它没有改变角度。 在render中的STD“cout”项目符号角度的帮助下,我在控制台中重定向了set angle的结果:“子弹[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);
//bullet.h
#如果不包括BULLET_H_
#定义包含的项目符号
#包括
#包括
等级子弹
{
SDL矩形盒;
int xvel,yvel,角度;
SDL_曲面*image,*bull;
公众:
子弹();
项目符号(SDL_表面*img、int x、int y、int xvel、int yvel、int角度);
无效移动();
无效显示(SDL_表面*屏幕);
SDL_Rect*getRect();
SDL_表面*加载_图像(常量字符*文件名,SDL_表面*屏幕);
int getangle();
空隙设置角(内角);
};
#endif//BULLET\u包括在内
//bullet.cpp
#包括“bullet.h”
#包括“SDL_rotozoom.h”
//第一建筑
bullet::bullet(SDL_表面*img,int x,int y,int xVel,int yVel,int ang)
{
方框x=x;
方框y=y;
图像=img;
box.w=图像->w;
box.h=图像->h;
xvel=xvel;
yvel=yvel;
角度=ang;
}
//二次构造
bullet::bullet()
{
角度+=30;
}
//移动方法
void bullet::move()
{
box.x+=xvel;
box.y+=yvel;
角度+=20;
}
//获得角度
int bullet::getangle()
{
返回角;
}
//设定角度
无效项目符号::设置角度(int agl)
{
角度=(((角度+agl)%360)+360)%360)
}
//展示子弹的方法
无效项目符号::显示(SDL_表面*屏幕)
{
SDL_BlitSurface(图像、空值、屏幕和方框);
}
//获取项目符号矩形的方法
SDL_Rect*项目符号::getRect()
{
返回&框;
}
//加载图像的方法
SDL_表面*项目符号::加载_图像(常量字符*文件名,SDL_表面*屏幕)
{
SDL_表面*tmp=SDL_加载BMP(文件名);
SDL_表面*tmp2=SDL_显示格式(tmp);
SDL_设置颜色键(tmp2、SDL_SRCCOLORKEY、SDL_映射RGB(屏幕->格式,0x00,0xff,0xff));
SDL_表面*rotozoomimage=rotozoomSurface(tmp2,角度,1.0,0);
SDL_自由曲面(tmp);
SDL_自由曲面(tmp2);
返回rotozoomimage;
}
//加载图像法
SDL_表面*项目符号::加载_图像(常量字符*文件名,SDL_表面*屏幕)
{
SDL_表面*tmp=SDL_加载BMP(文件名);
SDL_表面*tmp2=SDL_显示格式(tmp);
SDL_设置颜色键(tmp2、SDL_SRCCOLORKEY、SDL_映射RGB(屏幕->格式,0x00,0xff,0xff));
SDL_表面*rotozoomimage=rotozoomSurface(tmp2,角度,1.0,0);
SDL_自由曲面(tmp);
SDL_自由曲面(tmp2);
返回rotozoomimage;
}
//移动方法
void bullet::move()
{
box.x+=xvel;
box.y+=yvel;
角度+=20;
}
//接二连三
int bullet::getangle()
{
返回角;
}
无效项目符号::设置角度(int agl)
{
如果(角度>=360)
{
角度-=360;
}

否则,如果(角度据我所知,在加载图像后,您从未对任何内容使用
angle


当角度发生变化时,您需要旋转图像。

首先,为什么每个方法定义都有两个副本。其次,默认构造函数没有意义。第三,您只在
load\u image
中使用角度,而不在
show
中使用角度,这就解释了您的问题。bullet是您的类。在那里更改值后,如何使用角度SDL知道它必须更新图像吗?在bullet::move中更改角度值后,您应该进行一些其他SDL调用。检查SDL文档。谢谢,非常感谢,朋友们,它在show()方法中工作,我始终渲染静态图像SDL_BlitSurface([image],NULL,screen,&box);但是我做了这个SDL_BlitSurface(loadimage()),NULL,screen,&box);它工作了,:)