示例#1
0
def run ():
    drawtypes = [ draw_rect, draw_circle, draw_arc, draw_line, draw_aaline,
                  draw_lines, draw_aalines, draw_polygon, draw_aapolygon,
                  draw_ellipse ]
    curtype = 0
    video.init ()

    screen = video.set_mode (640, 480)
    screen.fill (white)
    draw_rect (screen)
    screen.flip ()

    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.MOUSEBUTTONDOWN:
                curtype += 1
                if curtype >= len (drawtypes):
                    curtype = 0
                screen.fill (white)
                drawtypes[curtype] (screen)
                screen.flip ()
    video.quit ()
示例#2
0
def run ():
    methods = [ draw_checked, draw_striped, draw_flipped, draw_mixed,
                draw_zoomed, draw_replaced, draw_extracted, draw_extracted2]
    curmethod = -1
    
    video.init ()
    screen = video.set_mode (320, 240, 32)
    screen.fill (black)
    
    surface = image.load_bmp (pygame2.examples.RESOURCES.get ("array.bmp"))
    surface = surface.convert (flags=sdlconst.SRCALPHA)
    screen.blit (surface)
    screen.flip ()

    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.MOUSEBUTTONDOWN:
                curmethod += 1
                if curmethod >= len (methods):
                    curmethod = 0
                screen.fill (black)
                screen.blit (surface)
                methods[curmethod](screen)
                screen.flip ()
    video.quit ()
示例#3
0
def run ():
    drawtypes = [ draw_aacircle, draw_circle, draw_filledcircle,
                  draw_box, draw_rectangle,
                  draw_arc,
                  draw_line, draw_aaline, draw_hline, draw_vline, 
                  draw_polygon, draw_aapolygon, draw_filledpolygon,
                  draw_texturedpolygon,
                  draw_ellipse, draw_aaellipse, draw_filledellipse,
                  draw_aatrigon, draw_trigon, draw_filledtrigon,
                  draw_bezier,
                  draw_pie, draw_filledpie, 
                  ]
    curtype = 0
    video.init ()

    screen = video.set_mode (640, 480, 32)
    screen.fill (white)
    drawtypes[0] (screen)
    screen.flip ()

    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.MOUSEBUTTONDOWN:
                curtype += 1
                if curtype >= len (drawtypes):
                    curtype = 0
                screen.fill (white)
                drawtypes[curtype] (screen)
                screen.flip ()
    video.quit ()
示例#4
0
def run():
    video.init ()
    freetype.init (8)

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

    screen = video.set_mode (800, 600)
    screen.fill (colors["grey_light"])

    sf, w, h = font.render("Hello World", colors["red"], colors['grey_dark'],
        ptsize=64, style=ftconstants.STYLE_UNDERLINE|ftconstants.STYLE_ITALIC)
    screen.blit (sf, (32, 32))

    font.render("abcdefghijklm", colors["grey_dark"], colors["green"],
                ptsize=64, dest=(screen, 32, 128))

    font.vertical = True
    font.render("Vertical?", colors["blue"], ptsize=32, dest=(screen, 32, 190))
    font.vertical = False

    font.render("Let's spin!", colors["red"], ptsize=48, rotation=55,
                dest=(screen, 64, 190))

    font.render("All around!", colors["green"], ptsize=48, rotation=-55,
                dest=(screen, 150, 270))

    font.render("and BLEND", pygame2.Color(255, 0, 0, 128), ptsize=64,
                dest=(screen, 250, 220))

    font.render("or BLAND!", pygame2.Color(0, 0xCC, 28, 128), ptsize=64,
                dest=(screen, 258, 237))

    text = "I \u2665 Unicode"
    if sys.version_info[0] < 3:
        text = "I " + unichr(0x2665) + " Unicode"
    font.render(text, pygame2.Color(0, 0xCC, 0xDD), ptsize=64,
                dest=(screen, 298, 320))
    
    text = "\u2665"
    if sys.version_info[0] < 3:
        text = unichr(0x2665)
    font.render(text, colors["grey_light"], colors["red"], ptsize=148,
                dest=(screen, 480, 32))

    font.render("...yes, this is a SDL surface", pygame2.Color(0, 0, 0),
                ptsize=24, style=ftconstants.STYLE_BOLD,
                dest=(screen, 380, 380))

    screen.flip ()

    okay = True
    while okay:
        for ev in event.get ():
            if ev.type == constants.QUIT:
                okay = False
            if ev.type == constants.KEYDOWN and ev.key == constants.K_ESCAPE:
                okay = False
    video.quit ()
示例#5
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 ()
示例#6
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 ()
示例#7
0
def run ():
    filltypes = [ fill_solid, fill_rgba,
                  fill_min, fill_rgba_min,
                  fill_max, fill_rgba_max,
                  fill_add, fill_rgba_add,
                  fill_sub, fill_rgba_sub,
                  fill_mult, fill_rgba_mult,
                  fill_and, fill_rgba_and,
                  fill_or, fill_rgba_or,
                  fill_xor, fill_rgba_xor,
                  fill_diff, fill_rgba_diff,
                  fill_screen, fill_rgba_screen,
                  fill_avg, fill_rgba_avg,
                  ]
    curtype = 0
    video.init ()
    
    screen = video.set_mode (760, 300, 32)
    surface = image.load_bmp (pygame2.examples.RESOURCES.get ("logo.bmp"))
    surface = surface.convert (flags=sdlconst.SRCALPHA)
    
    color = white
    screen.fill (color)
    screen.blit (surface, (40, 50))
    screen.flip ()

    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.MOUSEBUTTONDOWN:
                curtype += 1
                if curtype >= len (filltypes):
                    curtype = 0
                    if color == black:
                        color = white
                    else:
                        color = black

                screen.fill (color)
                screen.blit (surface, (40, 50))
                filltypes[curtype] (screen)
                screen.flip ()
    video.quit ()
