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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#include "mainwidget.h"
#include "actor.h"
#include "block.h"
#include "bonuspoint.h"
#include "constants.h"
MainWidget::MainWidget()
: currentKey(0)
{
setFocusPolicy(Qt::StrongFocus);
QVBoxLayout *layout = new QVBoxLayout(this);
QHBoxLayout *m_scoreLayout = new QHBoxLayout();
for (unsigned int i=1; i<4; ++i) {
QGroupBox *scoreBoxI = new QGroupBox(QString("Spieler %1").arg(i), this);
m_scoreLayout->addWidget(scoreBoxI);
QGridLayout *playerLayout = new QGridLayout();
scoreBoxI->setLayout(playerLayout);
playerLayout->addWidget( new QLabel("Rundenpunkte:", this), 0, 0);
playerLayout->addWidget( new QLabel("Gesamtpunkte:", this), 1, 0);
playerLayout->addWidget( new QLabel("", this), 0, 1);
playerLayout->addWidget( new QLabel("", this), 1, 1);
m_playerScoreLayouts.append(playerLayout);
}
m_scene = new QGraphicsScene(0, 0, 500, 500, this);
m_scene->setBackgroundBrush(Qt::black);
QGraphicsView *window = new QGraphicsView(m_scene, this);
window->setFrameStyle(0);
window->setAlignment(Qt::AlignLeft | Qt::AlignTop);
window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
window->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
layout->addLayout(m_scoreLayout);
layout->addWidget(window);
setLayout(layout);
setWindowTitle("pacman client");
loadDummyMap();
}
void MainWidget::updateScore()
{
QMapIterator<Color, Actor*> i(m_actors);
while (i.hasNext()) {
i.next();
int id = i.key() - 1;
if (id == 4) {
id = 3;
}
QLabel *turnPointsLbl =
dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget());
QLabel *allPointsLbl =
dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget());
turnPointsLbl->setText(QString::number(id * 100 * qrand()));
allPointsLbl->setText(QString::number(id * 200 * qrand()));
}
}
// temporary
transmission::map_t createDummyMap()
{
transmission::map_t map;
map = new transmission::field_t*[map_size[0]];
for (unsigned int i = 0; i < map_size[0]; ++i)
map[i] = new transmission::field_t[map_size[1]];
for (unsigned int x = 0; x < map_size[0]; ++x)
{
for (unsigned int y = 0; y < map_size[1]; ++y)
{
transmission::field_t &cur = map[x][y];
cur = 0;
}
}
map[4][3] |= green;
map[4][3] |= transmission::block;
map[5][3] |= noColor;
map[5][3] |= transmission::block;
map[6][3] |= noColor;
map[6][3] |= transmission::block;
map[7][3] |= red;
map[7][3] |= transmission::block;
map[7][5] |= transmission::bonuspoint;
map[5][5] |= blue;
map[5][5] |= transmission::pacman;
map[5][5] |= transmission::direction_left;
return map;
}
void MainWidget::loadDummyMap()
{
transmission::map_t map = createDummyMap();
for (unsigned int x = 0; x < map_size[0]; ++x)
{
for (unsigned int y = 0; y < map_size[1]; ++y)
{
const transmission::field_t &cur = map[x][y];
if (cur == 0)
continue;
qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur;
Color color = static_cast<Color>(cur & transmission::color_mask);
qDebug() << "col=" << color;
PixmapItem *item = 0;
if (cur & transmission::block)
item = new Block(color);
else if (cur & transmission::bonuspoint)
item = new BonusPoint();
else if (cur & transmission::pacman)
{
Actor *actor = m_actors.value(color, 0);
if (actor == 0) { // 0 entspricht NULL ;)
qDebug() << "new actor of col" << color;
actor = new Actor(color);
m_actors[color] = actor;
m_scene->addItem(actor);
actor->setPos(mapPositionToCoord(x, y));
}
Actor::Movement direction;
switch (cur & transmission::direction_mask)
{
case transmission::direction_none:
direction = Actor::None;
break;
case transmission::direction_left:
direction = Actor::Left;
break;
case transmission::direction_right:
direction = Actor::Right;
break;
case transmission::direction_up:
direction = Actor::Up;
break;
case transmission::direction_down:
direction = Actor::Down;
break;
default:
Q_ASSERT(false);
}
//actor->move(direction, mapPositionToCoord(x, y);
}
else
Q_ASSERT(false);
if(item != 0)
{
m_scene->addItem(item);
item->setPos(mapPositionToCoord(x, y));
}
}
}
updateScore();
}
QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y)
{
return QPoint(x * field_size[0], y * field_size[1]);
}
transmission::field_t MainWidget::translateKey(int key)
{
switch(key) {
case Qt::Key_Up:
return transmission::direction_up;
break;
case Qt::Key_Down:
return transmission::direction_down;
break;
case Qt::Key_Left:
return transmission::direction_left;
break;
case Qt::Key_Right:
return transmission::direction_right;
break;
default:
return 0;
}
}
void MainWidget::keyPressEvent(QKeyEvent* event)
{
QWidget::keyPressEvent(event);
currentKey = translateKey( event->key() );
}
void MainWidget::keyReleaseEvent(QKeyEvent* event)
{
QWidget::keyReleaseEvent(event);
transmission::field_t releasedKey = translateKey(event->key());
if (releasedKey == currentKey) {
// current key got released
// if this is false, a key got released which has already
// been replaced by a different key, so this case is disregarded
currentKey = 0;
}
}
|