This repository was archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
151 lines (114 loc) · 3.79 KB
/
Copy pathmainwindow.cpp
File metadata and controls
151 lines (114 loc) · 3.79 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "time.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
srand(time(NULL));
playerScore = 0;
enemyScore = 0;
centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);
p = parent;
setFixedSize(800,600);
move(QApplication::desktop()->screen()->rect().center()-this->rect().center());
setWindowFlags(Qt::Window | Qt::MSWindowsFixedSizeDialogHint);
timer = new QTimer(this);
myBall = new ball(this);
player = new paddle(this,true);
enemy = new paddle(this,false);
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(repaint()));
QObject::connect(timer,&QTimer::timeout,myBall,&ball::moveBall);
QObject::connect(timer,&QTimer::timeout,this,&MainWindow::colisionDetection);
QObject::connect(myBall,&ball::ballMoves,enemy,&paddle::AIProce);
QObject::connect(this,&MainWindow::draw,myBall,&ball::drawBall);
QObject::connect(this,&MainWindow::draw,player,&paddle::drawPaddle);
QObject::connect(this,&MainWindow::draw,enemy,&paddle::drawPaddle);
QObject::connect(this,&MainWindow::reportPos,player,&paddle::movePaddle);
QObject::connect(this,&MainWindow::bounce,myBall,&ball::bounce);
QObject::connect(myBall,&ball::scored,this,&MainWindow::scored);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resetGame()
{
myBall->resetBall();
player->paddleReset();
enemy->paddleReset();
playerScore = 0;
enemyScore = 0;
timer->stop();
running = false;
this->update();
}
void MainWindow::VictoryCheck()
{
if(playerScore >= maxScore){
QMessageBox::information(this,"Victory","You Won!!");
resetGame();
}
else if(enemyScore >= maxScore){
QMessageBox::information(this,"Defeat","You lost... Better luck next time!");
resetGame();
}
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton && running == false){
timer->start(16);
running = true;
}
else if (e->button() == Qt::LeftButton && running == true){
timer->stop();
running = false;
}
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPen pen(Qt::white,3,Qt::DashLine);
painter.fillRect(rect(), QBrush(QColor(Qt::black)));
painter.setPen(pen);
painter.drawLine(405,0,405,600);
painter.setPen(Qt::white);
painter.setBrush(QBrush(QColor(Qt::white)));
QString pl = "Palyer: "+QString::number(playerScore);
QString ai = "Computer: "+QString::number(enemyScore);
painter.drawText(185,50,pl);
painter.drawText(555,50,ai);
emit draw(painter);
if(firstTime){
QMessageBox::information(this,"Welcome!","One click to start or pause.\n Double click to reset.\n First with 10 points wins!!");
firstTime = false;
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
QPoint point = e->pos();
emit reportPos(point);
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *)
{
resetGame();
}
void MainWindow::scored(bool who)
{
if(who){
enemyScore++;
myBall->resetBall();
}
else{
playerScore++;
myBall->resetBall();
}
VictoryCheck();
}
void MainWindow::colisionDetection()
{
if(myBall->shape().x() < player->shape().x()+player->shape().width() && myBall->shape().y()+myBall->shape().height() > player->shape().y() && myBall->shape().y() < player->shape().y()+player->shape().height())emit bounce(rand()%2);
if(myBall->shape().x()+myBall->shape().width() > enemy->shape().x() && myBall->shape().y()+myBall->shape().height() > enemy->shape().y() && myBall->shape().y() < enemy->shape().y()+enemy->shape().height())emit bounce(rand()%2);
}