summaryrefslogtreecommitdiffstats
path: root/ktron/tron.h
blob: 821beddb6037349f4cb82fc41ac4e59a57dd1309 (plain)
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
/* ********************************************************************************
  This file is part of the game 'KTron'

  Copyright (C) 1998-2000 by Matthias Kiefer <[email protected]>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  *******************************************************************************/  

#ifndef TRON_H
#define TRON_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <tqwidget.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <math.h>
#include <krandomsequence.h>

class KActionCollection;

#include "player.h"

enum Player{One,Two,Both,Nobody};
// Bits that defines the rect and which sides to draw
enum {BACKGROUND=0, PLAYER1=1,PLAYER2=2,TOP=4,BOTTOM=8,LEFT=16,RIGHT=32};

/**
* @short The playingfield
*/
class Tron : public TQWidget
{
  Q_OBJECT
  

public:
  Tron(TQWidget *parent=0, const char *name=0);
  ~Tron();
  void setActionCollection(KActionCollection*);
  void updatePixmap();
  void setBackgroundPix(TQPixmap);
  void setComputerplayer(Player player, bool);
  bool isComputer(Player player);
  void setVelocity(int);
  void setRectSize(int newSize);

public slots:
  /** Starts a new game. The difference to reset is, that the players
  * points are set to zero. Emits gameEnds(Nobody).
  */
  void newGame();
  void togglePause();

  /**
  * checks if both players are computer and starts the game
  */
  void computerStart();

  void loadSettings();

signals:
  void gameEnds(Player loser);
  void gameReset();

protected:
  /** bitBlt�s the rect that has to be updated from the
  * bufferpixmap on the screen and writes eventually text
  */
  void paintEvent(TQPaintEvent *);
  /** resets game and creates a new playingfield */
  void resizeEvent(TQResizeEvent *);
  void keyPressEvent(TQKeyEvent *);
  void keyReleaseEvent(TQKeyEvent *);
  /** pauses game */
  void focusOutEvent(TQFocusEvent *);

private:
  /** Stores key shortcuts */
  KActionCollection* actionCollection;
  /** Drawing buffer */
  TQPixmap *pixmap;
  /** The playingfield */
  TQMemArray<int> *playfield;
  /** game status flag */
  bool gamePaused;
  /** game status flag */
  bool gameEnded;
  /**  used for waiting after game ended */
  bool gameBlocked;
  /** flag, if a string should be drawn, how to start the game */
  bool beginHint;
  /** Height  of the playingfield in number of rects*/
  int fieldHeight;
  /** Width of the playingfield in number of rects*/
  int fieldWidth;
  TQTimer *timer;
  player players[2];

  /** Backgroundpixmap **/
  TQPixmap bgPix;

  /** time in ms between two moves */
  int velocity;
  /** size of the rects */
  int rectSize;

  /** The random sequence generator **/
  KRandomSequence random;

  // Options
  /** determines level of computerplayer */
  int lookForward;

  // Funktionen
  /** resets the game */
  void reset();
  /** starts the game timer */
  void startGame();
  /** stops the game timer */
  void stopGame();
  /** creates a new playfield and a bufferpixmap */
  void createNewPlayfield();
  /** paints players at current player coordinates */
  void paintPlayers();
  /** draws a rect in current TronStyle at position x,y of the playingfield */
  void drawRect(TQPainter & p, int x, int y);
  /** emits gameEnds(Player) and displays the winner by changing color*/
  void showWinner(Player winner);

  /** calculates if player playerNr would crash
  * if he moves xInc in x-direction and yInc in y-direction
  */
  bool crashed(int playerNr,int xInc, int yInc) const;
  /** calculates if player playerNr should change direction */
  void think(int playerNr);
  void changeDirection(int playerNr,int dis_right,int dis_left);

  /** sets the direction of player playerNr to newDirection */
  void switchDir(int playerNr,Directions::Direction newDirection);
  /**
  * updates the the rect at current position to not draw a
  * border in the direction of the next step.
  * (only used in mode OLine)
  *
  * -1 means update both players
  */
  void updateDirections(int playerNr=-1);
  
private slots:
    /**
    * This is the main function of KTron.
    * It checkes if an accelerator is pressed and than moves this player
    * forward. Then it checkes if a crash occurred.
    * If no crash happen it moves both players forward and checks again
    * if someone crashed.
    */
    void doMove();
    void unblockGame();
    void showBeginHint();
};


#endif // TRON_H