#include "player.h" #include <QObject> #include <QGraphicsPixmapItem> #include <QKeyEvent> #include<QGraphicsScene> #include"mouse.h" #include <QTimer> player::player(QPixmap pixmap, Score* score) { setPixmap(pixmap); QTimer * timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(advanced())); timer->start(50); myscore = score; } //These functions move the player object and change the sprite when an arrow key is pressed void player::keyPressEvent(QKeyEvent *event){ if (event->key() == Qt::Key_Left){ setPos(x()-30,y()); if (l_key == 0){ setPixmap(QPixmap(":/images/cat_left.png")); l_key = 1;} //there are two diffrent images for each arrow key direction, the program alternates //between these two images so the cat looks like it is walking else if ( l_key == 1){ setPixmap(QPixmap(":/images/cat_left_alt.png")); l_key = 0; } } else if (event->key() == Qt::Key_Right){ setPos(x()+30,y()); if (r_key == 0){ setPixmap(QPixmap(":/images/cat_right.png")); r_key = 1; } else if (r_key == 1){ setPixmap(QPixmap(":/images/cat_right_alt.png")); r_key = 0; } } else if (event->key() == Qt::Key_Up){ setPos(x(),y()-30); if (u_key == 0){ setPixmap(QPixmap(":/images/cat_up.png")); u_key = 1; } else if (u_key == 1){ setPixmap(QPixmap(":/images/cat_up_alt.png")); u_key = 0; } } else if (event->key() == Qt::Key_Down){ setPos(x(),y()+30); if (d_key == 0 ){ setPixmap(QPixmap(":/images/cat_down.png")); d_key = 1; } else if (d_key == 1){ setPixmap(QPixmap(":/images/cat_down_alt.png")); d_key = 0; } } } void player::advanced(){ QList<QGraphicsItem *> colliding_items = collidingItems(); for (int i = 0, n = colliding_items.size(); i < n; ++i){ if (typeid(*(colliding_items[i])) == typeid(Mouse)){ //if the player collides with a mouse... scene()->removeItem(colliding_items[i]); delete colliding_items[i]; //delete the mouse myscore->increase(); //increase the score Mouse *mouse = new Mouse; scene()->addItem(mouse); //adds new mouse return; } } }