Browse Source

double speed of breakout gameplay. fix bug where breakout paddle appeared in middle of screen on large pi matrix. randomize breakout start angle.

Thomas Buck 1 year ago
parent
commit
46f43f82bc
2 changed files with 14 additions and 3 deletions
  1. 11
    3
      breakout.py
  2. 3
    0
      mapper.py

+ 11
- 3
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.04, to = 60.0):
17
+    def __init__(self, g, i, ts = 0.025, to = 60.0, ria = True):
18
         self.gui = g
18
         self.gui = g
19
         self.input = i
19
         self.input = i
20
         self.timestep = ts
20
         self.timestep = ts
21
         self.timeout = to
21
         self.timeout = to
22
+        self.randomInitialAngle = ria
22
 
23
 
23
         self.paddle_width = 9
24
         self.paddle_width = 9
24
 
25
 
35
         self.paddle_c = (255, 255, 255)
36
         self.paddle_c = (255, 255, 255)
36
         self.text_c = (0, 0, 255)
37
         self.text_c = (0, 0, 255)
37
 
38
 
38
-        random.seed()
39
+        if self.randomInitialAngle:
40
+            random.seed()
41
+
39
         self.restart()
42
         self.restart()
40
 
43
 
41
     def place(self):
44
     def place(self):
47
             -math.sqrt(.5), # v y
50
             -math.sqrt(.5), # v y
48
         ]
51
         ]
49
 
52
 
53
+        if self.randomInitialAngle:
54
+            angle_degree = random.randrange(-45, 45)
55
+            self.ball[2] = -1 * math.sin(angle_degree / 180 * 3.14159)
56
+            self.ball[3] = -1 * math.cos(angle_degree / 180 * 3.14159)
57
+
50
     def restart(self):
58
     def restart(self):
51
         self.start = time.time()
59
         self.start = time.time()
52
         self.last = time.time()
60
         self.last = time.time()
171
             if abs(pos_on_paddle) > self.paddle_width/2:
179
             if abs(pos_on_paddle) > self.paddle_width/2:
172
                 return
180
                 return
173
 
181
 
174
-            # if hit exactly in the middle the direction of the angle depens on the x-direction in came from
182
+            # if hit exactly in the middle the direction of the angle depens on the x-direction it came from
175
             if pos_on_paddle == 0:
183
             if pos_on_paddle == 0:
176
                 pos_on_paddle = -0.5 if self.ball[3] > 0 else 0.5
184
                 pos_on_paddle = -0.5 if self.ball[3] > 0 else 0.5
177
 
185
 

+ 3
- 0
mapper.py View File

81
         self.height = self.gui.height * 2
81
         self.height = self.gui.height * 2
82
 
82
 
83
     def set_pixel(self, x, y, color):
83
     def set_pixel(self, x, y, color):
84
+        if (x < 0) or (y < 0) or (x >= self.width) or (y >= self.height):
85
+            return
86
+
84
         if y >= self.gui.height:
87
         if y >= self.gui.height:
85
             x += self.width
88
             x += self.width
86
             y -= self.panelH
89
             y -= self.panelH

Loading…
Cancel
Save