This repository was archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfruit.cpp
More file actions
81 lines (56 loc) · 1.2 KB
/
fruit.cpp
File metadata and controls
81 lines (56 loc) · 1.2 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
/*
* File fruit.cpp
* created on 2-8-2018
* by smit
*
* <smit17av@gmail.com>
*/
#include "fruit.h"
#include <block.h>
#include "snake.h"
#include <cstdio>
#include <ctime>
Fruit::Fruit(QWidget *parent,QSize ParentSize,Snake *snake):
QFrame(parent)
{
this->snake = snake;
this->ParentSize = ParentSize;
QString style =
"QFrame{"
" border-image:url(\":/img/apple.png\");"
"};";
setStyleSheet(style);
setFixedSize(20,20);
move(100,200);
}
void Fruit::Eaten()
{
bool Maximized ;
QPoint point;
sos:
Maximized = parentWidget()->windowState().testFlag(Qt::WindowMaximized);
if(Maximized)
{
point.rx() = RandXY(100,ParentSize.width()-100);
point.ry() = RandXY(100,ParentSize.height())-100;
}
else
{
point.rx() = RandXY(100,parentWidget()->geometry().width());
point.ry() = RandXY(100,parentWidget()->geometry().height());
}
for(auto block :snake->blocks)
{
if(
(point.x() <= block->pos().x()+100 && point.x() >= block->pos().x()-100) &&
(point.y() <= block->pos().y()+100 && point.y() >= block->pos().y()-100)
)
goto sos;
}
move(point);
}
int Fruit::RandXY(int min,int max)
{
srand(time(nullptr));
return min + (rand() % (max - min +1 ) );
}