Procházet zdrojové kódy

breakout: collision with paddle

Signed-off-by: Thomas Buck <thomas@venom.fritz.box>
Jannis před 1 rokem
rodič
revize
8ff126684a
1 změnil soubory, kde provedl 17 přidání a 10 odebrání
  1. 17
    10
      breakout.py

+ 17
- 10
breakout.py Zobrazit soubor

@@ -62,7 +62,7 @@ class Breakout:
62 62
         self.maxScore = 5 * (self.gui.width - 2)
63 63
 
64 64
         # TODO easy mode
65
-        self.nothing_to_lose = True
65
+        self.nothing_to_lose = False
66 66
 
67 67
         DrawText = util.getTextDrawer()
68 68
         self.text = DrawText(self.gui, self.text_c)
@@ -136,15 +136,6 @@ class Breakout:
136 136
         if self.ball[1] <= 0:
137 137
             self.ball[3] = -self.ball[3]
138 138
 
139
-        # check for collision with paddle
140
-        if (self.ball[1] == self.gui.height - 2) and (self.ball[0] >= (self.player - int(self.paddle_width / 2))) and (self.ball[0] <= (self.player + int(self.paddle_width / 2))):
141
-            # TODO angle for bounce from paddle
142
-            #self.ball[3] = -self.ball[3]
143
-            d = self.ball[0] - (self.player - (self.paddle_width / 2))
144
-            angle = (d / self.paddle_width - 0.5) / 2 * 3.14159
145
-            print(math.degrees(angle))
146
-            self.ball[2] = math.cos(angle) * math.sqrt(2)
147
-            self.ball[3] = math.sin(angle) * math.sqrt(2)
148 139
 
149 140
         # check for collision with floor
150 141
         if self.ball[1] >= self.gui.height - 1:
@@ -155,6 +146,22 @@ class Breakout:
155 146
                 self.place()
156 147
                 self.lives -= 1
157 148
 
149
+        # check for collision with paddle
150
+        elif self.ball[1] >= self.gui.height - 2:
151
+            pos_on_paddle = self.player - self.ball[0]
152
+            if abs(pos_on_paddle) > self.paddle_width/2:
153
+                return
154
+
155
+            # if hit exactly in the middle the direction of the angle depens on the x-direction in came from
156
+            if pos_on_paddle == 0:
157
+                pos_on_paddle = -0.5 if self.ball[3] > 0 else 0.5
158
+
159
+            # small angles in the middle, big angles at the end of the paddle (angle measured against the orthogonal of the paddle)
160
+            angle_degree = 80 * pos_on_paddle / (self.paddle_width/2)
161
+            self.ball[2] = -1 * math.sin(angle_degree/180*3.14159) * math.sqrt(2)
162
+            self.ball[3] = -1 * math.cos(angle_degree/180*3.14159) * math.sqrt(2)
163
+
164
+
158 165
     def finishedEndScreen(self):
159 166
         if self.score >= self.maxScore:
160 167
             return self.winText.finished()

Loading…
Zrušit
Uložit