13
13
import numpy as np
14
14
import matplotlib .pyplot as plt
15
15
from numpy .random import randn , randint
16
+ from matplotlib .font_manager import FontProperties
16
17
17
18
instructions = """
18
19
Player A: Player B:
@@ -85,14 +86,11 @@ def update(self, pads):
85
86
self .vy *= 1.2 * pad .signy
86
87
fudge = .001
87
88
# probably cleaner with something like...
88
- #if not self.field.contains(self.x, self.y):
89
89
if self .x < fudge :
90
- #print("player A loses")
91
90
pads [1 ].score += 1
92
91
self ._reset (pads [0 ])
93
92
return True
94
93
if self .x > 7 - fudge :
95
- #print("player B loses")
96
94
pads [0 ].score += 1
97
95
self ._reset (pads [1 ])
98
96
return True
@@ -127,20 +125,42 @@ class Game(object):
127
125
def __init__ (self , ax ):
128
126
# create the initial line
129
127
self .ax = ax
130
- padAx = padBx = .50
128
+ ax .set_ylim ([- 1 , 1 ])
129
+ ax .set_xlim ([0 , 7 ])
130
+ padAx = 0
131
+ padBx = .50
131
132
padAy = padBy = .30
132
133
padBx += 6.3
133
- pA , = self .ax .barh (padAy , .2 , height = .3 , color = 'k' , alpha = .5 , edgecolor = 'b' , lw = 2 , label = "Player B" , animated = True )
134
- pB , = self .ax .barh (padBy , .2 , height = .3 , left = padBx , color = 'k' , alpha = .5 , edgecolor = 'r' , lw = 2 , label = "Player A" , animated = True )
134
+
135
+ # pads
136
+ pA , = self .ax .barh (padAy , .2 ,
137
+ height = .3 , color = 'k' , alpha = .5 , edgecolor = 'b' ,
138
+ lw = 2 , label = "Player B" ,
139
+ animated = True )
140
+ pB , = self .ax .barh (padBy , .2 ,
141
+ height = .3 , left = padBx , color = 'k' , alpha = .5 ,
142
+ edgecolor = 'r' , lw = 2 , label = "Player A" ,
143
+ animated = True )
135
144
136
145
# distractors
137
146
self .x = np .arange (0 , 2.22 * np .pi , 0.01 )
138
- self .line , = self .ax .plot (self .x , np .sin (self .x ), "r" , animated = True , lw = 4 )
139
- self .line2 , = self .ax .plot (self .x , np .cos (self .x ), "g" , animated = True , lw = 4 )
140
- self .line3 , = self .ax .plot (self .x , np .cos (self .x ), "g" , animated = True , lw = 4 )
141
- self .line4 , = self .ax .plot (self .x , np .cos (self .x ), "r" , animated = True , lw = 4 )
142
- self .centerline , = self .ax .plot ([3.5 , 3.5 ], [1 , - 1 ], 'k' , alpha = .5 , animated = True , lw = 8 )
143
- self .puckdisp = self .ax .scatter ([1 ], [1 ], label = '_nolegend_' , s = 200 , c = 'g' , alpha = .9 , animated = True )
147
+ self .line , = self .ax .plot (self .x , np .sin (self .x ), "r" ,
148
+ animated = True , lw = 4 )
149
+ self .line2 , = self .ax .plot (self .x , np .cos (self .x ), "g" ,
150
+ animated = True , lw = 4 )
151
+ self .line3 , = self .ax .plot (self .x , np .cos (self .x ), "g" ,
152
+ animated = True , lw = 4 )
153
+ self .line4 , = self .ax .plot (self .x , np .cos (self .x ), "r" ,
154
+ animated = True , lw = 4 )
155
+
156
+ # center line
157
+ self .centerline , = self .ax .plot ([3.5 , 3.5 ], [1 , - 1 ], 'k' ,
158
+ alpha = .5 , animated = True , lw = 8 )
159
+
160
+ # puck (s)
161
+ self .puckdisp = self .ax .scatter ([1 ], [1 ], label = '_nolegend_' ,
162
+ s = 200 , c = 'g' ,
163
+ alpha = .9 , animated = True )
144
164
145
165
self .canvas = self .ax .figure .canvas
146
166
self .background = None
@@ -151,15 +171,16 @@ def __init__(self, ax):
151
171
self .inst = True # show instructions from the beginning
152
172
self .background = None
153
173
self .pads = []
154
- self .pads .append (Pad (pA , 0 , padAy ))
174
+ self .pads .append (Pad (pA , padAx , padAy ))
155
175
self .pads .append (Pad (pB , padBx , padBy , 'r' ))
156
176
self .pucks = []
157
177
self .i = self .ax .annotate (instructions , (.5 , 0.5 ),
158
178
name = 'monospace' ,
159
179
verticalalignment = 'center' ,
160
180
horizontalalignment = 'center' ,
161
181
multialignment = 'left' ,
162
- textcoords = 'axes fraction' , animated = True )
182
+ textcoords = 'axes fraction' ,
183
+ animated = False )
163
184
self .canvas .mpl_connect ('key_press_event' , self .key_press )
164
185
165
186
def draw (self , evt ):
@@ -181,10 +202,6 @@ def draw(self, evt):
181
202
draw_artist (self .line3 )
182
203
draw_artist (self .line4 )
183
204
184
- # show the instructions - this is very slow
185
- if self .inst :
186
- self .ax .draw_artist (self .i )
187
-
188
205
# pucks and pads
189
206
if self .on :
190
207
self .ax .draw_artist (self .centerline )
@@ -196,23 +213,24 @@ def draw(self, evt):
196
213
for puck in self .pucks :
197
214
if puck .update (self .pads ):
198
215
# we only get here if someone scored
199
- self .pads [0 ].disp .set_label (" " + str (self .pads [0 ].score ))
200
- self .pads [1 ].disp .set_label (" " + str (self .pads [1 ].score ))
201
- self .ax .legend (loc = 'center' )
202
- self .leg = self .ax .get_legend ()
203
- #self.leg.draw_frame(False) #don't draw the legend border
204
- self .leg .get_frame ().set_alpha (.2 )
205
- plt .setp (self .leg .get_texts (), fontweight = 'bold' , fontsize = 'xx-large' )
206
- self .leg .get_frame ().set_facecolor ('0.2' )
216
+ self .pads [0 ].disp .set_label (
217
+ " " + str (self .pads [0 ].score ))
218
+ self .pads [1 ].disp .set_label (
219
+ " " + str (self .pads [1 ].score ))
220
+ self .ax .legend (loc = 'center' , framealpha = .2 ,
221
+ facecolor = '0.5' ,
222
+ prop = FontProperties (size = 'xx-large' ,
223
+ weight = 'bold' ))
224
+
207
225
self .background = None
208
- self .ax .figure .canvas .draw ()
226
+ self .ax .figure .canvas .draw_idle ()
209
227
return True
210
- puck .disp .set_offsets ([puck .x , puck .y ])
228
+ puck .disp .set_offsets ([[ puck .x , puck .y ] ])
211
229
self .ax .draw_artist (puck .disp )
212
230
213
231
# just redraw the axes rectangle
214
232
self .canvas .blit (self .ax .bbox )
215
-
233
+ self . canvas . flush_events ()
216
234
if self .cnt == 50000 :
217
235
# just so we don't get carried away
218
236
print ("...and you've been playing for too long!!!" )
@@ -246,7 +264,9 @@ def key_press(self, event):
246
264
self .pads [1 ].y = - 1
247
265
248
266
if event .key == 'a' :
249
- self .pucks .append (Puck (self .puckdisp , self .pads [randint (2 )], self .ax .bbox ))
267
+ self .pucks .append (Puck (self .puckdisp ,
268
+ self .pads [randint (2 )],
269
+ self .ax .bbox ))
250
270
if event .key == 'A' and len (self .pucks ):
251
271
self .pucks .pop ()
252
272
if event .key == ' ' and len (self .pucks ):
@@ -262,10 +282,11 @@ def key_press(self, event):
262
282
self .distract = not self .distract
263
283
264
284
if event .key == 'g' :
265
- #self.ax.clear()
266
285
self .on = not self .on
267
286
if event .key == 't' :
268
287
self .inst = not self .inst
269
- self .i .set_visible (self .i .get_visible ())
288
+ self .i .set_visible (not self .i .get_visible ())
289
+ self .background = None
290
+ self .canvas .draw_idle ()
270
291
if event .key == 'q' :
271
292
plt .close ()
0 commit comments