File indexing completed on 2024-04-28 04:04:39
0001 /* 0002 This file is part of the game 'KTron' 0003 0004 SPDX-FileCopyrightText: 1998-2000 Matthias Kiefer <matthias.kiefer@gmx.de> 0005 SPDX-FileCopyrightText: 2005 Benjamin C. Meyer <ben at meyerhome dot net> 0006 SPDX-FileCopyrightText: 2008-2009 Stas Verberkt <legolas at legolasweb dot nl> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 0010 */ 0011 0012 #ifndef TRON_H 0013 #define TRON_H 0014 0015 #include "player.h" 0016 #include "item.h" 0017 #include "playfield.h" 0018 #include "intelligence.h" 0019 0020 #include <QWidget> 0021 #include <QPixmap> 0022 #include <QResizeEvent> 0023 #include <QPaintEvent> 0024 #include <QFocusEvent> 0025 0026 0027 namespace KBAction 0028 { 0029 enum Action { 0030 NONE, 0031 UP, 0032 DOWN, 0033 LEFT, 0034 RIGHT, 0035 ACCELERATE 0036 }; 0037 } 0038 0039 /** 0040 * @short The playingfield 0041 */ 0042 class Tron : public QWidget 0043 { 0044 Q_OBJECT 0045 0046 public: 0047 explicit Tron(QWidget *parent=nullptr); 0048 ~Tron() override; 0049 void updatePixmap(); 0050 void setVelocity(int); 0051 void setRectSize(int newSize); 0052 void triggerKey(int, KBAction::Action, bool); 0053 bool running(); 0054 bool paused(); 0055 PlayField *getPlayField(); 0056 Player *getPlayer(int playerNr); 0057 bool hasWinner(); 0058 int getWinner(); 0059 0060 public Q_SLOTS: 0061 /** Starts a new game. The difference to reset is, that the players 0062 * points are set to zero. Emits gameEnds(Nobody). 0063 */ 0064 void newGame(); 0065 void togglePause(); 0066 void loadSettings(); 0067 void itemHit(int playerNumber, int x, int y); 0068 0069 Q_SIGNALS: 0070 void gameEnds(); 0071 void updatedScore(); 0072 void gameReset(); 0073 void pauseBlocked(bool block); 0074 0075 protected: 0076 /** Calls renderer */ 0077 void paintEvent(QPaintEvent *) override; 0078 /** resets game and creates a new playingfield */ 0079 void resizeEvent(QResizeEvent *) override; 0080 /** pauses game */ 0081 void focusOutEvent(QFocusEvent *) override; 0082 0083 private: 0084 /** The playingfield */ 0085 PlayField pf; 0086 /** The players */ 0087 Player *players[2]; 0088 /** game status flag */ 0089 bool gamePaused; 0090 /** game status flag */ 0091 bool gameEnded; 0092 /** used for waiting after game ended */ 0093 bool gameBlocked; 0094 QTimer *timer; 0095 Item apple; 0096 /** Intelligence for computer */ 0097 Intelligence intelligence; 0098 /** Backgroundpixmap **/ 0099 QPixmap bgPix; 0100 /** time in ms between two moves */ 0101 int velocity; 0102 /** size of the rects */ 0103 int blockHeight; 0104 int blockWidth; 0105 /** counter for the number of moves, modulo 20 */ 0106 int modMoves; 0107 0108 private: 0109 // Functions 0110 /** resets the game */ 0111 void reset(); 0112 /** starts the game timer */ 0113 void startGame(); 0114 /** stops the game timer */ 0115 void stopGame(); 0116 /** creates a new playfield and a bufferpixmap */ 0117 void createNewPlayfield(); 0118 /** paints players at current player coordinates */ 0119 void paintPlayers(); 0120 /** emits gameEnds(Player) */ 0121 void showWinner(); 0122 /** retrieves the line speed */ 0123 int lineSpeed(); 0124 /** resizes the visual board */ 0125 void resizeRenderer(); 0126 /** generates new apple */ 0127 void newApple(); 0128 // Key handling / movement 0129 void switchKeyOn(int, KBAction::Action); 0130 void switchKeyOff(int, KBAction::Action); 0131 /** Check head to head collisions */ 0132 void checkHeadToHeadCollision(); 0133 /** Helper for the doMove() function */ 0134 void movementHelper(bool onlyAcceleratedPlayers); 0135 /** Tries to generate a new obstacle */ 0136 void newObstacle(); 0137 0138 private Q_SLOTS: 0139 /** 0140 * This is the main function of KTron. 0141 * It checks if an accelerator is pressed and than moves this player 0142 * forward. Then it checks if a crash occurred. 0143 * If no crash happen it moves both players forward and checks again 0144 * if someone crashed. 0145 */ 0146 void doMove(); 0147 void unblockGame(); 0148 void resetOnThemeChange(); 0149 }; 0150 0151 0152 #endif // TRON_H 0153