PDA

View Full Version : Play a simple .gif-animation and KeyEvent-question :)



Technopia
10th September 2015, 14:39
Hello! I am trying to learn how to create a simple game following some nice tutorials on Youtube. I have two questions:

1. There is nothing wrong with the "moving code" I use. It does exacly what I tell it to do. I want to learn how to NOT make the player stop moving to the left or right when I click the spacebutton to shoot :)


void Player::keyPressEvent(QKeyEvent *event){
// move the player left and right
if (event->key() == Qt::Key_Left){
if (pos().x() > 0)
setPos(x()-10,y());
}
else if (event->key() == Qt::Key_Right){
if (pos().x() + 100 < 800)
setPos(x()+10,y());
}
// shoot with the spacebar
else if (event->key() == Qt::Key_Space){
// create a bullet
Projectile * projectile = new Projectile();
projectile->setPos(x()+35,y());
scene()->addItem(projectile);
}
}

2. How to play add and play a simple .gif-animation. I use the following code (a sample) with no errors, but nothing appears in my window:

QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie("C:/Users/Technopia/Desktop/banana.gif"); //Everybody love bananas :cool:
gif_anim->setMovie(movie);
movie->start();


Thanks :o

anda_skoa
10th September 2015, 14:50
1. There is nothing wrong with the "moving code" I use. It does exacly what I tell it to do. I want to learn how to NOT make the player stop moving to the left or right when I click the spacebutton to shoot :)

Then you probably don't want to move on key press, but as long as they is pressed.
E.g. by starting a timer in key press, moving in the slot connected to the timer's timeout() signal, and stopping the timer in key released.


2. How to play add and play a simple .gif-animation. I use the following code (a sample) with no errors, but nothing appears in my window:

QLabel *gif_anim = new QLabel();
QMovie *movie = new QMovie("C:/Users/Technopia/Desktop/banana.gif"); //Everybody love bananas :cool:
gif_anim->setMovie(movie);
movie->start();


Do you show() the label?

Cheers,
_

Technopia
11th September 2015, 13:52
Thanks for reply :) I got the .gif-animation to show up. However in a new window. I am a beginner at this so I have difficult how the structures should be built. I am trying to show an animation if player reach a certain score, and I tried to implent the scoreAnimation function in my main score-function. However it gives me alot of errors. I do think im on right track tho and need a bit of push. Check #code comments for my thought.

Score.h

#ifndef SCORE_H
#define SCORE_H

#include <QGraphicsTextItem>
#include <QLabel> // I guess QLabel must be included here
#include <QMovie> // I guess QMovie must be included here

class Score: public QGraphicsTextItem{ <-- should I add "public: QLabel" and "public: QMovie" here?
public:
Score(QGraphicsItem * parent=0);
//Do I need to add -> Score(QLabel * label) here?
//Do I need to add -> Score(QMovie * movie) here?
void increase();
int getScore();
//int scoreAnimation(); <- This needs to be here I guess
private:
int score;
};

#endif // SCORE_H


#include "score.h"
#include <QFont>
#include <QDebug>
#include <QLabel>
#include <QMovie>
#include <QtGui>

Score::Score(QGraphicsItem *parent): QGraphicsTextItem(parent){
score = 0;

setPlainText(QString("Score: ") + QString::number(score));
setDefaultTextColor(Qt::yellow);
setFont(QFont("times",16));
}

void Score::increase(){
score++;
scoreAnimation(); // <--- Here I call another function
setPlainText(QString("Score: ") + QString::number(score));
}

int Score::getScore(){
return score;
}

int Score::scoreAnimation(){
switch(score){
case 1:
/* This is the part I try to add an animation. I guess i need to set loopcount to one also. This code gives me alot of errors and im not sure how to structure it up :D
qDebug() << "10 Score to Gryffindor!";
QLabel* label = new QLabel();
QMovie *movie = new QMovie(":/images/C:/Users/Damien/Desktop/test.gif");
label->setMovie(movie);
movie->start();
label->show();
*/
break;
default:
break;
}
return 0;
}

Thanks. An explanation would give me a great amount of understanding about how the structure works.

anda_skoa
11th September 2015, 14:42
Ah, I see.

So, the animation showed up in a separate window because the label you are using has no parent.
A QWidget without a parent becomes a window by itself.

From your new code I take it you want the animation inside a QGraphicsScene.
For that you need a slightly different approach.

Create your own animation item, by deriving from QObject and QGraphicsPixmapItem.
Let it have a QMovie as a member or provide the QMovie via a setter and store the pointer to it as a member.

In either case make the item show the movie's currentPixmap().

Also connect to the movie's frameChanged() signal to a slot that calls update() on the item.

Cheers,
_