pong: make computer slow
Stefan Schuermans

Stefan Schuermans commited on 2019-06-15 14:37:52
Showing 2 changed files, with 38 additions and 1 deletions.

... ...
@@ -35,7 +35,7 @@ Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
35 35
   m_filePadColor(dirBase.getFile("padColor")),
36 36
   m_ballColor(), m_lineColor(), m_padColor(),
37 37
   m_ballPosX(-1), m_ballPosY(-1), m_ballDirX(0), m_ballDirY(0),
38
-  m_padSize(0), m_leftPosY(0), m_rightPosY(0)
38
+  m_padSize(0), m_leftPosY(0), m_rightPosY(0), m_leftDelay(0), m_rightDelay(0)
39 39
 {
40 40
   // FIXME: activate at begin for initial development only
41 41
   activate();
... ...
@@ -70,6 +70,8 @@ void Pong::reinitialize()
70 70
   m_padSize = (m_height + 1) / 3;
71 71
   m_leftPosY = (m_height - m_padSize) / 2;
72 72
   m_rightPosY = (m_height - m_padSize) / 2;
73
+  m_leftDelay = 0;
74
+  m_rightDelay = 0;
73 75
 
74 76
   // convert colors
75 77
   color2data(m_fileBallColor, m_ballColor);
... ...
@@ -126,6 +128,28 @@ void Pong::timeCall()
126 128
   planTimeCall();
127 129
 }
128 130
 
131
+/**
132
+ * @brief delay processing for computer players
133
+ * @param[in,out] delay delay variable of computer player
134
+ * @return whether computer player is allowed to move
135
+ */
136
+bool Pong::computerDelay(int &delay) const
137
+
138
+{
139
+  // zero delay: generate new delay
140
+  if (delay <= 0) {
141
+    int avg_steps = (m_height - m_padSize) / 2;
142
+    int delay_range = avg_steps > 1 ? m_width / avg_steps: m_width;
143
+    delay = rand() % delay_range + delay_range;
144
+  }
145
+
146
+  // count down delay
147
+  --delay;
148
+
149
+  // moving allowd if delay expired
150
+  return delay <= 0;
151
+}
152
+
129 153
 /**
130 154
  * @brief computation of ideal pad position for computer players
131 155
  * @param[in] padBallX x coordinate of position of ball when hitting the pad
... ...
@@ -199,18 +223,22 @@ void Pong::computerMovePad(int padYmin, int padYmax, int &padPosY) const
199 223
 /// computer player for left pad
200 224
 void Pong::computerLeft()
201 225
 {
226
+  if (computerDelay(m_leftDelay)) {
202 227
     int padYmin, padYmax;
203 228
     computerComputePadPos(1, m_leftPosY, padYmin, padYmax);
204 229
     computerMovePad(padYmin, padYmax, m_leftPosY);
205 230
   }
231
+}
206 232
 
207 233
 /// computer player for right pad
208 234
 void Pong::computerRight()
209 235
 {
236
+  if (computerDelay(m_rightDelay)) {
210 237
     int padYmin, padYmax;
211 238
     computerComputePadPos(m_width - 2, m_rightPosY, padYmin, padYmax);
212 239
     computerMovePad(padYmin, padYmax, m_rightPosY);
213 240
   }
241
+}
214 242
 
215 243
 /// bounce ball
216 244
 void Pong::bounceBall()
... ...
@@ -61,6 +61,13 @@ protected:
61 61
   /// callback when requested time reached
62 62
   virtual void timeCall();
63 63
 
64
+  /**
65
+   * @brief delay processing for computer players
66
+   * @param[in,out] delay delay variable of computer player
67
+   * @return whether computer player is allowed to move
68
+   */
69
+  bool computerDelay(int &delay) const;
70
+
64 71
   /**
65 72
    * @brief computation of ideal pad position for computer players
66 73
    * @param[in] padBallX x coordinate of position of ball when hitting the pad
... ...
@@ -120,6 +127,8 @@ protected:
120 127
   int       m_padSize;       ///< size of player pads
121 128
   int       m_leftPosY;      ///< position of top pixel of left pad
122 129
   int       m_rightPosY;     ///< position of top pixel of left pad
130
+  int       m_leftDelay;     ///< delay for coputer moving left pad
131
+  int       m_rightDelay;    ///< delay for coputer moving left pad
123 132
 }; // class Canvas
124 133
 
125 134
 } // namespace Blinker
126 135