Stefan Schuermans commited on 2019-06-10 18:25:59
Showing 2 changed files, with 72 additions and 0 deletions.
| ... | ... |
@@ -108,6 +108,10 @@ void Pong::redraw() |
| 108 | 108 |
/// callback when requested time reached |
| 109 | 109 |
void Pong::timeCall() |
| 110 | 110 |
{
|
| 111 |
+ // computer player: move pads |
|
| 112 |
+ computerLeft(); |
|
| 113 |
+ computerRight(); |
|
| 114 |
+ |
|
| 111 | 115 |
// bounce ball |
| 112 | 116 |
bounceBall(); |
| 113 | 117 |
|
| ... | ... |
@@ -122,6 +126,68 @@ void Pong::timeCall() |
| 122 | 126 |
planTimeCall(); |
| 123 | 127 |
} |
| 124 | 128 |
|
| 129 |
+/// computer player for left pad |
|
| 130 |
+void Pong::computerLeft() |
|
| 131 |
+{
|
|
| 132 |
+ // ball not moving towards pad: do not move |
|
| 133 |
+ if (m_ballDirX >= 0) {
|
|
| 134 |
+ return; |
|
| 135 |
+ } |
|
| 136 |
+ |
|
| 137 |
+ // compute expected ball position at pad |
|
| 138 |
+ int expectedY = m_ballPosY + (m_ballPosX - 1) * m_ballDirY; |
|
| 139 |
+ |
|
| 140 |
+ // compute pad position to hit ball with center of pad |
|
| 141 |
+ int padY = expectedY - m_padSize / 2; |
|
| 142 |
+ |
|
| 143 |
+ // do not move pad out of field |
|
| 144 |
+ if (padY < 0) {
|
|
| 145 |
+ padY = 0; |
|
| 146 |
+ } |
|
| 147 |
+ if (padY > m_height - m_padSize) {
|
|
| 148 |
+ padY = m_height - m_padSize; |
|
| 149 |
+ } |
|
| 150 |
+ |
|
| 151 |
+ // move pad |
|
| 152 |
+ if (m_leftPosY < padY) {
|
|
| 153 |
+ ++m_leftPosY; |
|
| 154 |
+ } |
|
| 155 |
+ else if (m_leftPosY > padY) {
|
|
| 156 |
+ --m_leftPosY; |
|
| 157 |
+ } |
|
| 158 |
+} |
|
| 159 |
+ |
|
| 160 |
+/// computer player for right pad |
|
| 161 |
+void Pong::computerRight() |
|
| 162 |
+{
|
|
| 163 |
+ // ball not moving towards pad: do not move |
|
| 164 |
+ if (m_ballDirX <= 0) {
|
|
| 165 |
+ return; |
|
| 166 |
+ } |
|
| 167 |
+ |
|
| 168 |
+ // compute expected ball position at pad |
|
| 169 |
+ int expectedY = m_ballPosY + (m_width - 2 - m_ballPosX) * m_ballDirY; |
|
| 170 |
+ |
|
| 171 |
+ // compute pad position to hit ball with center of pad |
|
| 172 |
+ int padY = expectedY - m_padSize / 2; |
|
| 173 |
+ |
|
| 174 |
+ // do not move pad out of field |
|
| 175 |
+ if (padY < 0) {
|
|
| 176 |
+ padY = 0; |
|
| 177 |
+ } |
|
| 178 |
+ if (padY > m_height - m_padSize) {
|
|
| 179 |
+ padY = m_height - m_padSize; |
|
| 180 |
+ } |
|
| 181 |
+ |
|
| 182 |
+ // move pad |
|
| 183 |
+ if (m_rightPosY < padY) {
|
|
| 184 |
+ ++m_rightPosY; |
|
| 185 |
+ } |
|
| 186 |
+ else if (m_rightPosY > padY) {
|
|
| 187 |
+ --m_rightPosY; |
|
| 188 |
+ } |
|
| 189 |
+} |
|
| 190 |
+ |
|
| 125 | 191 |
/// bounce ball |
| 126 | 192 |
void Pong::bounceBall() |
| 127 | 193 |
{
|
| ... | ... |
@@ -61,6 +61,12 @@ protected: |
| 61 | 61 |
/// callback when requested time reached |
| 62 | 62 |
virtual void timeCall(); |
| 63 | 63 |
|
| 64 |
+ /// computer player for left pad |
|
| 65 |
+ void computerLeft(); |
|
| 66 |
+ |
|
| 67 |
+ /// computer player for right pad |
|
| 68 |
+ void computerRight(); |
|
| 69 |
+ |
|
| 64 | 70 |
/// bounce ball |
| 65 | 71 |
void bounceBall(); |
| 66 | 72 |
|
| 67 | 73 |