Browse Source

breakout: collision with pieces

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

+ 32
- 14
breakout.py View File

14
 import util
14
 import util
15
 
15
 
16
 class Breakout:
16
 class Breakout:
17
-    def __init__(self, g, i, ts = 0.1, to = 60.0):
17
+    def __init__(self, g, i, ts = 0.04, to = 60.0):
18
         self.gui = g
18
         self.gui = g
19
         self.input = i
19
         self.input = i
20
         self.timestep = ts
20
         self.timestep = ts
43
         self.ball = [
43
         self.ball = [
44
             self.player, # x
44
             self.player, # x
45
             self.gui.height - 2, # y
45
             self.gui.height - 2, # y
46
-            1, # v x
47
-            -1, # v y
46
+            math.sqrt(.5), # v x
47
+            -math.sqrt(.5), # v y
48
         ]
48
         ]
49
 
49
 
50
     def restart(self):
50
     def restart(self):
111
 
111
 
112
     def step(self):
112
     def step(self):
113
         # move ball
113
         # move ball
114
+        old_ball = self.ball.copy()
114
         self.ball[0] += self.ball[2]
115
         self.ball[0] += self.ball[2]
115
         self.ball[1] += self.ball[3]
116
         self.ball[1] += self.ball[3]
116
 
117
 
124
             self.ball[2] = -self.ball[2]
125
             self.ball[2] = -self.ball[2]
125
             self.ball[0] = self.gui.width - 1
126
             self.ball[0] = self.gui.width - 1
126
 
127
 
127
-        # check for collisions with pieces
128
-        if self.data[int(self.ball[0])][int(self.ball[1])] != self.bg_c:
129
-            self.data[int(self.ball[0])][int(self.ball[1])] = self.bg_c
130
-            self.score += 1
131
-
132
-            # just invert Y travel direction
133
-            # TODO inaccurate collision behaviour in "corners"
134
-            self.ball[3] = -self.ball[3]
135
-
136
         # check for collision with ceiling
128
         # check for collision with ceiling
137
         if self.ball[1] <= 0:
129
         if self.ball[1] <= 0:
138
             self.ball[3] = -self.ball[3]
130
             self.ball[3] = -self.ball[3]
139
 
131
 
132
+        # check for collisions with pieces
133
+        grid_pos_x = int(self.ball[0])
134
+        grid_pos_y = int(self.ball[1])
135
+        if self.data[grid_pos_x][grid_pos_y] != self.bg_c:
136
+            self.data[grid_pos_x][grid_pos_y] = self.bg_c
137
+            self.score += 1
138
+
139
+            old_grid_pos_x = int(old_ball[0])
140
+            old_grid_pos_y = int(old_ball[1])
141
+
142
+            # horizontal collision
143
+            if old_grid_pos_y == grid_pos_y:
144
+                self.ball[2] = -self.ball[2]
145
+            # vertical collision
146
+            elif old_grid_pos_x == grid_pos_x:
147
+                self.ball[3] = -self.ball[3]
148
+            # "diagonal" collision with horizontal obstacle
149
+            elif self.data[old_grid_pos_x-1][old_grid_pos_y] != self.bg_c or self.data[old_grid_pos_x+1][old_grid_pos_y] != self.bg_c:
150
+                self.ball[2] = -self.ball[2]
151
+            # "diagonal" collision with vertical obstacle
152
+            elif self.data[old_grid_pos_x][old_grid_pos_y-1] != self.bg_c or self.data[old_grid_pos_x][old_grid_pos_y+1] != self.bg_c:
153
+                self.ball[3] = -self.ball[3]
154
+            # "diagonal" collision without obstacle
155
+            else:
156
+                self.ball[2] = -self.ball[2]
157
+                self.ball[3] = -self.ball[3]
140
 
158
 
141
         # check for collision with floor
159
         # check for collision with floor
142
         if self.ball[1] >= self.gui.height - 1:
160
         if self.ball[1] >= self.gui.height - 1:
159
 
177
 
160
             # small angles in the middle, big angles at the end of the paddle (angle measured against the orthogonal of the paddle)
178
             # small angles in the middle, big angles at the end of the paddle (angle measured against the orthogonal of the paddle)
161
             angle_degree = 80 * pos_on_paddle / (self.paddle_width/2)
179
             angle_degree = 80 * pos_on_paddle / (self.paddle_width/2)
162
-            self.ball[2] = -1 * math.sin(angle_degree/180*3.14159) * math.sqrt(2)
163
-            self.ball[3] = -1 * math.cos(angle_degree/180*3.14159) * math.sqrt(2)
180
+            self.ball[2] = -1 * math.sin(angle_degree/180*3.14159)
181
+            self.ball[3] = -1 * math.cos(angle_degree/180*3.14159)
164
 
182
 
165
 
183
 
166
     def finishedEndScreen(self):
184
     def finishedEndScreen(self):

Loading…
Cancel
Save