full_screen.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. from pygame.locals import *
  3. import pygame
  4. # Resolution
  5. HEIGHT = 700
  6. WIDTH = 700
  7. pygame.init()
  8. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  9. pygame.display.set_caption('Fullscreen demo')
  10. WHITE = (255, 255, 255)
  11. GREEN = (0, 255, 0)
  12. # Creates the text
  13. font = pygame.font.Font('freesansbold.ttf', 22)
  14. fullscreen_str = 'Press F11 to change between full screen and normal mode'
  15. text_surf = font.render(fullscreen_str, True, WHITE, GREEN)
  16. text_rect = text_surf.get_rect()
  17. text_rect.center = (WIDTH / 2, HEIGHT / 2)
  18. # Draws the text
  19. screen.blit(text_surf, text_rect)
  20. fullscreen = False
  21. running = True
  22. while running:
  23. pygame.display.update()
  24. for event in pygame.event.get():
  25. if event.type == QUIT:
  26. pygame.quit()
  27. running = False
  28. elif event.type == KEYDOWN:
  29. if event.key == K_F11:
  30. if pygame.display.get_driver() == 'x11':
  31. pygame.display.toggle_fullscreen()
  32. else:
  33. screen_copy = screen.copy()
  34. if fullscreen:
  35. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  36. else:
  37. screen = pygame.display.set_mode((WIDTH, HEIGHT),
  38. pygame.FULLSCREEN)
  39. fullscreen = not fullscreen
  40. screen.blit(screen_copy, (0, 0))
  41. elif event.key == K_ESCAPE:
  42. running = False