Пример #1
0
def draw_texturedpolygon (screen):
    wm.set_caption ("primitives.textured_polygon examples")
    tex = image.load_bmp (pygame2.examples.RESOURCES.get ("logo.bmp"))
    
    primitives.textured_polygon (screen, ((4, 40), (280, 7), (40, 220),
                                          (33, 237), (580, 370), (0, 0),
                                          (640, 480)), tex, 10, 30)
Пример #2
0
def run ():
    black = pygame2.Color (0, 0, 0)
    white = pygame2.Color (255, 255, 255)
    green = pygame2.Color (0, 255, 0)
    red = pygame2.Color (255, 0, 0)
    
    curcolor = black
    pressed = False
    lastpos = 0, 0
    
    video.init ()
    screen = video.set_mode (640, 480)
    
    screen.fill (white)
    screen.flip ()

    wm.set_caption ("Mouse demo")

    okay = True
    while okay:
        for ev in event.get ():
            if ev.type == sdlconst.QUIT:
                okay = False
            if ev.type == sdlconst.KEYDOWN and ev.key == sdlconst.K_ESCAPE:
                okay = False
            if ev.type == sdlconst.MOUSEMOTION:
                if pressed:
                    x, y = ev.pos
                    lastpos = ev.pos
                    screen.fill (curcolor, pygame2.Rect (x - 2, y - 2, 5, 5))
                    screen.flip ()
            elif ev.type == sdlconst.MOUSEBUTTONDOWN:
                if ev.button == 1:
                    pressed = True
                elif ev.button == 4:
                    x, y = lastpos[0], lastpos[1] - 2
                    screen.fill (green, pygame2.Rect (x - 2, y - 2, 5, 5))
                    lastpos = x, y
                    screen.flip ()
                elif ev.button == 5:
                    x, y = lastpos[0], lastpos[1] + 2
                    screen.fill (red, pygame2.Rect (x - 2, y - 2, 5, 5))
                    lastpos = x, y
                    screen.flip ()
            elif ev.type == sdlconst.MOUSEBUTTONUP:
                if ev.button == 1:
                    pressed = False
                elif ev.button == 3:
                    if curcolor == white:
                        curcolor = black
                        screen.fill (white)
                    else:
                        curcolor = white
                        screen.fill (black)
                    screen.flip ()
    video.quit ()
Пример #3
0
def run ():
    video.init ()
    freetype.init ()

    font = freetype.Font (pygame2.examples.RESOURCES.get ("sans.ttf"))

    fpsmanager = sdlgfx.FPSmanager (2)

    screen = video.set_mode (640, 480)
    wm.set_caption ("FPSmanager example")
    screenrect = pygame2.Rect (640, 480)
    screen.fill (black)
    screen.flip ()

    okay = True
    while okay:
        for ev in event.get ():
            if ev.type == sdlconst.QUIT:
                okay = False
            if ev.type == sdlconst.KEYDOWN:
                framerate = fpsmanager.framerate
                if ev.key == sdlconst.K_ESCAPE:
                    okay = False
                elif ev.key in (sdlconst.K_PLUS, sdlconst.K_KP_PLUS):
                    framerate = min (framerate + 1, gfxconst.FPS_UPPER_LIMIT)
                    fpsmanager.framerate = framerate
                elif ev.key in (sdlconst.K_MINUS, sdlconst.K_KP_MINUS):
                    framerate = max (framerate - 1, gfxconst.FPS_LOWER_LIMIT)
                    fpsmanager.framerate = framerate

        screen.fill (black)

        prev = time.time ()
        fpsmanager.delay ()
        last = time.time ()

        millis = ((last - prev) * 1000)
        fpstext = "FPS: %d" % fpsmanager.framerate
        timetext = "time (ms) passed since last update: %.3f" % millis
                   
        surfacef, w, h = font.render (fpstext, white, ptsize=28)
        surfacet, w2, h2 = font.render (timetext, white, ptsize=28)
        blitrect = pygame2.Rect (w, h)
        blitrect.center = screenrect.centerx, screenrect.centery - h
        screen.blit (surfacef, blitrect.topleft)
        blitrect = pygame2.Rect (w2, h2)
        blitrect.center = screenrect.centerx, screenrect.centery + h
        screen.blit (surfacet, blitrect.topleft)
        screen.flip ()
    
    video.quit ()
