-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.cpp
87 lines (70 loc) · 2.19 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "game.h"
Game::Game(QWidget *parent):QGraphicsView(parent)
{
//making the view or window
setFixedSize(1400,880);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
//making the game scene
gameScene = new QGraphicsScene(this);
gameScene->setSceneRect(0,0,1400,880);
QGraphicsPixmapItem *bg = new QGraphicsPixmapItem();
bg->setPixmap(QPixmap(":/images/bg.jpg").scaled(1400,880));
gameScene->addItem(bg);
//adding the gameScene to the view
setScene(gameScene);
score = new Score();
gameScene->addItem(score);
snake2 = NULL;
snake = NULL;
}
void Game::keyPressEvent(QKeyEvent *event)
{
if(snake)
snake->keyPressEvent(event);
else
QGraphicsView::keyPressEvent(event);
}
void Game::displayMainMenu(QString title,QString play)
{
//Create the title
titleText = new QGraphicsTextItem(title);
QFont titleFont("arial" , 50);
titleText->setFont( titleFont);
int xPos = width()/2 - titleText->boundingRect().width()/2;
int yPos = 150;
titleText->setPos(xPos,yPos);
gameScene->addItem(titleText);
//create Button
Button * playButton = new Button(play,titleText);
int pxPos = 160 ;
int pyPos = 160;
playButton->setPos(pxPos,pyPos);
connect(playButton,SIGNAL(clicked()) , this , SLOT(start()));
//gameScene->addItem(playButton);
//Create Quit Button
Button * quitButton = new Button("Quit",titleText);
int qxPos = 160;
int qyPos = 230;
quitButton->setPos(qxPos,qyPos);
connect(quitButton, SIGNAL(clicked()),this,SLOT(close()));
//gameScene->addItem(quitButton);
}
void Game::start(){
snake = new MoveSnake();
snake->setFlag(QGraphicsItem::ItemIsFocusable);
snake->setFocus();
score->setVisible(true);
score->setScore(0);
gameScene->addItem(snake);
gameScene->removeItem(titleText);
delete titleText;
if (snake2)
snake2->deleteLater();
snake2 = snake;
}
void Game::gameOver(){
displayMainMenu("Game Over!","Play Again");
gameScene->removeItem(snake);
//delete snake;
}