#ifndef GAME_H
#define GAME_H
#include "player.h"
#include "button.h"
#include "sea.h"
#include "land.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
#include <QWidget>
#include <QKeyEvent>
Q_OBJECT
public:
// this class stuff
void menu();
void createMap();
void createPlayer();
// other class stuff
Player *player;
Button *startButton;
Button *quitButton;
Land *land;
Sea *sea;
// static stuff
int curr_pos_in_menu;
void changeColorOfButton();
public slots:
void start();
};
#endif // GAME_H
#ifndef GAME_H
#define GAME_H
#include "player.h"
#include "button.h"
#include "sea.h"
#include "land.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>
#include <QWidget>
#include <QKeyEvent>
class Game: public QGraphicsView{
Q_OBJECT
public:
// this class stuff
static QString in_room;
Game(QWidget *parent=NULL);
QGraphicsScene *scene;
void menu();
void createMap();
void createPlayer();
QPixmap darkenImage(QString image, float num);
// other class stuff
Player *player;
Button *startButton;
Button *quitButton;
Land *land;
Sea *sea;
// static stuff
int curr_pos_in_menu;
void changeColorOfButton();
public slots:
void start();
void mousePressEvent(QMouseEvent *event);
};
#endif // GAME_H
To copy to clipboard, switch view to plain text mode
#include "game.h"
#include "player.h"
#include "button.h"
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QDebug>
#include <QApplication>
#include <QTimer>
#include <QFont>
#include <QMessageBox>
#include <fstream>
#include <cmath>
#include <QRect>
#include <QDesktopWidget>
#include <QtMath>
{
scene->setSceneRect(0,0,350,300);
setFixedSize(350,300);
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
show();
menu();
}
void Game::menu()
{
title->setPos(10,30);
title
->setFont
(QFont("comic sans",
40,
70));
scene->addItem(title);
qDebug() << width();
startButton = new Button("START", 200, 50);
startButton->setPos(width()/2-startButton->rect().width()/2,125);
connect(startButton,SIGNAL(clicked()),this,SLOT(start()));
scene->addItem(startButton);
quitButton = new Button("QUIT", 200, 50);
quitButton->setPos(width()/2-quitButton->rect().width()/2,200);
connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
scene->addItem(quitButton);
}
void Game::createMap()
{
land = new Land;
land->setPos(0,0);
scene->addItem(land);
sea = new Sea;
sea->setPos(0,0);
scene->addItem(sea);
}
void Game::createPlayer()
{
player = new Player;
player->setFocus();
centerOn(player);
scene->addItem(player);
}
{
for(int i=0;i<tmp.height();i++)
{
for(int k=0;k<tmp.width();k++)
{
QColor color
( tmp.
pixel( k, i
) );
// changed this from pixelColor() to ensure alpha is copied if ( tmp.pixel(k,i) != 0) // modify only the pixels with non-zero alpha
{
color.setRgb(color.red()*num,color.blue()*num,color.green()*num);
tmp.setPixelColor(k,i,color);
}
}
}
// Now, convert the image to a pixmap and set it on the graphics object
return t;
}
void Game::start()
{
scene->clear();
in_room = "world";
// semi-transparent background
/*
QRect rec = QApplication::desktop()->screenGeometry();
int height = rec.height();
int width = rec.width();
*/
setSceneRect(0,0,7097,7298);
setFixedSize(1000,800);
//setBackgroundBrush(QBrush(QImage(":/Images/Grass.png")));
//showFullScreen();
createMap();
createPlayer();
}
{
if(event->buttons() == Qt::RightButton && in_room!="menu"){
QLineF ln
(event
->pos
(),player
->pos
());
int angle = -1 * ln.angle() + 460;
int tmp = angle + 80;
if(tmp>360)
tmp-=360;
if(angle > 360)
angle += -360;
player->setPlayerRotation(angle,tmp);
player->setDestination(event->pos());
}
}
#include "game.h"
#include "player.h"
#include "button.h"
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QDebug>
#include <QApplication>
#include <QTimer>
#include <QFont>
#include <QMessageBox>
#include <fstream>
#include <cmath>
#include <QRect>
#include <QDesktopWidget>
#include <QtMath>
QString Game::in_room = "menu";
Game::Game(QWidget *parent)
{
scene = new QGraphicsScene();
scene->setSceneRect(0,0,350,300);
setFixedSize(350,300);
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setBackgroundBrush(QBrush(QImage("red")));
show();
menu();
}
void Game::menu()
{
QGraphicsTextItem *title = new QGraphicsTextItem("NIGHTLESS");
title->setPos(10,30);
title->setFont(QFont("comic sans",40,70));
scene->addItem(title);
qDebug() << width();
startButton = new Button("START", 200, 50);
startButton->setPos(width()/2-startButton->rect().width()/2,125);
connect(startButton,SIGNAL(clicked()),this,SLOT(start()));
scene->addItem(startButton);
quitButton = new Button("QUIT", 200, 50);
quitButton->setPos(width()/2-quitButton->rect().width()/2,200);
connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
scene->addItem(quitButton);
}
void Game::createMap()
{
land = new Land;
land->setPos(0,0);
scene->addItem(land);
sea = new Sea;
sea->setPos(0,0);
scene->addItem(sea);
}
void Game::createPlayer()
{
player = new Player;
player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus();
centerOn(player);
scene->addItem(player);
}
QPixmap Game::darkenImage(QString file_name, float num)
{
QImage tmp( file_name );
for(int i=0;i<tmp.height();i++)
{
for(int k=0;k<tmp.width();k++)
{
QColor color( tmp.pixel( k, i ) ); // changed this from pixelColor() to ensure alpha is copied
if ( tmp.pixel(k,i) != 0) // modify only the pixels with non-zero alpha
{
color.setRgb(color.red()*num,color.blue()*num,color.green()*num);
tmp.setPixelColor(k,i,color);
}
}
}
// Now, convert the image to a pixmap and set it on the graphics object
QPixmap t = QPixmap::fromImage( tmp );
return t;
}
void Game::start()
{
scene->clear();
in_room = "world";
// semi-transparent background
/*
QRect rec = QApplication::desktop()->screenGeometry();
int height = rec.height();
int width = rec.width();
*/
setSceneRect(0,0,7097,7298);
setFixedSize(1000,800);
//setBackgroundBrush(QBrush(QImage(":/Images/Grass.png")));
//showFullScreen();
createMap();
createPlayer();
}
void Game::mousePressEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::RightButton && in_room!="menu"){
QLineF ln(event->pos(),player->pos());
int angle = -1 * ln.angle() + 460;
int tmp = angle + 80;
if(tmp>360)
tmp-=360;
if(angle > 360)
angle += -360;
player->setPlayerRotation(angle,tmp);
QPointF t(event->pos());
player->setDestination(event->pos());
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks