Exemple #1
0
def sprites():
    pg.init()
    clock = pg.time.Clock()
    window = Window("Renderpyg Example", size=WINDOW_RESOLUTION)
    renderer = Renderer(window, vsync=True)
    """ 
	We will draw into a buffer texture to allow easy resolution changes
	It will also make it easier to apply screen transitions and similar effects later

	When using pygame._sdl2.video you do not call pygame.display.setmode()
	Therefore calling surface.convert() or surface.convert_alpha() will throw an error
	When you create a Texture that needs alpha blending you must set its blend mode
	Alpha blending will be set automatically when loading from an image with transparency, such as PNG

	Remember to use the buffer size instead of the window size when drawing onto the offscreen buffer
	This will allow you to scale the screen to any window or fullscreen desktop size
	"""
    buffer = Texture(renderer, RENDER_RESOLUTION, target=True)
    buffer.blend_mode = 1
    screensize = buffer.get_rect()
    """ 
	You can set fullscreen when creating the window by using Window(title, size, desktop_fullscreen=True)
	I prefer creating a window before going to fullscreen to avoid strange window placement that occurs
	if you exit fullscreen later on.
	"""
    FULLSCREEN = False
    if FULLSCREEN:
        window.set_fullscreen(True)
    """
	Font features in pygame are design for blitting to a surface, not for GPU rendering
	It is possible to create a streaming texture and then using texture.update() to update the texture
	from a pygame surface, but accessing GPU memory is slow and this should be done sparingly.

	Therefore I created a simple TextureFont class. We will use the animation feature of this class
	for a little extra fun. We will also create some sprites and let them animate too.

	Also, for this example we use a Character class to move and rotate individual characters across
	the screen. This is very similar to how you will handle sprites later.
	"""
    tfont = TextureFont(renderer, FONT, FONT_SIZE)
    sprite = Sprite((renderer, EXAMPLE_DATA + 'aliens.png'),
                    7,
                    8,
                    by_count=True)
    group = pg.sprite.Group()
    animations = [
        keyrange(0, 7, 200),
        keyrange(7, 14, 200),
        keyrange(14, 21, 200),
        keyrange(28, 35, 200)
    ]

    for _ in range(SPRITE_COUNT):
        spr = Sprite(sprite.images)
        spr.set_pos(randrange(0, RENDER_RESOLUTION[0]),
                    randrange(0, RENDER_RESOLUTION[1]))
        spr.set_animation(random.choice(animations), -1)
        spr.velocity = pg.Vector2(randrange(-10, 11), randrange(-10, 11))
        if randrange(10) < 2:
            spr.rotation = randrange(-10, 11)
        group.add(spr)
    """ 
	Here starts a simple game loop
	Press SPACE to toggle between a large window, a small window, and fullscreen
	Press ENTER to add more characters to the screen

	At the beginning of each frame we must set the renderer target to our buffer Texture
	All the following draw calls will be drawn to the buffer instead of the screen
	After all of our drawing, we reset the target and draw the buffer onto the screen
	"""
    timer = pg.time.get_ticks()
    delta = 0
    running = True
    while running:
        renderer.target = buffer
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    running = False
                elif event.key == pg.K_SPACE:
                    if FULLSCREEN:
                        FULLSCREEN = False
                        window.size = WINDOW_RESOLUTION
                        window.set_windowed()
                    elif window.size == WINDOW_RESOLUTION:
                        window.size = SMALL_RESOLUTION
                    else:
                        FULLSCREEN = True
                        window.size = WINDOW_RESOLUTION
                        window.set_fullscreen(True)

        #Must set the draw color before clearing the scren or drawing lines and rectt

        renderer.draw_color = (0, 0, 0, 255)
        renderer.clear()
        """
		Draw the background image if available. 
		By default Texture.draw() will fill the renderer unless you supply a destination Rect
		texture.draw( dstrect=Rect(x, y, width, height) )
		"""
        group.update(delta)
        group.draw()
        tfont.animate(**FONT_PARAMS)

        # Setting renderer.target = None will make following draw calls render to the underlying window
        # Since we don't provide a dstrect it will fill the renderer

        renderer.target = None
        buffer.draw()
        renderer.present(
        )  # all draw calls occur and the screen is updated here
        delta = clock.tick(FRAMES_PER_SECOND)
Exemple #2
0
               event.type == pygame.WINDOWEVENT and event.event == pygame.WINDOWEVENT_CLOSE:
                win2.destroy()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_LEFT:
                x -= 5
            elif event.key == pygame.K_RIGHT:
                x += 5
            elif event.key == pygame.K_DOWN:
                y += 5
            elif event.key == pygame.K_UP:
                y -= 5
            elif event.key == pygame.K_f:
                if full == 0:
                    win.set_fullscreen(True)
                    full = 1
                else:
                    win.set_windowed()
                    full = 0
            elif event.key == pygame.K_SPACE:
                bg_index = (bg_index + 1) % len(backgrounds)
                renderer.draw_color = backgrounds[bg_index]

    renderer.clear()
    renderer.copy_pos(tex, x, y)
    renderer.present()

    clock.tick(60)
    win.title = str('FPS: {}'.format(clock.get_fps()))
Exemple #3
0
def main():
    import os
    from .tfont import TextureFont
    example_data = os.path.join(os.path.dirname(__file__), 'data', '')
    pg.init()
    window = Window('Testing', (1600, 900))
    renderer = Renderer(window)
    #tilemap = load_tmx(renderer, path+'tilemap.tmx')
    clock = pg.time.Clock()
    running = True
    camera = pg.Vector2()
    center = True
    scale = 1
    tfont = TextureFont(renderer, None, 48)
    loaded_map = load_tilemap_string(map_data)
    loaded_cells = load_tileset(renderer, example_data + 'tiles.png', 64, 64)
    tilemap = Tilemap(loaded_map, loaded_cells)
    tilemap.update_tilemap(loaded_map, 0)
    tilemap.add_layer(loaded_map)

    scaling = 0

    mode = 0
    while running:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    running = False
                elif event.key == pg.K_f:
                    window_mode = window_mode + 1 if window_mode < 3 else 1
                    if window_mode == 1:
                        window.size = WINDOW_RESOLUTION
                        window.set_windowed()
                    elif window_mode == 2:
                        window.size = SMALL_RESOLUTION
                    else:
                        window.set_fullscreen(True)
                elif event.key == pg.K_t:
                    mode = not mode
            elif event.type == pg.MOUSEMOTION:
                x, y = pg.mouse.get_rel()
                if pg.mouse.get_pressed()[0]:
                    camera.x -= x * 2
                    camera.y -= y * 2
            elif event.type == pg.MOUSEBUTTONUP:
                if event.button == 3:
                    center = not center
                elif event.button == 4:
                    scaling += 0.001
                elif event.button == 5:
                    scaling -= 0.001

        scale += scaling
        render_tilemap(tilemap,
                       camera,
                       scale,
                       center=center,
                       clamp=True,
                       smooth=True)
        tfont.draw(f'cam: {camera}, center: {center}, {scale}', 10, 10)
        tfont.draw(printfo, 10, 60)
        renderer.present()
        renderer.draw_color = (255, 0, 0, 0)
        renderer.clear()
        clock.tick(5)