Пример #4
0
def draw_zoomed (screen):
    wm.set_caption ("2x zoom")
    pxarray = PixelArray (screen)
    
    # Temporary array.
    sf = video.Surface (640, 480, 32)
    tmparray = PixelArray (sf)
    tmparray[::2, ::2] = pxarray[:]
    tmparray[1::2, ::2] = pxarray[:-1]
    tmparray[:, 1::2] = tmparray[:, :-1:2]
    
    pxarray[:] = tmparray[80:400, 80:320]
    del tmparray
    del sf
    del pxarray
Пример #5
0
def run ():
    white = pygame2.Color (255, 255, 255)
    black = pygame2.Color (0, 0, 0)

    fontmap = [ "0123456789",
                "ABCDEFGHIJ",
                "KLMNOPQRST",
                "UVWXYZ    ",
                "abcdefghij",
                "klmnopqrst",
                "uvwxyz    ",
                ",;.:!?-+()" ]

    video.init ()

    imgfont = image.load_bmp (pygame2.examples.RESOURCES.get ("font.bmp"))
    bmpfont = BitmapFont (imgfont, (32, 32), fontmap)
    
    screen = video.set_mode (640, 480)
    screen.fill (white)
    
    center = (320 - bmpfont.surface.w / 2, 10)
    screen.blit (bmpfont.surface, center)
    screen.flip ()

    wm.set_caption ("Keyboard demo")

    x = 0, 0
    pos = (310, 300)
    area = pygame2.Rect (300, 290, 50, 50)

    okay = True
    while okay:
        for ev in event.get ():
            if ev.type == sdlconst.QUIT:
                okay = False
            if ev.type == sdlconst.KEYDOWN:
                if ev.key == sdlconst.K_ESCAPE:
                    okay = False
                elif bmpfont.contains (ev.unicode):
                    screen.fill (white)
                    screen.fill (black, area)
                    screen.blit (bmpfont.surface, center)
                    bmpfont.render_on (screen, ev.unicode, pos)
                    screen.flip ()
    video.quit ()
Пример #6
0
    def test_pygame2_sdl_wm_set_caption(self):

        # __doc__ (as of 2010-01-13) for pygame2.sdl.wm.set_caption:

        # set_caption (str) -> None
        # 
        # Sets the caption of the current SDL window.

        # handled in wm_get_caption()
        self.assertRaises (pygame2.Error, wm.set_caption, "test", "test")
        sf = video.set_mode (10, 10)
        self.assertEqual (wm.get_caption (), (None, None))
        self.assertTrue (wm.set_caption ("test window") == None)
        self.assertEqual (wm.get_caption (), ("test window", None))
        self.assertTrue (wm.set_caption ("", "icon") == None)
        self.assertEqual (wm.get_caption (), ("", "icon"))
        self.assertTrue (wm.set_caption ("test window", "icon") == None)
        self.assertEqual (wm.get_caption (), ("test window", "icon"))
Пример #7
0
    def test_pygame2_sdl_wm_get_caption(self):

        # __doc__ (as of 2010-01-13) for pygame2.sdl.wm.get_caption:

        # get_caption () -> str
        # 
        # Gets the caption of the current SDL window.
        self.assertRaises (pygame2.Error, wm.get_caption)
        sf = video.set_mode (10, 10)
        self.assertEqual (wm.get_caption (), (None, None))
        wm.set_caption ("test window")
        self.assertEqual (wm.get_caption (), ("test window", None))
        wm.set_caption ("", "icon")
        self.assertEqual (wm.get_caption (), ("", "icon"))
        wm.set_caption ("test window", "icon")
        self.assertEqual (wm.get_caption (), ("test window", "icon"))
Пример #8
0
def draw_checked (screen):
    wm.set_caption ("Manipulating every second pixel in every second row")
    pxarray = PixelArray (screen)
    pxarray[::2,::2] = white
    del pxarray
Пример #9
0
def draw_ellipse (screen):
    wm.set_caption ("draw.ellipse examnples")
    draw.ellipse (screen, black, pygame2.Rect (20, 20, 100, 100), 4)
    draw.ellipse (screen, red, pygame2.Rect (160, 100, 160, 60))
    draw.ellipse (screen, green, pygame2.Rect (180, 190, 180, 200))
    draw.ellipse (screen, blue, pygame2.Rect (390, 120, 200, 140), 7)
Пример #10
0
def draw_filledcircle (screen):
    wm.set_caption ("primitives.filled_circle examples")
    primitives.filled_circle (screen, (100, 200), 45, black)
    primitives.filled_circle (screen, (200, 160), 80, red)
    primitives.filled_circle (screen, (370, 210), 100, green)
    primitives.filled_circle (screen, (400, 400), 45, blue)
Пример #11
0
def run ():
    freetype.init ()
    video.init ()
    sdlmixer.init ()
    sdlmixer.open_audio (sdlmixerconst.DEFAULT_FREQUENCY,
                         sdlmixerconst.DEFAULT_FORMAT,
                         sdlmixerconst.DEFAULT_CHANNELS,
                         1024)
    print ("Detected decoders: %s" % sdlmixer.get_decoders ())

    sound = sdlmixer.Chunk (pygame2.examples.RESOURCES.get ("house_lo.wav"))
    channel_sound = sdlmixer.Channel (1)

    font = freetype.Font (pygame2.examples.RESOURCES.get ("sans.ttf"))
    surfaces = get_help (font)

    screen = video.set_mode (640, 480)
    wm.set_caption ("SDL_mixer sound example")

    screenrect = pygame2.Rect (640, 480)
    screen.fill (black)
    yoff = 100
    for (sf, w, h) in surfaces:
        screen.blit (sf, (100, yoff))
        yoff += h + 10
    screen.flip ()

    okay = True
    while okay:
        for ev in event.get ():
            if ev.type == sdlconst.QUIT:
                okay = False
            if ev.type == sdlconst.KEYDOWN:
                # play, pause, resume
                if ev.key == sdlconst.K_SPACE:
                    if channel_sound.paused:
			print ("Resuming")
                        channel_sound.resume ()
                    elif channel_sound.playing:
			print ("Pausing")
                        channel_sound.pause ()
                    else:
			print ("Starting")
                        channel_sound.play (sound, -1)
                if ev.key == sdlconst.K_ESCAPE:
                    # exit the application
                    okay = False
                elif ev.key in (sdlconst.K_PLUS, sdlconst.K_KP_PLUS):
                    # increase volume
                    channel_sound.volume = min (channel_sound.volume + 1,
                                                sdlmixerconst.MAX_VOLUME)
                    print ("Volume is now: %d" % channel_sound.volume)
                elif ev.key in (sdlconst.K_MINUS, sdlconst.K_KP_MINUS):
                    # decrease volume
                    channel_sound.volume = max (channel_sound.volume - 1, 0)
                    print ("Volume is now: %d" % channel_sound.volume)

        screen.flip ()

    freetype.quit ()
    video.quit ()
    sdlmixer.close_audio ()
    sdlmixer.quit ()
Пример #12
0
def draw_replaced (screen):
    wm.set_caption ("Replacing colors")
    pxarray = PixelArray (screen)
    pxarray.replace (black, white, 0.06)
    pxarray.replace (red, green, 0)
    del pxarray
Пример #13
0
def draw_mixed (screen):
    wm.set_caption ("Manipulating parts")
    pxarray = PixelArray (screen)
    pxarray[::2, :120:2] = pxarray[-1, -1]
    pxarray[::6, 120::] = red
    del pxarray
Пример #14
0
def draw_rect (screen):
    wm.set_caption ("draw.rect examples")
    draw.rect (screen, black, pygame2.Rect (20, 20, 100, 100))
    draw.rect (screen, red, pygame2.Rect (160, 100, 160, 60))
    draw.rect (screen, green, pygame2.Rect (180, 190, 180, 200))
    draw.rect (screen, blue, pygame2.Rect (390, 120, 200, 140))
Пример #15
0
def draw_hline (screen):
    wm.set_caption ("primitives.hline examples")
    off = 0
    for y in range (0, 480, 4):
        primitives.hline (screen, 10, 630, y, colors[off])
        off = (off + 1) % 4
Пример #16
0
def draw_filledpolygon (screen):
    wm.set_caption ("primitives.filled_polygon examples")
    primitives.filled_polygon (screen, ((4, 40), (280, 7), (40, 220),
                                        (33, 237), (580, 370),
                                        (0, 0), (640, 480)), black)
Пример #17
0
def draw_aaline (screen):
    wm.set_caption ("primitives.aaline examples")
    primitives.aaline (screen, 4, 40, 17, 320, black)
    primitives.aaline (screen, 280, 7, 40, 220, red)
    primitives.aaline (screen, 33, 237, 580, 370, green)
    primitives.aaline (screen, 0, 0, 640, 480, blue)
