Browse Source

breakout: collision with paddle

Signed-off-by: Thomas Buck <thomas@venom.fritz.box>
Jannis 1 year ago
parent
commit
8ff126684a
1 changed files with 17 additions and 10 deletions
  1. 17
    10
      breakout.py

+ 17
- 10
breakout.py View File

62
         self.maxScore = 5 * (self.gui.width - 2)
62
         self.maxScore = 5 * (self.gui.width - 2)
63
 
63
 
64
         # TODO easy mode
64
         # TODO easy mode
65
-        self.nothing_to_lose = True
65
+        self.nothing_to_lose = False
66
 
66
 
67
         DrawText = util.getTextDrawer()
67
         DrawText = util.getTextDrawer()
68
         self.text = DrawText(self.gui, self.text_c)
68
         self.text = DrawText(self.gui, self.text_c)
136
         if self.ball[1] <= 0:
136
         if self.ball[1] <= 0:
137
             self.ball[3] = -self.ball[3]
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
         # check for collision with floor
140
         # check for collision with floor
150
         if self.ball[1] >= self.gui.height - 1:
141
         if self.ball[1] >= self.gui.height - 1:
155
                 self.place()
146
                 self.place()
156
                 self.lives -= 1
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
     def finishedEndScreen(self):
165
     def finishedEndScreen(self):
159
         if self.score >= self.maxScore:
166
         if self.score >= self.maxScore:
160
             return self.winText.finished()
167
             return self.winText.finished()

Loading…
Cancel
Save