| 67 | |
| 68 | |
| 69 | class Puck: |
| 70 | def __init__(self, disp, pad, field): |
| 71 | self.vmax = .2 |
| 72 | self.disp = disp |
| 73 | self.field = field |
| 74 | self._reset(pad) |
| 75 | |
| 76 | def _reset(self, pad): |
| 77 | self.x = pad.x + pad.xoffset |
| 78 | if pad.y < 0: |
| 79 | self.y = pad.y + pad.yoffset |
| 80 | else: |
| 81 | self.y = pad.y - pad.yoffset |
| 82 | self.vx = pad.x - self.x |
| 83 | self.vy = pad.y + pad.w/2 - self.y |
| 84 | self._speedlimit() |
| 85 | self._slower() |
| 86 | self._slower() |
| 87 | |
| 88 | def update(self, pads): |
| 89 | self.x += self.vx |
| 90 | self.y += self.vy |
| 91 | for pad in pads: |
| 92 | if pad.contains(self): |
| 93 | self.vx *= 1.2 * pad.signx |
| 94 | self.vy *= 1.2 * pad.signy |
| 95 | fudge = .001 |
| 96 | # probably cleaner with something like... |
| 97 | if self.x < fudge: |
| 98 | pads[1].score += 1 |
| 99 | self._reset(pads[0]) |
| 100 | return True |
| 101 | if self.x > 7 - fudge: |
| 102 | pads[0].score += 1 |
| 103 | self._reset(pads[1]) |
| 104 | return True |
| 105 | if self.y < -1 + fudge or self.y > 1 - fudge: |
| 106 | self.vy *= -1.0 |
| 107 | # add some randomness, just to make it interesting |
| 108 | self.vy -= (randn()/300.0 + 1/300.0) * np.sign(self.vy) |
| 109 | self._speedlimit() |
| 110 | return False |
| 111 | |
| 112 | def _slower(self): |
| 113 | self.vx /= 5.0 |
| 114 | self.vy /= 5.0 |
| 115 | |
| 116 | def _faster(self): |
| 117 | self.vx *= 5.0 |
| 118 | self.vy *= 5.0 |
| 119 | |
| 120 | def _speedlimit(self): |
| 121 | if self.vx > self.vmax: |
| 122 | self.vx = self.vmax |
| 123 | if self.vx < -self.vmax: |
| 124 | self.vx = -self.vmax |
| 125 | |
| 126 | if self.vy > self.vmax: |
no outgoing calls
no test coverage detected
searching dependent graphs…