gpt4 book ai didi

c++ - QT如何使用setPixmap skin.copy()做动画?

转载 作者:行者123 更新时间:2023-11-30 05:40:29 28 4
gpt4 key购买 nike

我想在我的应用程序中做一些动画。我有一颗小行星,当他向右移动时,动画会向右移动,当他向左移动时,动画会向左移动,角度在 0 到 360° 之间。我已经为你添加了瓷砖,向我解释如何完美地切割它。我在类里面有一个整数,数字在 0 和 34 之间(图像上的小行星)我在每个动画中减少 -1 或向该变量添加 +1。

为小行星制作动画的方法:

void Asteroid::animation(){
int x = 1;
int y = 1;
int width = 5;
int height = 5;
// what should I add here?

if(this->angle >= 0 && this->angle < 180){
// cut image go to the right
setPixmap(this->skin.copy(x,y,width,height));

this->frame++;
if(frame > 34) this->frame = 1;
} else {
// cut image go to the left
setPixmap(this->skin.copy(x,y,width,height));

this->frame--;
if(frame < 1) this->frame = 34;
}
}

皮肤像素图的内容:

Asteroids skin

界面

#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QPoint>
#include <QVector>
#include <QPixmap>
#include <QString>
#include <QTimer>
#include <QObject>
#include <time.h>
#include <QtCore/qmath.h>

#include "SettingsAsteroid.h"

class Asteroid : public QObject, public QGraphicsPixmapItem
{
Q_OBJECT

// Animation
QPixmap skin;
int frame; // 32 images

// Data
int speed;
int size;
int pv;
float angle;

public:
Asteroid(int size);
Asteroid(QPointF position, int size);
~Asteroid();

int getPosX();
int getPosY();

void setPosX(int newPos);
void setPosY(int newPos);

int getSpeed();
int getSize();
int getPV();
int getAngle();

// météorite détruite
void destroyed();

// toucher par un tir
void touched(int damage);

bool isDestroyed();

private slots:
// mouvement de la météorite
void move();

// animation de la météorite
void animation();

signals:
//void notKilled();

// transmet la taille du météorite détruit
void killed(int);
};

实现

#include "Asteroid.h"

Asteroid::Asteroid(int size)
{
this->skin = QPixmap(SettingsAsteroid::getRandomSkin());
this->speed = SettingsAsteroid::getRandomSpeed(4,8);
this->pv = SettingsAsteroid::getPV(size);
this->angle = SettingsAsteroid::getRandomAngle();
this->frame = 1;

// Position de départ random
QPointF position = QPointF(SettingsAsteroid::getRandomStartPosition());
this->setPos(position.x(), position.y());

this->size = size;

// mouvement de l'astéroide
QTimer* timer2 = new QTimer();
connect(timer2,SIGNAL(timeout()),this,SLOT(move()));
timer2->start(this->speed);

// animation de l'astéroide
QTimer* timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(animation()));
timer->start(100);
}

Asteroid::Asteroid(QPointF position, int size)
{
this->skin = QPixmap(SettingsAsteroid::getRandomSkin());
this->speed = SettingsAsteroid::getRandomSpeed(4,8);
this->pv = SettingsAsteroid::getPV(size);
this->angle = SettingsAsteroid::getRandomAngle();
this->frame = 1;


this->setPos(position.x(), position.y());

this->size = size;

QTimer* timer2 = new QTimer();
connect(timer2,SIGNAL(timeout()),this,SLOT(move()));
timer2->start(this->speed);

//Decoupe sprite et anmiation
QTimer* timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(animation()));
timer->start(150);
}

Asteroid::~Asteroid()
{
}

int Asteroid::getPosX(){
return this->pos().x();
}

int Asteroid::getPosY(){
return this->pos().y();
}

void Asteroid::setPosX(int newPos){
this->setPos(newPos, this->pos().y());
}

void Asteroid::setPosY(int newPos){
this->setPos(this->pos().x(), newPos);
}

int Asteroid::getSpeed(){
return this->speed;
}

int Asteroid::getSize(){
return this->size;
}

int Asteroid::getPV(){
return this->pv;
}

int Asteroid::getAngle(){
return this->angle;
}

bool Asteroid::isDestroyed(){

if(pv > 0) return false;
else return true;
}

void Asteroid::destroyed(){
// AVERTIR CLASSE PRINCIPALE QUE DETRUIT
}

void Asteroid::touched(int damage){
this->pv -= damage;
if(pv<=0) destroyed();
}

void Asteroid::move(){
double dx = getSpeed() * qCos(qDegreesToRadians(angle)) ;
double dy = getSpeed() * qSin(qDegreesToRadians(angle)) ;

setPos(QPointF(getPosX() + dx, getPosY() + dy)) ;

// place la météorite de l'autre coté de la scène
if (getPosX()<=0) setPosX(799);
else if (getPosY()<=0) setPosY(659);
else if (getPosX()>= 800) setPosX(1);
else if (getPosY()>= 600) setPosY(1);
}

void Asteroid::animation(){
// what add here?
int x = 1;
int y = 1;
int width = 5;
int height = 5;

if(this->angle >= 0 && this->angle < 180){
// cut image go to the right
setPixmap(this->skin.copy(x,y,width,height));

this->frame++;
if(frame > 34) this->frame = 1;
} else {
// cut image go to the left
setPixmap(this->skin.copy(x,y,width,height));
this->frame--;
if(frame < 1) this->frame = 34;
}
}

角度是场景中小行星的方向。将会有一艘宇宙飞船应该摧毁小行星。我想制作在现场移动的小行星的动画

最佳答案

假设皮肤中的小行星图片是正方形的,可以如下操作。

顺便说一句,你的皮肤只有 32 项,而不是 34 项。

void Asteroid::animation() {
int step = skin.height();
int N = skin.width() / step; // Number of images in the skin.
Q_ASSERT(skin.width() % step == 0); // ensure proper format of the skin

if (angle >= 0 && angle < 180) {
frame ++;
if (frame > N) frame -= N;
} else {
frame --;
if (frame < 1) frame += N;
}

int x = (frame - 1) * step;
setPixmap(skin.copy(x, 0, step, step));
}

关于c++ - QT如何使用setPixmap skin.copy()做动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31615286/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com