Advertisement
Windspar

Pygame Test code showing pygame draw commands vs surfaces with alpha

Jan 31st, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | Gaming | 0 0
  1. import pygame
  2.  
  3. # Draw commands don't use alpha the same way as surfaces.
  4.  
  5. def main(caption, size, flags=0):
  6.     pygame.display.set_caption(caption)
  7.     display = pygame.display.set_mode(size, flags)
  8.     drect = display.get_rect()
  9.     clock = pygame.time.Clock()
  10.     running = True
  11.     delta = 0
  12.     fps = 60
  13.  
  14.     color = pygame.Color('green')
  15.     color.a = 80
  16.     rcolor = pygame.Color('red')
  17.     rcolor.a = 150
  18.  
  19.     red_box = pygame.Surface((40, 40), pygame.SRCALPHA)
  20.     red_box.fill(rcolor)
  21.     green_box = pygame.Surface((700, 100), pygame.SRCALPHA)
  22.     green_box.fill(color)
  23.     pygame.draw.rect(green_box, rcolor, (10, 10, 40, 40))
  24.     green_box.blit(red_box, (120, 10))
  25.  
  26.     while running:
  27.         for event in pygame.event.get():
  28.             if event.type == pygame.QUIT:
  29.                 running = False
  30.  
  31.         display.fill('black')
  32.         pygame.draw.line(display, 'dodgerblue', (drect.centerx, 5), (drect.centerx, 400), 3)
  33.         display.blit(green_box, (50, 150))
  34.         pygame.draw.rect(display, color, (50, 20, 700, 100))
  35.  
  36.         pygame.display.flip()
  37.         delta = clock.tick(fps)
  38.  
  39. pygame.init()
  40. main('Testing', (800, 600))
  41. pygame.quit()
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement