1 | /********************************************************************************** |
2 | This file is part of the game 'KTron' |
3 | |
4 | Copyright (C) 1998-2000 by Matthias Kiefer <matthias.kiefer@gmx.de> |
5 | Copyright (C) 2005 Benjamin C. Meyer <ben at meyerhome dot net> |
6 | Copyright (C) 2008-2009 Stas Verberkt <legolas at legolasweb dot nl> |
7 | |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | |
22 | *******************************************************************************/ |
23 | |
24 | #ifndef TRON_H |
25 | #define TRON_H |
26 | |
27 | #include "player.h" |
28 | #include "item.h" |
29 | #include "playfield.h" |
30 | #include "intelligence.h" |
31 | |
32 | #include <QWidget> |
33 | #include <QPixmap> |
34 | #include <QResizeEvent> |
35 | #include <QPaintEvent> |
36 | #include <QFocusEvent> |
37 | |
38 | #define WINNING_DIFF 5 |
39 | |
40 | namespace KBAction |
41 | { |
42 | enum Action { |
43 | NONE, |
44 | UP, |
45 | DOWN, |
46 | LEFT, |
47 | RIGHT, |
48 | ACCELERATE |
49 | }; |
50 | } |
51 | |
52 | /** |
53 | * @short The playingfield |
54 | */ |
55 | class Tron : public QWidget |
56 | { |
57 | Q_OBJECT |
58 | |
59 | public: |
60 | Tron(QWidget *parent=0); |
61 | ~Tron(); |
62 | void updatePixmap(); |
63 | void setVelocity(int); |
64 | void setRectSize(int newSize); |
65 | void triggerKey(int, KBAction::Action, bool); |
66 | bool running(); |
67 | bool paused(); |
68 | PlayField *getPlayField(); |
69 | Player *getPlayer(int playerNr); |
70 | bool hasWinner(); |
71 | int getWinner(); |
72 | |
73 | public slots: |
74 | /** Starts a new game. The difference to reset is, that the players |
75 | * points are set to zero. Emits gameEnds(Nobody). |
76 | */ |
77 | void newGame(); |
78 | void togglePause(); |
79 | void loadSettings(); |
80 | void itemHit(int playerNumber, int x, int y); |
81 | |
82 | signals: |
83 | void gameEnds(); |
84 | void updatedScore(); |
85 | void gameReset(); |
86 | void pauseBlocked(bool block); |
87 | |
88 | protected: |
89 | /** Calls renderer */ |
90 | void paintEvent(QPaintEvent *); |
91 | /** resets game and creates a new playingfield */ |
92 | void resizeEvent(QResizeEvent *); |
93 | /** pauses game */ |
94 | void focusOutEvent(QFocusEvent *); |
95 | |
96 | private: |
97 | /** The playingfield */ |
98 | PlayField pf; |
99 | /** The players */ |
100 | Player *players[2]; |
101 | /** game status flag */ |
102 | bool gamePaused; |
103 | /** game status flag */ |
104 | bool gameEnded; |
105 | /** used for waiting after game ended */ |
106 | bool gameBlocked; |
107 | QTimer *timer; |
108 | Item apple; |
109 | /** Intelligence for computer */ |
110 | Intelligence intelligence; |
111 | /** Backgroundpixmap **/ |
112 | QPixmap bgPix; |
113 | /** time in ms between two moves */ |
114 | int velocity; |
115 | /** size of the rects */ |
116 | int blockHeight; |
117 | int blockWidth; |
118 | /** counter for the number of moves, modulo 20 */ |
119 | int modMoves; |
120 | |
121 | private: |
122 | // Functions |
123 | /** resets the game */ |
124 | void reset(); |
125 | /** starts the game timer */ |
126 | void startGame(); |
127 | /** stops the game timer */ |
128 | void stopGame(); |
129 | /** creates a new playfield and a bufferpixmap */ |
130 | void createNewPlayfield(); |
131 | /** paints players at current player coordinates */ |
132 | void paintPlayers(); |
133 | /** emits gameEnds(Player) */ |
134 | void showWinner(); |
135 | /** retrieves the line speed */ |
136 | int lineSpeed(); |
137 | /** resizes the visual board */ |
138 | void resizeRenderer(); |
139 | /** generates new apple */ |
140 | void newApple(); |
141 | // Key handling / movement |
142 | void switchKeyOn(int, KBAction::Action); |
143 | void switchKeyOff(int, KBAction::Action); |
144 | /** Check head to head collissions */ |
145 | void checkHeadToHeadCollission(); |
146 | /** Helper for the doMove() function */ |
147 | void movementHelper(bool onlyAcceleratedPlayers); |
148 | /** Tries to generate a new obstacle */ |
149 | void newObstacle(); |
150 | |
151 | private slots: |
152 | /** |
153 | * This is the main function of KTron. |
154 | * It checkes if an accelerator is pressed and than moves this player |
155 | * forward. Then it checkes if a crash occurred. |
156 | * If no crash happen it moves both players forward and checks again |
157 | * if someone crashed. |
158 | */ |
159 | void doMove(); |
160 | void unblockGame(); |
161 | }; |
162 | |
163 | |
164 | #endif // TRON_H |
165 | |
166 | |