示例#8
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 ()
示例#9
0
def run ():
    blittypes = [ blit_solid, blit_min, blit_max, blit_add, blit_sub,
                  blit_mult, blit_and, blit_or, blit_xor, blit_diff,
                  blit_screen, blit_avg,
                  blit_rgba, blit_rgba_min, blit_rgba_max, blit_rgba_add,
                  blit_rgba_sub, blit_rgba_mult, blit_rgba_and, blit_rgba_or,
                  blit_rgba_xor, blit_rgba_diff, blit_rgba_screen,
                  blit_rgba_avg ]
    curtype = 0
    video.init ()
    screen = video.set_mode (640, 480, 32)
    color = white
    imgdir = os.path.dirname (os.path.abspath (__file__))
    logo = None
    if hassdlimage:
        logo = image.load (pygame2.examples.RESOURCES.get ("logo.gif"))
    else:
        logo = image.load_bmp (pygame2.examples.RESOURCES.get ("logo.bmp"))
    
    screen.fill (color)
    screen.blit (logo, (-10, 140))
    blit_solid (screen)
    screen.flip ()

    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.MOUSEBUTTONDOWN:
                curtype += 1
                if curtype >= len (blittypes):
                    curtype = 0
                    if color == black:
                        color = white
                    else:
                        color = black

                screen.fill (color)
                screen.blit (logo, (-10, 140))
                blittypes[curtype] (screen)
                screen.flip ()
    video.quit ()
示例#10
0
def run():
    video.init()

    surface = None
    if hassdlimage:
        surface = image.load(pygame2.examples.RESOURCES.get("logo.gif"))
    else:
        surface = image.load_bmp(pygame2.examples.RESOURCES.get("logo.bmp"))

    screen = video.set_mode(surface.w + 10, surface.h + 10)
    screen.fill(pygame2.Color(255, 255, 255))
    screen.blit(surface, (5, 5))
    screen.flip()

    okay = True
    while okay:
        for ev in event.get():
            if ev.type == constants.QUIT:
                okay = False
            if ev.type == constants.KEYDOWN and ev.key == constants.K_ESCAPE:
                okay = False
    video.quit()
示例#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 keepLooping():
    for e in event.get():
        if (e.type == K.KEYUP and e.key == K.K_ESCAPE) \
        or (e.type == K.QUIT):
            return False
    return True
示例#13
0
def run ():
    video.init ()
    joystick.init ()

    if joystick.num_joysticks () == 0:
        print ("No joysticks found. Exiting.")
        return
    
    screen = video.set_mode (640, 480)
    screen.fill (white)
    screen.flip ()

    wm.set_caption ("Joystick demo")

    crect = pygame2.Rect (400, 300)
    srect = pygame2.Rect (640, 480)
    crect.center = srect.center
    joyrect = pygame2.Rect (4, 4)
    joyrect.center = srect.center

    joy = joystick.Joystick (0)

    btndict = {}
    xoff, yoff = 50, crect.top
    for i in range (joy.num_buttons):
        btndict[i] = [False, pygame2.Rect (xoff, yoff, 20, 20)]
        yoff += 25

    xpart = ((crect.width - joyrect.width) / 2) / 32768.0
    ypart = ((crect.height - joyrect.height) / 2) / 32768.0
    
    okay = True
    while okay:
        for ev in event.get ():
            if ev.type == sdlconst.QUIT:
                okay = False
            elif ev.type == sdlconst.KEYDOWN and ev.key == sdlconst.K_ESCAPE:
                okay = False
            elif ev.type == sdlconst.JOYAXISMOTION and ev.joy == joy.index:
                if ev.axis == 0:
                    # x axis movement
                    if ev.value == 0:
                        joyrect.centerx = crect.centerx
                    else:
                        joyrect.centerx = crect.centerx + ev.value * xpart
                elif ev.axis == 1:
                    # y axis movement
                    if ev.value == 0:
                        joyrect.centery = crect.centery
                    else:
                        joyrect.centery = crect.centery + ev.value * ypart

            elif ev.type == sdlconst.JOYBUTTONDOWN and ev.joy == joy.index:
                btndict[ev.button][0] = True
            elif ev.type == sdlconst.JOYBUTTONUP and ev.joy == joy.index:
                btndict[ev.button][0] = False
            elif ev.type == sdlconst.JOYHATMOTION and ev.joy == joy.index:
                pass
            elif ev.type == sdlconst.JOYBALLMOTION and ev.joy == joy.index:
                pass

            draw.rect (screen, black, crect)
            draw.line (screen, white, crect.midtop, crect.midbottom)
            draw.line (screen, white, crect.midleft, crect.midright)
            draw.rect (screen, green, joyrect)

            for (state, rect) in btndict.values():
                if state:
                    draw.rect (screen, green, rect)
                else:
                    draw.rect (screen, black, rect)
            
            screen.flip ()
    video.quit ()