Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint, uniform
- import time
- import pygame
- from pygame import Rect as R
- from pygame import Vector2 as V
- from pygame.locals import *
- WHITE = (255, 255, 255)
- RED = (255, 0, 0)
- pygame.init()
- PDS = pygame.display.set_mode()
- PDR = PDS.get_rect()
- FPS = 120
- ball = V(PDR.center)
- rad = 80
- v = V()
- f = V()
- pressed = False
- dts = time.time()
- while True:
- pygame.event.get()
- now = time.time()
- dt = (now - dts) * FPS
- dts = now
- mp = V(pygame.mouse.get_pos())
- dist = mp.distance_to(ball)
- mb = pygame.mouse.get_pressed()
- if mb[0] and not pressed:
- if dist <= rad:
- pressed = True
- if pressed:
- if not mb[0] and dist > rad:
- deg = V().angle_to(mp - ball)
- a = V(1, 0)
- b = a.rotate(deg + 180) * (dist - rad)
- v = b / 10
- pressed = False
- ball += v * dt
- if v.x != 0 or v.y != 0:
- a = v.normalize()
- v -= (a / 10) * dt
- if ball.x + rad > PDR.w:
- ball.x = PDR.w - rad
- v.x = -v.x
- if ball.x - rad < 0:
- ball.x = rad
- v.x = -v.x
- if ball.y + rad > PDR.h:
- ball.y = PDR.h - rad
- v.y = -v.y
- if ball.y - rad < 0:
- ball.y = rad
- v.y = -v.y
- PDS.fill(0)
- if pressed and dist > rad:
- pygame.draw.line(PDS, RED, ball, mp)
- pygame.draw.circle(PDS, WHITE, ball, rad)
- pygame.display.update()
Add Comment
Please, Sign In to add comment