Advertisement
cookertron

Kostik Iln - pygame.quit()

May 18th, 2022
632
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import pygame
  3. from pygame.locals import *
  4. from pygame import Rect as R
  5. from pygame import Vector2 as V
  6. from random import randint
  7.  
  8. BLACK = (15, 56, 15)
  9. WHITE = (139, 172, 15)
  10.  
  11. pygame.init()
  12. PDR = R(0, 0, 320, 240)
  13. PDS = pygame.display.set_mode(PDR.size)#, FULLSCREEN | SCALED) # primary display surface
  14. SDS = PDS.copy() # secondary display surface
  15. FPS = 120
  16.  
  17. window_closed = False
  18.  
  19. ball_pos = V(PDR.center)
  20. ball_dir = V(1, 0).rotate(randint(0, 359))
  21. ball_radius = 10
  22.  
  23. delta = time.perf_counter()
  24.  
  25. exit_demo = False
  26. while not exit_demo:
  27.     now = time.perf_counter()
  28.     interval = (now - delta) * FPS
  29.     delta = now
  30.  
  31.     if not window_closed:
  32.         for e in pygame.event.get():
  33.             if e.type == KEYUP:
  34.                 if e.key == K_ESCAPE:
  35.                     exit_demo = True
  36.                 if e.key == K_SPACE:
  37.                     pygame.quit()
  38.                     window_closed = True
  39.  
  40.     SDS.fill(BLACK)
  41.     pygame.draw.circle(SDS, WHITE, ball_pos, ball_radius)
  42.  
  43.     if not window_closed:
  44.         PDS.blit(SDS, (0, 0))
  45.         pygame.display.update()
  46.  
  47.     ball_pos += ball_dir * interval
  48.     if ball_pos.x > PDR.right - ball_radius:
  49.         ball_pos.x = PDR.right - ball_radius
  50.         ball_dir.x = -ball_dir.x
  51.     if ball_pos.x < ball_radius:
  52.         ball_pos.x = ball_radius
  53.         ball_dir.x = -ball_dir.x
  54.     if ball_pos.y > PDR.bottom - ball_radius:
  55.         ball_pos.y = PDR.bottom - ball_radius
  56.         ball_dir.y = -ball_dir.y
  57.     if ball_pos.y < ball_radius:
  58.         ball_pos.y = ball_radius
  59.         ball_dir.y = -ball_dir.y
  60.  
Advertisement
RAW Paste Data Copied
Advertisement