pong: add pads
Stefan Schuermans

Stefan Schuermans commited on 2019-06-10 16:51:39
Showing 3 changed files, with 60 additions and 5 deletions.

... ...
@@ -32,8 +32,10 @@ Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase):
32 32
   Game(name, mgrs, dirBase),
33 33
   m_fileBallColor(dirBase.getFile("ballColor")),
34 34
   m_fileLineColor(dirBase.getFile("lineColor")),
35
-  m_ballColor(), m_lineColor(),
36
-  m_ballPosX(-1), m_ballPosY(-1), m_ballDirX(0), m_ballDirY(0)
35
+  m_filePadColor(dirBase.getFile("padColor")),
36
+  m_ballColor(), m_lineColor(), m_padColor(),
37
+  m_ballPosX(-1), m_ballPosY(-1), m_ballDirX(0), m_ballDirY(0),
38
+  m_padSize(0), m_leftPosY(0), m_rightPosY(0)
37 39
 {
38 40
   // FIXME: activate at begin for initial development only
39 41
   activate();
... ...
@@ -53,7 +55,8 @@ bool Pong::updateConfigGame()
53 55
 
54 56
   // color file was modified -> convert color, return true for update
55 57
   if (colorUpdate(m_fileBallColor, m_ballColor) ||
56
-      colorUpdate(m_fileLineColor, m_lineColor)) {
58
+      colorUpdate(m_fileLineColor, m_lineColor) ||
59
+      colorUpdate(m_filePadColor, m_padColor)) {
57 60
     ret = true;
58 61
   }
59 62
 
... ...
@@ -63,11 +66,15 @@ bool Pong::updateConfigGame()
63 66
 /// re-initialize game (e.g. due to config change)
64 67
 void Pong::reinitialize()
65 68
 {
66
-  // TODO
69
+  // compute parameters
70
+  m_padSize = (m_height + 1) / 3;
71
+  m_leftPosY = (m_height - m_padSize) / 2;
72
+  m_rightPosY = (m_height - m_padSize) / 2;
67 73
 
68 74
   // convert colors
69 75
   color2data(m_fileBallColor, m_ballColor);
70 76
   color2data(m_fileLineColor, m_lineColor);
77
+  color2data(m_filePadColor, m_padColor);
71 78
 
72 79
   // FIXME: start ball for development
73 80
   startBall();
... ...
@@ -87,6 +94,10 @@ void Pong::redraw()
87 94
     pixel(y, x, m_lineColor);
88 95
   }
89 96
 
97
+  // draw pads
98
+  lineVert(m_leftPosY, m_leftPosY + m_padSize - 1, 0, m_padColor);
99
+  lineVert(m_rightPosY, m_rightPosY + m_padSize - 1, m_width - 1, m_padColor);
100
+
90 101
   // draw ball
91 102
   pixel(m_ballPosY, m_ballPosX, m_ballColor);
92 103
 
... ...
@@ -97,7 +108,7 @@ void Pong::redraw()
97 108
 /// callback when requested time reached
98 109
 void Pong::timeCall()
99 110
 {
100
-  // bounce ball
111
+  // bounce ball at sides
101 112
   if (m_ballPosY <= 0 && m_ballDirY < 0) {
102 113
     m_ballDirY = 1;
103 114
   }
... ...
@@ -111,6 +122,44 @@ void Pong::timeCall()
111 122
     m_ballDirX = -1;
112 123
   }
113 124
 
125
+  // bounce ball at left player
126
+  if (m_ballPosX == 1 && m_ballDirX < 0) {
127
+    // top corner
128
+    if (m_ballPosY == m_leftPosY - 1 && m_ballDirY > 0) {
129
+      m_ballDirX = 1;
130
+      m_ballDirY = -1;
131
+    }
132
+    // bottom corner
133
+    else if (m_ballPosY == m_leftPosY + m_padSize && m_ballDirY < 0) {
134
+      m_ballDirX = 1;
135
+      m_ballDirY = 1;
136
+    }
137
+    // pad edge
138
+    else if (m_ballPosY >= m_leftPosY &&
139
+             m_ballPosY < m_leftPosY + m_padSize) {
140
+      m_ballDirX = 1;
141
+    }
142
+  }
143
+
144
+  // bounce ball at right player
145
+  if (m_ballPosX == m_width - 2 && m_ballDirX > 0) {
146
+    // top corner
147
+    if (m_ballPosY == m_rightPosY - 1 && m_ballDirY > 0) {
148
+      m_ballDirX = -1;
149
+      m_ballDirY = -1;
150
+    }
151
+    // bottom corner
152
+    else if (m_ballPosY == m_rightPosY + m_padSize && m_ballDirY < 0) {
153
+      m_ballDirX = -1;
154
+      m_ballDirY = 1;
155
+    }
156
+    // pad edge
157
+    else if (m_ballPosY >= m_rightPosY &&
158
+             m_ballPosY < m_rightPosY + m_padSize) {
159
+      m_ballDirX = -1;
160
+    }
161
+  }
162
+
114 163
   // move ball
115 164
   m_ballPosX += m_ballDirX;
116 165
   m_ballPosY += m_ballDirY;
... ...
@@ -70,12 +70,17 @@ protected:
70 70
 protected:
71 71
   ColorFile m_fileBallColor; ///< color file for ball color
72 72
   ColorFile m_fileLineColor; ///< color file for center line
73
+  ColorFile m_filePadColor;  ///< color file for player pad
73 74
   ColorData m_ballColor;     ///< ball color
74 75
   ColorData m_lineColor;     ///< center line color
76
+  ColorData m_padColor;      ///< player pad color
75 77
   int       m_ballPosX;      ///< ball position X
76 78
   int       m_ballPosY;      ///< ball position Y
77 79
   int       m_ballDirX;      ///< ball direction X
78 80
   int       m_ballDirY;      ///< ball direction Y
81
+  int       m_padSize;       ///< size of player pads
82
+  int       m_leftPosY;      ///< position of top pixel of left pad
83
+  int       m_rightPosY;     ///< position of top pixel of left pad
79 84
 }; // class Canvas
80 85
 
81 86
 } // namespace Blinker
82 87