NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Bullet.cpp:

#include "Bullet.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include "Enemy.h"
#include "Game.h"

extern Game * game;
Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsRectItem(parent){

setRect(0,0,10,50);
QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));

timer->start(50);
}

void Bullet::move(){

QList<QGraphicsItem *> colliding_items = collidingItems();


for (int i = 0, n = colliding_items.size(); i < n; ++i){
if (typeid(*(colliding_items[i])) == typeid(Enemy)){
game->score->increase();


scene()->removeItem(colliding_items[i]);
scene()->removeItem(this);


delete colliding_items[i];
delete this;


return;
}
}


setPos(x(),y()-10);

if (pos().y() + rect().height() < 0){
scene()->removeItem(this);
delete this;
}
}

Bullet.h:

#ifndef BULLET
#define BULLET

#include <QGraphicsRectItem>
#include <QGraphicsItem>
#include <QObject>

class Bullet: public QObject,public QGraphicsRectItem{
Q_OBJECT
public:
Bullet(QGraphicsItem * parent=0);
public slots:
void move();
};

#endif // BULLET

Enemy.cpp:

#include "Enemy.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QList>
#include <stdlib.h> // rand() -> really large int
#include "Game.h"

extern Game * game;

Enemy::Enemy(QGraphicsItem *parent): QObject(), QGraphicsRectItem(parent){
int random_number = rand() % 700;
setPos(random_number,0);


setRect(0,0,100,100);


QTimer * timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(move()));


timer->start(50);
}

void Enemy::move(){

setPos(x(),y()+5);


if (pos().y() > 600){

game->health->decrease();

scene()->removeItem(this);
delete this;
}
}

Enemy.h:

#ifndef ENEMY
#define ENEMY

#include <QGraphicsRectItem>
#include <QObject>
#include <QGraphicsItem>

class Enemy: public QObject,public QGraphicsRectItem{
Q_OBJECT
public:
Enemy(QGraphicsItem * parent=0);
public slots:
void move();
};

#endif // ENEMY

Game.cpp:

#include "Game.h"
#include <QTimer>
#include <QGraphicsTextItem>
#include <QFont>
#include "Enemy.h"

Game::Game(QWidget *parent){

scene = new QGraphicsScene();
scene->setSceneRect(0,0,800,600); // make the scene 800x600 instead of infinity by infinity (default)


setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(800,600);


player = new Player();
player->setRect(0,0,100,100); // change the rect from 0x0 (default) to 100x100 pixels
player->setPos(400,500); // TODO generalize to always be in the middle bottom of screen

player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus();

scene->addItem(player);


score = new Score();
scene->addItem(score);
health = new Health();
health->setPos(health->x(),health->y()+25);
scene->addItem(health);


QTimer * timer = new QTimer();
QObject::connect(timer,SIGNAL(timeout()),player,SLOT(spawn()));
timer->start(1500);

show();
}

Game.h:
#ifndef GAME
#define GAME

#include <QGraphicsView>
#include <QWidget>
#include <QGraphicsScene>
#include "Player.h"
#include "Score.h"
#include "Health.h"

class Game: public QGraphicsView{
public:
Game(QWidget * parent=0);

QGraphicsScene * scene;
Player * player;
Score * score;
Health * health;

};

#endif // GAME

Health.cpp:

#include "Health.h"
#include <QFont>

Health::Health(QGraphicsItem *parent): QGraphicsTextItem(parent){

health = 3;


setPlainText(QString("Health: ") + QString::number(health)); // Health: 3
setDefaultTextColor(Qt::black);
setFont(QFont("times",16));
}

void Health::decrease(){
health--;
setPlainText(QString("Health: ") + QString::number(health)); // Health: 2
}

int Health::getHealth(){
return health;
}

Health.h:
#ifndef HEALTH
#define HEALTH

#include <QGraphicsTextItem>

class Health: public QGraphicsTextItem{
public:
Health(QGraphicsItem * parent=0);
void decrease();
int getHealth();
private:
int health;
};

#endif // HEALTH

Player.cpp:

#include "Player.h"
#include <QGraphicsScene>
#include <QKeyEvent>
#include "Bullet.h"
#include "Enemy.h"

Player::Player(QGraphicsItem *parent): QGraphicsRectItem(parent){

}

void Player::keyPressEvent(QKeyEvent *event){

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());
}

else if (event->key() == Qt::Key_Space){

Bullet * bullet = new Bullet();
bullet->setPos(x(),y());
scene()->addItem(bullet);
}
}

void Player::spawn(){

Enemy * enemy = new Enemy();
scene()->addItem(enemy);
}

Player.h:

#ifndef PLAYER
#define PLAYER

#include <QGraphicsRectItem>
#include <QObject>
#include <QGraphicsItem>

class Player:public QObject, public QGraphicsRectItem{
Q_OBJECT
public:
Player(QGraphicsItem * parent=0);
void keyPressEvent(QKeyEvent * event);
public slots:
void spawn();
};

#endif // PLAYER

Score.cpp:

#include "Score.h"
#include <QFont>

Score::Score(QGraphicsItem *parent): QGraphicsTextItem(parent){

score = 0;


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

void Score::increase(){
score++;
setPlainText(QString("Score: ") + QString::number(score)); // Score: 1
}

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

Score.h:

#ifndef SCORE
#define SCORE

#include <QGraphicsTextItem>

class Score: public QGraphicsTextItem{
public:
Score(QGraphicsItem * parent=0);
void increase();
int getScore();
private:
int score;
};

#endif // SCORE

main.cpp:

#include <QApplication>
#include "Game.h"


Game * game;

int main(int argc, char *argv[]){
QApplication a(argc, argv);

game = new Game();
game->show();

return a.exec();
}







     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.