Пример #18
0
def draw_arc (screen):
    wm.set_caption ("primitives.arc examples")
    primitives.arc (screen, (80, 100), 75, 0, 30, black)
    primitives.arc (screen, (100, 200), 123, 4, 70, red)
    primitives.arc (screen, (300, 300), 78, 200, 30, green)
    primitives.arc (screen, (400, 80), 55, 10, 360, blue)
Пример #19
0
def draw_rectangle (screen):
    wm.set_caption ("primitves.rectangle examples")
    primitives.rectangle (screen, pygame2.Rect (20, 20, 100, 100), black)
    primitives.rectangle (screen, pygame2.Rect (160, 100, 160, 60), red)
    primitives.rectangle (screen, pygame2.Rect (180, 190, 180, 200), green)
    primitives.rectangle (screen, pygame2.Rect (390, 120, 200, 140), blue)
Пример #20
0
def draw_striped (screen):
    wm.set_caption ("Manipulating every third column")
    pxarray = PixelArray (screen)
    pxarray[::3] = white
    del pxarray
Пример #21
0
def draw_flipped (screen):
    wm.set_caption ("Flipping around x and y")
    pxarray = PixelArray (screen)
    pxarray[:] = pxarray[::-1, ::-1]
    del pxarray
Пример #22
0
def draw_circle (screen):
    wm.set_caption ("draw.circle examples")
    draw.circle (screen, black, (100, 100), 50)
    draw.circle (screen, red, (200, 160), 80, 4)
    draw.circle (screen, green, (370, 210), 100, 12)
    draw.circle (screen, blue, (400, 400), 45, 40)
Пример #23
0
def draw_filledpie (screen):
    wm.set_caption ("primitives.filled_pie examples")
    primitives.filled_pie (screen, (100, 200), 45, 0, 90, black)
    primitives.filled_pie (screen, (200, 160), 80, 45, 135, red)
    primitives.filled_pie (screen, (370, 210), 100, 89, 217, green)
    primitives.filled_pie (screen, (400, 400), 45, 10, 350, blue)
Пример #24
0
def draw_arc (screen):
    wm.set_caption ("draw.arc examples")
    draw.arc (screen, black, pygame2.Rect (80, 100, 100, 100), 0, 30)
    draw.arc (screen, red, pygame2.Rect (70, 200, 300, 150), 4, 70)
    draw.arc (screen, green, pygame2.Rect (40, 20, 100, 150), 7, 230)
    draw.arc (screen, blue, pygame2.Rect (400, 80, 200, 300), 10, 360)
Пример #25
0
def draw_extracted2 (screen):
    wm.set_caption ("Extracting colors (black, 50% match)")
    pxarray = PixelArray (screen)
    pxarray[:] = pxarray.extract (black, 0.5)
    del pxarray
Пример #26
0
def draw_bezier (screen):
    wm.set_caption ("primitives.bezier examples")
    primitives.bezier (screen, ((10, 10), (420, 40), (60, 200)), 50, black)
    primitives.bezier (screen, ((240, 100), (370, 300), (40, 300)), 30, red)
    primitives.bezier (screen, ((300, 400), (620, 270), (470, 200)), 10, green)
    primitives.bezier (screen, ((33, 400), (440, 460), (312, 410)), 3, blue)
Пример #27
0
def draw_aaline (screen):
    wm.set_caption ("draw.aaline examples")
    draw.aaline (screen, black, 4, 40, 17, 320, True)
    draw.aaline (screen, red, 280, 7, 40, 220, True)
    draw.aaline (screen, green, 33, 237, 580, 370, True)
    draw.aaline (screen, blue, 0, 0, 640, 480, False)
Пример #28
0
def draw_aapolygon (screen):
    wm.set_caption ("draw.aapolygon examples")
    draw.aapolygon (screen, black, ((4, 40), (280, 7), (40, 220),
                                    (33, 237), (580, 370),
                                    (0, 0), (640, 480)), 4)
Пример #29
0
def draw_line (screen):
    wm.set_caption ("draw.line examples")
    draw.line (screen, black, 4, 40, 17, 320)
    draw.line (screen, red, 280, 7, 40, 220, 4)
    draw.line (screen, green, 33, 237, 580, 370, 8)
    draw.line (screen, blue, 0, 0, 640, 480, 16)
Пример #30
0
def draw_vline (screen):
    wm.set_caption ("primitives.vline examples")
    off = 0
    for x in range (0, 640, 4):
        primitives.vline (screen, x, 10, 470, colors[off])
        off = (off + 1) % 4