Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

PyGame: Use SDL2 to render a pixel

$
0
0

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

import pygameimport pygame._sdl2SCREEN_W = 800SCREEN_H = 800pygame.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 greenclock = pygame.time.Clock()scale_factor = 1# Create a green surfacegreen_pixel = pygame.Surface((scale_factor, scale_factor))green_pixel.fill((0, 255, 0, 255))use_sdl2 = Truewhile 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.blitTypeError: 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?


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>