79 lines
1.8 KiB
C++
79 lines
1.8 KiB
C++
#include "player.h"
|
|
|
|
Player::Player(QWidget *parent)
|
|
: QWidget(parent)
|
|
{
|
|
this->setFocusPolicy(Qt::StrongFocus);
|
|
this->startTimer(20);
|
|
this->QWidget::setFocus();
|
|
|
|
RectItem = new QGraphicsRectItem();
|
|
this->RectItem->setRect(0, 0, 100, 100);
|
|
this->RectItem->setBrush(QBrush(Qt::red));
|
|
}
|
|
|
|
Player::~Player()
|
|
{
|
|
|
|
}
|
|
|
|
void Player::setMaxSize(QSize WindowSize)
|
|
{
|
|
this->mainWindowSize = WindowSize;
|
|
}
|
|
|
|
void Player::setSpeed(unsigned movePixel)
|
|
{
|
|
this->Speed = static_cast<int>(movePixel);
|
|
}
|
|
|
|
void Player::keyPressEvent(QKeyEvent *e)
|
|
{
|
|
keys[e->key()] = true;
|
|
QWidget::keyPressEvent(e);
|
|
}
|
|
|
|
void Player::keyReleaseEvent(QKeyEvent *e)
|
|
{
|
|
keys[e->key()] = false;
|
|
QWidget::keyReleaseEvent(e);
|
|
}
|
|
|
|
void Player::mousePressEvent(QMouseEvent *e)
|
|
{
|
|
std::cout << "Geklicket: " << e->x() << " - " << e->y() << std::endl;
|
|
}
|
|
|
|
void Player::mouseReleaseEvent(QMouseEvent *e)
|
|
{
|
|
std::cout << "Geklicket: " << e->x() << " - " << e->y() << std::endl;
|
|
|
|
}
|
|
|
|
void Player::timerEvent(QTimerEvent *)
|
|
{
|
|
if(this->keys[Qt::Key_Up] || this->keys[Qt::Key_W]) {
|
|
if(this->RectItem->pos().y() - Speed >= 0)
|
|
this->RectItem->moveBy(0, Speed*-1);
|
|
|
|
}
|
|
if(this->keys[Qt::Key_Down] || this->keys[Qt::Key_S]) {
|
|
if( this->RectItem->pos().y() + Speed + this->RectItem->rect().height() < this->mainWindowSize.height() )
|
|
this->RectItem->moveBy(0, Speed);
|
|
|
|
}
|
|
if(this->keys[Qt::Key_Left] || this->keys[Qt::Key_A]) {
|
|
if(this->RectItem->pos().x() - Speed >= 0)
|
|
this->RectItem->moveBy(Speed*-1, 0);
|
|
|
|
}
|
|
if(this->keys[Qt::Key_Right] || this->keys[Qt::Key_D]) {
|
|
if(this->RectItem->pos().x() + Speed + this->RectItem->rect().width() < this->mainWindowSize.width() )
|
|
this->RectItem->moveBy(Speed, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|