Kaynağa Gözat

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 yıl önce
ebeveyn
işleme
46f43f82bc
2 değiştirilmiş dosya ile 14 ekleme ve 3 silme
  1. 11
    3
      breakout.py
  2. 3
    0
      mapper.py

+ 11
- 3
breakout.py Dosyayı Görüntüle

@@ -14,11 +14,12 @@ import math
14 14
 import util
15 15
 
16 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 18
         self.gui = g
19 19
         self.input = i
20 20
         self.timestep = ts
21 21
         self.timeout = to
22
+        self.randomInitialAngle = ria
22 23
 
23 24
         self.paddle_width = 9
24 25
 
@@ -35,7 +36,9 @@ class Breakout:
35 36
         self.paddle_c = (255, 255, 255)
36 37
         self.text_c = (0, 0, 255)
37 38
 
38
-        random.seed()
39
+        if self.randomInitialAngle:
40
+            random.seed()
41
+
39 42
         self.restart()
40 43
 
41 44
     def place(self):
@@ -47,6 +50,11 @@ class Breakout:
47 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 58
     def restart(self):
51 59
         self.start = time.time()
52 60
         self.last = time.time()
@@ -171,7 +179,7 @@ class Breakout:
171 179
             if abs(pos_on_paddle) > self.paddle_width/2:
172 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 183
             if pos_on_paddle == 0:
176 184
                 pos_on_paddle = -0.5 if self.ball[3] > 0 else 0.5
177 185
 

+ 3
- 0
mapper.py Dosyayı Görüntüle

@@ -81,6 +81,9 @@ class MapperStripToRect(MapperNull):
81 81
         self.height = self.gui.height * 2
82 82
 
83 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 87
         if y >= self.gui.height:
85 88
             x += self.width
86 89
             y -= self.panelH

Loading…
İptal
Kaydet