move_hw_text.py 946 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import pygame, sys
  2. from keyboard import is_pressed
  3. #Initialize pygame and set up window
  4. pygame.init()
  5. winSize = (800, 600)
  6. screen = pygame.display.set_mode(winSize)
  7. pygame.display.set_caption('Move text')
  8. #Load font and render text
  9. myriadProFont = pygame.font.SysFont('Myriad Pro', 48)
  10. helloWorld = myriadProFont.render('Hello World!', 1, (255, 0, 255), (255, 255, 255))
  11. #Create x and y coordinate variables for displaying text on screen
  12. x, y = 0, 0
  13. #Add clock so that the next frame is displayed at an exact time
  14. clock = pygame.time.Clock()
  15. while True:
  16. if is_pressed('q'):
  17. sys.exit()
  18. clock.tick(40)
  19. #Keep screen open and let user exit window using for loop
  20. for ev in pygame.event.get():
  21. if ev.type == pygame.QUIT:
  22. sys.exit()
  23. screen.fill((0,0,0))
  24. screen.blit(helloWorld, (x, y))
  25. x += 6
  26. #Update window
  27. pygame.display.update()