Ejemplo n.º 1
0
class ScreenTest(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        set_root(__file__)

    def setUp(self):
        surf.fill((0, 0, 0))
        self.screen = Screen(surf)

    def assertImagesEqual(self, a, b):
        adata, bdata = (pygame.image.tostring(i, 'RGB') for i in (a, b))

        if adata != bdata:
            raise AssertionError("Images differ")

    def test_blit_surf(self):
        """We can blit a surface to the screen."""
        self.screen.blit(images.alien, (0, 0))
        self.assertImagesEqual(surf, images.expected_alien_blit)

    def test_blit_name(self):
        """screen.blit() accepts an image name instead of a Surface."""
        self.screen.blit('alien', (0, 0))
        self.assertImagesEqual(surf, images.expected_alien_blit)
Ejemplo n.º 2
0
class ScreenTest(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        set_root(__file__)

    def setUp(self):
        self.screen = Screen(surf)
        self.screen.clear()

    def assertImagesAlmostEqual(self, a, b):
        """Check that 2 images are equal besides 1 bit alpha blending rounding errors"""
        zdata = zip(pygame.image.tostring(a, 'RGB'),
                    pygame.image.tostring(b, 'RGB'))

        for z in zdata:
            if abs(z[0] - z[1]) > 1:
                raise AssertionError("Images differ")

    def test_blit_surf(self):
        """We can blit a surface to the screen."""
        self.screen.blit(images.alien, (0, 0))
        self.assertImagesAlmostEqual(surf, images.expected_alien_blit)

    def test_blit_name(self):
        """screen.blit() accepts an image name instead of a Surface."""
        self.screen.blit('alien', (0, 0))
        self.assertImagesAlmostEqual(self.screen.surface,
                                     images.expected_alien_blit)

    def test_bounds(self):
        """test that the bounds method is present / works and that the return
        value is minimally correct (top-left should equal 0, bottom-right
        greater than 0)"""
        b = self.screen.bounds()
        self.assertEqual(b.left, 0)
        self.assertEqual(b.top, 0)
        self.assertGreater(b.width, 0)
        self.assertGreater(b.height, 0)
Ejemplo n.º 3
0
 def setUp(self):
     self.screen = Screen(self.surf)
     self.screen.clear()
Ejemplo n.º 4
0
class ScreenTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        """Initialise the display and set loaders to target the current dir."""
        pygame.init()
        cls.surf = pygame.display.set_mode((200, 100))
        set_root(__file__)

    @classmethod
    def tearDownClass(cls):
        """Shut down the display."""
        pygame.display.quit()

    def setUp(self):
        self.screen = Screen(self.surf)
        self.screen.clear()

    def assertImagesAlmostEqual(self, computed, expected):
        """Check that 2 images are approximately equal."""
        comp_surf = pygame.surfarray.array3d(computed)
        exp_surf = pygame.surfarray.array3d(expected)

        if np.allclose(comp_surf, exp_surf, atol=2):
            return

        name = sys._getframe(1).f_code.co_name
        tmpdir = Path(__file__).parent / 'failed-image'
        tmpdir.mkdir(exist_ok=True)
        pygame.image.save(
            computed,
            str(tmpdir / '{}-computed.png'.format(name))
        )
        pygame.image.save(
            expected,
            str(tmpdir / '{}-expected.png'.format(name))
        )

        raise AssertionError(
            "Images differ; saved comparison images to {}".format(tmpdir)
        )

    def test_blit_surf(self):
        """We can blit a surface to the screen."""
        self.screen.blit(images.alien, (0, 0))
        self.assertImagesAlmostEqual(self.surf, images.expected_alien_blit)

    def test_blit_name(self):
        """screen.blit() accepts an image name instead of a Surface."""
        self.screen.blit('alien', (0, 0))
        self.assertImagesAlmostEqual(
            self.screen.surface,
            images.expected_alien_blit
        )

    def test_bounds(self):
        """test that the bounds method is present / works and that the return
        value is minimally correct (top-left should equal 0, bottom-right
        greater than 0)"""
        b = self.screen.bounds()
        self.assertEqual(b.left, 0)
        self.assertEqual(b.top, 0)
        self.assertGreater(b.width, 0)
        self.assertGreater(b.height, 0)

    def test_fill_gradient(self):
        """We can fill the screen with a gradient."""
        self.screen.fill('black', gcolor='blue')
        self.assertImagesAlmostEqual(
            self.screen.surface,
            images.expected_gradient
        )

    def test_polygon(self):
        poly = [(0, 99), (49, 0), (99, 99)]
        yellow = (255, 255, 0)
        """We can draw a polygon."""
        self.screen.draw.polygon(poly, yellow)
        self.assertImagesAlmostEqual(
            self.screen.surface,
            images.expected_polygon
        )

    def test_filled_polygon(self):
        poly = [(0, 99), (49, 0), (99, 99)]
        yellow = (255, 255, 0)
        """We can draw a polygon."""
        self.screen.draw.filled_polygon(poly, yellow)
        self.assertImagesAlmostEqual(
            self.screen.surface,
            images.expected_filled_polygon
        )

    def test_polygon_errors(self):
        """draw.polygon raises errors as expected."""
        yellow = (255, 255, 0)
        with self.assertRaises(TypeError):
            self.screen.draw.polygon(2, yellow)
        with self.assertRaises(TypeError):
            self.screen.draw.polygon([2], yellow)


    def test_wrapped_gradient_text(self):
        """We can draw wrapped gradient text.

        Relates to issue #165 https://github.com/lordmauve/pgzero/issues/165

        """
        self.screen.draw.text(
            'gradient\ntext',
            (0, 0),
            fontname='eunomia_regular',
            fontsize=18,
            color='red',
            gcolor='blue'
        )
        self.assertImagesAlmostEqual(
            self.screen.surface,
            images.expected_wrapped_gradient_text
        )
Ejemplo n.º 5
0
 def setUp(self):
     self.screen = Screen()
     self.screen._set_surface(self.surf)
     self.screen.clear()
Ejemplo n.º 6
0
 def setUp(self):
     surf.fill((0, 0, 0))
     self.screen = Screen(surf)
Ejemplo n.º 7
0
A Game by Peter Nielsen (@peternielsen112)
This game is licensed under a GPL-3.0 License.
For How to Play, see the README.md file.
If you encounter a bug or have a suggestion, see README.md for instructions.
'''

# import statements
import pgzrun
import random
import pygame
import sys
import time
from pgzero.screen import Screen
from pgzero.builtins import Actor, keyboard

screen = Screen()

# Constants
WIDTH = 585
HEIGHT = 700
SPEED = 5
tie_speed = 9
tiestart = (random.randint(1, 250))
if len(sys.argv) >= 2:
    shipchoice = sys.argv[1]
else:
    shipchoice = 'xwing'

# music
pygame.mixer.music.load('theme.mp3')
pygame.mixer.music.play(loops=-1)