1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] PyGame: Use SDL2 to render a pixel

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I want to render a singular green pixel in PyGame with SDL2. This is my current code:

    import pygame
    import pygame._sdl2

    SCREEN_W = 800
    SCREEN_H = 800

    pygame.init()

    pygame_screen = pygame.display.set_mode((SCREEN_W, SCREEN_H), vsync=0, flags=pygame.SCALED)
    window = pygame._sdl2.Window.from_display_module()

    renderer = pygame._sdl2.Renderer.from_window(window)
    renderer.draw_color = (0, 255, 0, 255) # Set the draw color to green

    clock = pygame.time.Clock()

    scale_factor = 1

    # Create a green surface
    green_pixel = pygame.Surface((scale_factor, scale_factor))
    green_pixel.fill((0, 255, 0, 255))

    use_sdl2 = True

    while True:
    msec = clock.tick(60)
    pygame_screen.fill((0, 0, 0))

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    quit()

    if use_sdl2:
    renderer.clear()
    dest_rect = pygame.rect.Rect(100, 100, scale_factor, scale_factor)
    renderer.blit(green_pixel, dest_rect)
    renderer.present()
    else:
    dest_rect = pygame.rect.Rect(100, 100, scale_factor, scale_factor)
    pygame_screen.blit(green_pixel, dest_rect)
    pygame.display.flip()


    This gives the following error:

    Traceback (most recent call last):
    File "/home/harald-yoga/Documents/Projects/Python/game-engine/src/game_engine/main.py", line 37, in <module>
    renderer.blit(green_pixel, dest_rect)
    File "src_c/cython/pygame/_sdl2/video.pyx", line 1166, in pygame._sdl2.video.Renderer.blit
    File "src_c/cython/pygame/_sdl2/video.pyx", line 1179, in pygame._sdl2.video.Renderer.blit
    TypeError: source must be drawable



    The non-SDL2 version (use_sdl2 = False) works fine and renders a green pixel at x: 100, y: 100. What am I doing wrong with the SDL version?

    Continue reading...

Compartilhe esta Página