Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, time, keyboard
- import pygame
- from pygame.locals import *
- from pygame import Rect as R
- from pygame import Vector2 as V
- from random import randint
- BLACK = (15, 56, 15)
- WHITE = (139, 172, 15)
- pygame.init()
- PDR = R(0, 0, 720, 720)
- PDS = pygame.display.set_mode(PDR.size) # primary display surface
- SDS = PDS.copy() # secondary display surface
- FPS = 120
- window_closed = False
- ball_pos = V(PDR.center)
- ball_dir = V(1, 0).rotate(randint(0, 359))
- ball_radius = 50
- delta = time.perf_counter()
- exit_demo = False
- while not exit_demo:
- now = time.perf_counter()
- interval = (now - delta) * FPS
- delta = now
- for e in pygame.event.get():
- if e.type == KEYUP:
- if e.key == K_ESCAPE:
- exit_demo = True
- if e.key == K_SPACE:
- if not window_closed:
- pygame.quit()
- window_closed = True
- if window_closed:
- if keyboard.read_key() == "space":
- pygame.init()
- PDS = pygame.display.set_mode(PDR.size)
- # time.sleep(1)
- window_closed = False
- SDS.fill(BLACK)
- pygame.draw.circle(SDS, WHITE, ball_pos, ball_radius)
- if not window_closed:
- PDS.blit(SDS, (0, 0))
- pygame.display.update()
- ball_pos += ball_dir * interval
- if ball_pos.x > PDR.right - ball_radius:
- ball_pos.x = PDR.right - ball_radius
- ball_dir.x = -ball_dir.x
- if ball_pos.x < ball_radius:
- ball_pos.x = ball_radius
- ball_dir.x = -ball_dir.x
- if ball_pos.y > PDR.bottom - ball_radius:
- ball_pos.y = PDR.bottom - ball_radius
- ball_dir.y = -ball_dir.y
- if ball_pos.y < ball_radius:
- ball_pos.y = ball_radius
- ball_dir.y = -ball_dir.y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement