/
test_ui.py
3079 lines (2277 loc) · 80.4 KB
/
test_ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
from __future__ import print_function
import os
import shutil
import tempfile
import mock
import pygame
import pytest
from pygame.locals import (
KEYDOWN,
MOUSEBUTTONDOWN,
MOUSEBUTTONUP,
MOUSEMOTION,
Rect,
)
from pyspacewar.ui import key_name
class SurfaceStub(object):
def __init__(self, size=(800, 600), bitsize=32):
self.w, self.h = size
self.alpha = 255
self.colorkey = None
self.bitsize = bitsize
self._ops = []
def get_width(self):
return self.w
def get_height(self):
return self.h
def get_size(self):
return (self.w, self.h)
def get_rect(self):
return Rect(0, 0, self.w, self.h)
def get_bitsize(self):
return self.bitsize
def set_alpha(self, alpha):
self.alpha = alpha
def set_colorkey(self, color):
r, g, b = color
self.colorkey = (r, g, b)
def _fmt_color(self, color):
if color == self.colorkey:
return '<colorkey>'
r, g, b = color
return "#%02x%02x%02x" % (r, g, b)
def set_at(self, pos, color):
x, y = pos
self._record("(%s, %s) <- %s" % (x, y, self._fmt_color(color)))
def blit(self, what, pos, area=None):
x, y = pos
if area:
ax, ay, aw, ah = area
subset = "[(%s, %s)..(%s, %s)]" % (ax, ay, ax + aw - 1, ay + ah - 1)
else:
subset = ""
if isinstance(what, SurfaceStub) and what.alpha != 255:
subset += "[alpha=%s]" % what.alpha
self._record("(%s, %s) <- %r%s" % (x, y, what, subset))
if isinstance(what, SurfaceStub):
for op in what._ops:
self._record(" %s" % op)
def fill(self, color, rect=None):
if not rect:
# clearing the entire surface makes previous drawing operations
# irrelevant
self._ops = []
x, y, w, h = rect or (0, 0, self.w, self.h)
self._record("(%s, %s)..(%s, %s) <- fill(%s)" % (
x, y, x + w - 1, y + h - 1, self._fmt_color(color)))
def _rect(self, color, rect, line_width):
x, y, w, h = rect
self._record("(%s, %s)..(%s, %s) <- rect(%s, %s)" % (
x, y, x + w - 1, y + h - 1, self._fmt_color(color), line_width))
def _line(self, color, pt1, pt2):
x1, y1 = pt1
x2, y2 = pt2
self._record("(%s, %s)..(%s, %s) <- line(%s)" % (
x1, y1, x2, y2, self._fmt_color(color)))
def _aaline(self, color, pt1, pt2):
x1, y1 = pt1
x2, y2 = pt2
self._record("(%s, %s)..(%s, %s) <- aaline(%s)" % (
x1, y1, x2, y2, self._fmt_color(color)))
def _circle(self, color, center, radius, width=0):
x, y = center
extra = []
if width:
extra.append("width=%s" % width)
self._record("(%s, %s) <- circle(%s, %s%s)" % (
x, y, self._fmt_color(color), radius, ", ".join(extra)))
def _record(self, op):
self._ops.append(op)
def __repr__(self):
return '<Surface(%dx%d)>' % (self.w, self.h)
class PrintingSurfaceStub(SurfaceStub):
def __init__(self, size=(800, 600), filter=None, bitsize=32):
super(PrintingSurfaceStub, self).__init__(size, bitsize)
self.filter = filter
def _record(self, op):
if not self.filter or self.filter(op):
print(op)
class TextSurfaceStub(SurfaceStub):
def __init__(self, size, text="<text>"):
super(TextSurfaceStub, self).__init__(size)
self.text = text
def __repr__(self):
text = self.text
if not isinstance(text, str):
text = text.encode('UTF-8')
return repr(text)
class FontStub(object):
def size(self, text):
w = len(text) * 10
h = 16
return (w, h)
def get_linesize(self):
return 16
def render(self, text, antialias, color, background=None):
w, h = self.size(text)
return TextSurfaceStub((w, h), text)
class ImageStub(SurfaceStub):
def __init__(self, size=(100, 80)):
super(ImageStub, self).__init__(size)
@classmethod
def load(cls, filename):
return ImageStub()
def convert(self):
return ImageStub(self.get_size())
def convert_alpha(self):
return ImageStub(self.get_size())
def __repr__(self):
return '<Image(%dx%d)>' % (self.w, self.h)
class TransformStub(object):
@staticmethod
def scale(surface, size):
return ImageStub(size)
class DrawStub(object):
def rect(self, surface, color, rect, line_width):
if isinstance(surface, SurfaceStub):
surface._rect(color, rect, line_width)
def line(self, surface, color, pt1, pt2):
if isinstance(surface, SurfaceStub):
surface._line(color, pt1, pt2)
def aaline(self, surface, color, pt1, pt2):
if isinstance(surface, SurfaceStub):
surface._aaline(color, pt1, pt2)
def circle(self, surface, color, center, radius, width=0):
if isinstance(surface, SurfaceStub):
surface._circle(color, center, radius, width)
def array_alpha_stub(surface):
import numpy
size = surface.get_size()
array = numpy.empty(size, numpy.uint8)
return array
def pixels_alpha_stub(surface):
import numpy
size = surface.get_size()
array = numpy.empty(size, numpy.uint8)
return array
def doctest_is_modifier_key():
"""Test for is_modifier_key
>>> from pyspacewar.ui import is_modifier_key
>>> from pygame.locals import K_LSHIFT, K_a
Sometimes we want to distinguish modifier keys like Shift from
real keys like A.
>>> is_modifier_key(K_LSHIFT)
True
>>> is_modifier_key(K_a)
False
"""
def doctest_find():
"""Test for find
>>> from pyspacewar.ui import find
This is a helper that can compute pathnames to data files
>>> os.path.exists(find('images', 'background.jpg'))
True
"""
def doctest_Viewport():
"""Tests for Viewport
>>> from pyspacewar.world import Vector
>>> from pyspacewar.ui import Viewport
>>> viewport = Viewport(SurfaceStub())
>>> viewport.origin
Vector(0, 0)
>>> viewport.scale
1.0
To make things more interesting we will set a scale of 0.2 and shift
the origin to (100, 200)
>>> viewport.scale = 0.2
>>> viewport.origin = Vector(100, 200)
``screen_len`` converts a length in world units to screen units
>>> viewport.screen_len(50)
10
>>> viewport.screen_len(6)
1
``screen_pos`` converts world coordinates to screen coordinates
>>> viewport.screen_pos(Vector(100, 200))
(400, 300)
>>> viewport.screen_pos(Vector(150, 250))
(410, 290)
>>> viewport.screen_pos(Vector(106, 199))
(401, 300)
``world_pos`` converts screen coordinates to world coordinates
>>> viewport.world_pos((400, 300))
(100.0, 200.0)
>>> viewport.world_pos((410, 290))
(150.0, 250.0)
>>> viewport.world_pos((401, 300))
(105.0, 200.0)
``in_screen`` tests if world coordinates are visible on screen
>>> viewport.in_screen(Vector(100, 200))
True
>>> viewport.in_screen(Vector(2101, 200))
False
```shift_by_pixels`` adjusts the viewport position by a given
number of screen pixels
>>> viewport.shift_by_pixels((20, -10))
>>> viewport.screen_pos(Vector(100, 200))
(380, 310)
"""
def doctest_Viewport_screen_size_change():
"""Tests for Viewport
>>> from pyspacewar.world import Vector
>>> from pyspacewar.ui import Viewport
>>> viewport = Viewport(SurfaceStub())
``screen_pos`` converts world coordinates to screen coordinates
>>> viewport.screen_pos(Vector(0, 0))
(400, 300)
You can change the size of the surface
>>> viewport.surface.w = 640
>>> viewport.surface.h = 480
>>> viewport.surface_size_changed()
>>> viewport.screen_pos(Vector(0, 0))
(320, 240)
"""
def doctest_Viewport_keep_visible():
"""Tests for Viewport.keep_visible.
>>> from pyspacewar.world import Vector
>>> from pyspacewar.ui import Viewport
>>> viewport = Viewport(SurfaceStub())
Points that are already visible change nothing
>>> viewport.keep_visible([Vector(0, 0)], 10)
>>> viewport.keep_visible([Vector(100, 100)], 10)
>>> viewport.keep_visible([Vector(-200, -100)], 10)
>>> print(viewport.origin, viewport.scale)
(0.000, 0.000) 1.0
We can see the range of points that are inside the view margin, for
a given margin size
>>> viewport.world_inner_bounds(0)
(-400.0, -300.0, 400.0, 300.0)
>>> viewport.world_inner_bounds(10)
(-390.0, -290.0, 390.0, 290.0)
Points that are off-screen cause scrolling
>>> viewport.keep_visible([Vector(600, 200)], 10)
>>> print(viewport.origin, viewport.scale)
(210.000, 0.000) 1.0
>>> viewport.keep_visible([Vector(300, 700)], 10)
>>> print(viewport.origin, viewport.scale)
(210.000, 410.000) 1.0
>>> viewport.keep_visible([Vector(-300, -100)], 10)
>>> print(viewport.origin, viewport.scale)
(90.000, 190.000) 1.0
>>> viewport.world_inner_bounds(10)
(-300.0, -100.0, 480.0, 480.0)
If you specify several widely-separated points, they may cause a scale
change
>>> viewport.keep_visible([Vector(-300, -100),
... Vector(500, 400)], 10)
>>> print(viewport.origin, round(viewport.scale, 3))
(99.732, 190.000) 0.974
>>> xmin, ymin, xmax, ymax = viewport.world_inner_bounds(10)
>>> xmin <= -300 and 500 <= xmax
True
>>> ymin <= -100 and 400 <= ymax
True
"""
def doctest_Viewport_draw_trail():
"""Tests for Viewport.draw_trail
>>> from pyspacewar.world import Vector
>>> from pyspacewar.ui import Viewport
>>> viewport = Viewport(SurfaceStub())
>>> viewport.scale = 2.0
>>> viewport.draw_trail([
... Vector(120, 50),
... Vector(130, 55),
... Vector(140, 60),
... ], [
... (250, 250, 250),
... (200, 200, 200),
... (150, 150, 150),
... ], PrintingSurfaceStub().set_at)
(640, 200) <- #fafafa
(660, 190) <- #c8c8c8
(680, 180) <- #969696
"""
def doctest_FrameRateCounter_frame():
"""Tests for FrameRateCounter.frame
>>> from pyspacewar.ui import FrameRateCounter
>>> frc = FrameRateCounter()
>>> frc.frame()
>>> len(frc.frames)
1
>>> frc.frame()
>>> len(frc.frames)
2
>>> frc.frames = list(range(frc.avg_last_n_frames - 1))
>>> frc.frame()
>>> len(frc.frames) == frc.avg_last_n_frames
True
>>> frc.frame()
>>> len(frc.frames) == frc.avg_last_n_frames
True
>>> frc.frame()
>>> len(frc.frames) == frc.avg_last_n_frames
True
>>> frc.frames[:3]
[2, 3, 4]
"""
def doctest_FrameRateCounter_reset():
"""Tests for FrameRateCounter.reset
>>> from pyspacewar.ui import FrameRateCounter
>>> frc = FrameRateCounter()
>>> frc.frames = list(range(15))
>>> frc.reset()
>>> frc.frames
[]
"""
def doctest_FrameRateCounter_fps():
"""Tests for FrameRateCounter.fps
>>> from pyspacewar.ui import FrameRateCounter
>>> frc = FrameRateCounter()
>>> frc.frames = []
>>> frc.fps()
0
>>> frc.frames = [1000]
>>> frc.fps()
0
>>> frc.frames += [1005]
5 ms per frame corresponds to 200 frames per second.
>>> frc.fps()
200.0
>>> frc.frames += [1020]
The first frame took 5 ms, the next one 15 ms. Average is 10 ms per frame,
and this corresponds to 100 frames per second.
>>> frc.fps()
100.0
"""
def doctest_FrameRateCounter_fps_no_division_by_zero():
"""Tests for FrameRateCounter.fps
>>> from pyspacewar.ui import FrameRateCounter
>>> frc = FrameRateCounter()
>>> frc.frames = []
>>> frc.fps()
0
>>> frc.frames = [1000]
>>> frc.fps()
0
>>> frc.frames += [1000]
>>> frc.fps()
0
"""
def doctest_FrameRateCounter_notional_fps():
"""Tests for FrameRateCounter.notional_fps
>>> from pyspacewar.ui import FrameRateCounter
>>> frc = FrameRateCounter()
>>> frc.frames = []
>>> frc.notional_fps()
0.0
20 ms per frame corresponds to 50 fps
>>> frc.get_ticks = lambda: 1020
>>> frc.frames = [1000]
>>> frc.notional_fps()
50.0
"""
def doctest_HUDCollection():
"""Tests for HUDCollection
>>> from pyspacewar.ui import HUDCollection
>>> from pyspacewar.ui import HUDElement
This is a very boring class that holds a bunch of elements
and can ask all of them to draw.
>>> hc = HUDCollection([
... HUDElement(180, 60, 1.0, 0.0),
... ])
>>> hc.draw(SurfaceStub())
"""
def doctest_HUDElement():
"""Tests for HUDElement
>>> from pyspacewar.ui import HUDElement
>>> e = HUDElement(180, 60, 0, 0)
A HUD element can compute its on-screen position
>>> e.position(SurfaceStub(), margin=10)
(10, 10)
Alignment goes from 0.0 (left/top) to 1.0 (right/bottom)
>>> e.xalign = 0.5
>>> e.yalign = 1.0
>>> e.position(SurfaceStub(), margin=10)
(310, 530)
The default draw method is a no-op; you're supposed to override
it in subclasses
>>> e.draw(SurfaceStub())
"""
def doctest_HUDLabel():
"""Test for HUDLabel
>>> from pyspacewar.ui import HUDLabel
>>> font = FontStub()
>>> label = HUDLabel(font, "Hello", xalign=1.0)
>>> label.draw(PrintingSurfaceStub())
(740, 10) <- 'Hello'
"""
def doctest_HUDFormattedText():
r"""Tests for HUDFormattedText
>>> from pyspacewar.ui import HUDFormattedText
>>> font = FontStub()
>>> bold_font = FontStub()
>>> text = u'''
... = Hello =
...
... This is nice?
...
... it is \u2014 really nice???
...
... It even supports justified text if the text is long
... enough not to fit on a single line blah blah blah
... is this thing on?
... '''.lstrip()
>>> help_text = HUDFormattedText(font, bold_font, text)
>>> help_text.position(SurfaceStub())
(30, 30)
>>> (help_text.width, help_text.height)
(740, 540)
>>> help_text.draw(PrintingSurfaceStub())
(30, 30) <- <Surface(740x540)>[alpha=242]
(0, 0)..(739, 539) <- fill(#010208)
(0, 0) <- <colorkey>
(0, 539) <- <colorkey>
(739, 0) <- <colorkey>
(739, 539) <- <colorkey>
(70, 70) <- 'Hello'
(70, 102) <- 'This'
(120, 102) <- 'is'
(150, 102) <- 'nice?'
(90, 134) <- 'it is'
(230, 134) <- 'really'
(300, 134) <- 'nice???'
(70, 166) <- 'It'
(100, 166) <- 'even'
(151, 166) <- 'supports'
(242, 166) <- 'justified'
(343, 166) <- 'text'
(394, 166) <- 'if'
(424, 166) <- 'the'
(465, 166) <- 'text'
(516, 166) <- 'is'
(547, 166) <- 'long'
(598, 166) <- 'enough'
(669, 166) <- 'not'
(710, 166) <- 'to'
(70, 182) <- 'fit'
(110, 182) <- 'on'
(140, 182) <- 'a'
(160, 182) <- 'single'
(230, 182) <- 'line'
(280, 182) <- 'blah'
(330, 182) <- 'blah'
(380, 182) <- 'blah'
(430, 182) <- 'is'
(460, 182) <- 'this'
(510, 182) <- 'thing'
(570, 182) <- 'on?'
(620, 514) <- 'Page 1 of 1'
"""
def doctest_HUDFormattedText_layout_pages_last_paragraph_has_keep_with_next():
"""Test for HUDFormattedText.layout_pages
>>> from pyspacewar.ui import HUDFormattedText
>>> font = FontStub()
>>> bold_font = FontStub()
>>> text = u'''
... = Hello =
...
... Lorem ipsum et cetera and so on blah blah blah.
...
... = And another thing =
...
... when splitting pages we keep titles together with the
... text that follows them.
...
... = And the last thing =
... '''.strip()
>>> help_text = HUDFormattedText(font, bold_font, text)
>>> pages = help_text.layout_pages(text, (300, 100))
This lays out the text onto multiple pages.
>>> for n, page in enumerate(pages):
... print('Page {}:'.format(n))
... for para_size, para in page:
... print('- paragraph (height={})'.format(para_size))
Page 0:
- paragraph (height=64)
Page 1:
- paragraph (height=80)
Page 2:
- paragraph (height=16)
There's one paragraph on each page because the title has been
merged together with the following text, to keep them on the same
page.
"""
def doctest_HUDFormattedText_render_text_no_pages():
"""Test for HUDFormattedText.layout_pages
>>> from pyspacewar.ui import HUDFormattedText
>>> font = FontStub()
>>> bold_font = FontStub()
>>> text = ''
>>> help_text = HUDFormattedText(font, bold_font, text)
>>> help_text.split_to_paragraphs(text)
['']
>>> help_text.layout_pages(text, (300, 100))
[[(0, [])]]
>>> help_text.draw(PrintingSurfaceStub())
(30, 30) <- <Surface(740x540)>[alpha=242]
(0, 0)..(739, 539) <- fill(#010208)
(0, 0) <- <colorkey>
(0, 539) <- <colorkey>
(739, 0) <- <colorkey>
(739, 539) <- <colorkey>
(620, 514) <- 'Page 1 of 1'
"""
def doctest_HUDInfoPanel():
"""Tests for HUDInfoPanel
>>> from pyspacewar.ui import HUDInfoPanel
>>> font = FontStub()
>>> panel = HUDInfoPanel(font, ncols=10, content=[
... ['Lat', 42],
... ['Lon', lambda: -55],
... ])
>>> panel.draw(PrintingSurfaceStub())
(10, 10) <- <Surface(100x32)>[alpha=204.0]
(0, 0)..(99, 31) <- fill(#080808)
(0, 0) <- <colorkey>
(0, 31) <- <colorkey>
(99, 0) <- <colorkey>
(99, 31) <- <colorkey>
(11, 11) <- 'Lat'
(89, 11) <- '42'
(11, 27) <- 'Lon'
(79, 27) <- '-55'
"""
def doctest_HUDShipInfo():
"""Test for HUDShipInfo
>>> from pyspacewar.ui import HUDShipInfo
>>> from pyspacewar.world import Ship
>>> ship = Ship()
>>> font = FontStub()
>>> panel = HUDShipInfo(ship, font)
>>> panel.draw(PrintingSurfaceStub())
(10, 10) <- <Surface(120x76)>[alpha=204.0]
(0, 0)..(119, 75) <- fill(#080808)
(0, 0) <- <colorkey>
(0, 75) <- <colorkey>
(119, 0) <- <colorkey>
(119, 75) <- <colorkey>
(11, 11) <- 'direction'
(119, 11) <- '0'
(11, 27) <- 'heading'
(119, 27) <- '0'
(11, 43) <- 'speed'
(99, 43) <- '0.0'
(11, 59) <- 'frags'
(119, 59) <- '0'
(11, 81)..(128, 84) <- rect(#ccffff, 1)
(12, 82)..(127, 83) <- fill(#ffffff)
"""
def doctest_HUDCompass():
"""Test for HUDCompass
>>> from pyspacewar.ui import HUDCompass, Viewport
>>> from pyspacewar.world import World, Ship, Planet, Missile, Vector
>>> world = World()
>>> world.add(Planet(Vector(20, -30), mass=20, radius=1))
>>> world.add(Planet(Vector(20, 30), mass=20, radius=30))
>>> world.add(Planet(Vector(-20, 30), mass=20, radius=50))
>>> world.add(Planet(Vector(2000, -300), mass=10))
>>> world.add(Missile(Vector(15, 0)))
>>> ship = Ship()
>>> viewport = Viewport(SurfaceStub())
>>> compass = HUDCompass(world, ship, viewport)
>>> compass.draw(PrintingSurfaceStub())
(10, 490) <- <Surface(100x100)>[alpha=229]
(0, 0)..(99, 99) <- fill(<colorkey>)
(50, 50) <- circle(#001122, 50)
(50, 50) <- #99aaff
(51, 51) <- #aa7766
(51, 49)..(52, 50) <- fill(#aa7766)
(49, 49) <- circle(#aa7766, 2)
(50, 50)..(95, 50) <- aaline(#445566)
(50, 50)..(50, 50) <- aaline(#99aaff)
"""
def doctest_HUDCompass_no_antialiasing():
"""Test for HUDCompass
>>> from pyspacewar.ui import HUDCompass, Viewport
>>> from pyspacewar.world import World, Ship, Vector
>>> world = World()
>>> ship = Ship(velocity=Vector(1.0, 1.0))
>>> viewport = Viewport(SurfaceStub())
>>> compass = HUDCompass(world, ship, viewport)
>>> compass.draw(PrintingSurfaceStub(bitsize=16))
(10, 490) <- <Surface(100x100)>[alpha=229]
(0, 0)..(99, 99) <- fill(<colorkey>)
(50, 50) <- circle(#001122, 50)
(50, 50) <- #99aaff
(50, 50)..(95, 50) <- line(#445566)
(50, 50)..(81, 19) <- line(#99aaff)
"""
def doctest_HUDTitle_FadingImage():
"""Test for HUDTitle
>>> from pyspacewar.ui import HUDTitle, FadingImage
>>> title = HUDTitle(ImageStub())
>>> title.image = FadingImage(ImageStub())
>>> title.draw(PrintingSurfaceStub())
(350, 135) <- <Image(100x80)>
Each frame also drops the alpha level
>>> title.alpha
242.25
>>> title.draw(PrintingSurfaceStub())
(350, 135) <- <Image(100x80)>[alpha=242.25]
>>> title.alpha
230.1375
Eventually the image becomes invisible
>>> title.alpha = 0.95
>>> title.draw(PrintingSurfaceStub())
"""
def doctest_HUDTitle_NumPyFadingImage():
"""Test for HUDTitle
>>> try:
... import numpy # noqa: F401
... except ImportError:
... pytest.skip('needs numpy')
>>> from pyspacewar.ui import HUDTitle, NumPyFadingImage
>>> title = HUDTitle(ImageStub())
>>> title.image = NumPyFadingImage(ImageStub())
>>> title.draw(PrintingSurfaceStub())
(350, 135) <- <Image(100x80)>
Each frame also drops the alpha level, which is reflected directly
in the image alpha channel via numpy array operations that we cannot
easily see in this doctest
>>> title.alpha
242.25
>>> title.draw(PrintingSurfaceStub())
(350, 135) <- <Image(100x80)>
>>> title.alpha
230.1375
Eventually the image becomes invisible
>>> title.alpha = 0.95
>>> title.draw(PrintingSurfaceStub())
"""
def doctest_HUDMenu():
r"""Test for HUDMenu
>>> from pyspacewar.ui import HUDMenu
>>> font = FontStub()
>>> menu = HUDMenu(font, [
... 'Say',
... 'Hello',
... 'etc.\t!',
... ])
>>> surface = PrintingSurfaceStub()
>>> menu.draw(surface)
(306, 236) <- <Surface(188x128)>[(0, 0)..(187, 127)][alpha=229.5]
(0, 0)..(187, 127) <- fill(<colorkey>)
(0, 0)..(187, 31) <- fill(#d23030)
(79, 8) <- 'Say'
(0, 0) <- <colorkey>
(0, 31) <- <colorkey>
(187, 0) <- <colorkey>
(187, 31) <- <colorkey>
(0, 48)..(187, 79) <- fill(#781818)
(69, 56) <- 'Hello'
(0, 48) <- <colorkey>
(0, 79) <- <colorkey>
(187, 48) <- <colorkey>
(187, 79) <- <colorkey>
(0, 96)..(187, 127) <- fill(#781818)
(32, 104) <- 'etc.'
(146, 104) <- '!'
(0, 96) <- <colorkey>
(0, 127) <- <colorkey>
(187, 96) <- <colorkey>
(187, 127) <- <colorkey>
>>> menu.find(surface, (310, 244))
0
>>> menu.find(surface, (310, 356))
2
>>> menu.find(surface, (310, 100))
-1
"""
def doctest_HUDMenu_scrolling():
r"""Test for HUDMenu
>>> from pyspacewar.ui import HUDMenu
>>> font = FontStub()
>>> menu = HUDMenu(font, [
... 'Item {}'.format(n) for n in range(6)
... ])
>>> surface = PrintingSurfaceStub(
... size=(200, 200),
... filter=lambda s: 'colorkey' not in s,
... )
>>> menu.draw(surface)
(38, 12) <- <Surface(124x272)>[(0, 0)..(123, 175)][alpha=229.5]
(0, 0)..(123, 31) <- fill(#d23030)
(32, 8) <- 'Item 0'
(0, 48)..(123, 79) <- fill(#781818)
(32, 56) <- 'Item 1'
(0, 96)..(123, 127) <- fill(#781818)
(32, 104) <- 'Item 2'
(0, 144)..(123, 175) <- fill(#781818)
(32, 152) <- 'Item 3'
(0, 192)..(123, 223) <- fill(#781818)
(32, 200) <- 'Item 4'
(0, 240)..(123, 271) <- fill(#781818)
(32, 248) <- 'Item 5'
If we look closely we can see that the menu is larger than the
screen surface so only the upper part of it was displayed.
>>> menu.full_height
272
>>> menu.height
176
>>> menu.top
0
Scrolling happens automatically if you select an item below the
displayed portion.
>>> menu.selected_item = 5
>>> menu.draw(surface)
(38, 12) <- <Surface(124x272)>[(0, 96)..(123, 271)][alpha=229.5]
(0, 0)..(123, 31) <- fill(#781818)
(32, 8) <- 'Item 0'
(0, 48)..(123, 79) <- fill(#781818)
(32, 56) <- 'Item 1'
(0, 96)..(123, 127) <- fill(#781818)
(32, 104) <- 'Item 2'
(0, 144)..(123, 175) <- fill(#781818)
(32, 152) <- 'Item 3'
(0, 192)..(123, 223) <- fill(#781818)
(32, 200) <- 'Item 4'
(0, 240)..(123, 271) <- fill(#d23030)
(32, 248) <- 'Item 5'
>>> menu.top
96
Scrolling also happens automatically if you select an item above the
displayed portion.
>>> menu.selected_item = 1
>>> menu.draw(surface)
(38, 12) <- <Surface(124x272)>[(0, 48)..(123, 223)][alpha=229.5]
(0, 0)..(123, 31) <- fill(#781818)
(32, 8) <- 'Item 0'
(0, 48)..(123, 79) <- fill(#d23030)
(32, 56) <- 'Item 1'
(0, 96)..(123, 127) <- fill(#781818)
(32, 104) <- 'Item 2'
(0, 144)..(123, 175) <- fill(#781818)
(32, 152) <- 'Item 3'
(0, 192)..(123, 223) <- fill(#781818)
(32, 200) <- 'Item 4'
(0, 240)..(123, 271) <- fill(#781818)
(32, 248) <- 'Item 5'
>>> menu.top
48
"""