6. Gravitačný aktraktor
from pygame import *
from random import *
WIDTH = 800
HEIGHT = 600
BLACK = Color(0, 0, 0)
RED = Color(255, 0, 0)
R = 2
location = Vector2([randrange(WIDTH), randrange(HEIGHT)])
velocity = Vector2([0, 0])
acceleration = Vector2([0, 0])
mous = Vector2([0, 0])
screen = display.set_mode([WIDTH, HEIGHT])
while True:
ev = event.poll()
if ev.type == MOUSEMOTION:
mous = Vector2(ev.pos)
direction = mous - location
direction = 0.5 * direction.normalize()
acceleration = direction
velocity += acceleration
velocity.scale_to_length(min(max(4, velocity.magnitude()), 8))
location += velocity
# screen.fill(BLACK) Čo keby vymažem a zväčším polomer
draw.circle(screen, RED, (int(location.x), int(location.y)), R)
display.update()
time.delay(10)
from pygame import *
from math import hypot
from random import *
CERVENA = Color(255, 0, 0)
ROZLISENIE = [1000, 800]
okno = display.set_mode(ROZLISENIE)
x = randint(0, 1000)
y = randint(0, 800)
vx = 0
vy = 0
ax = 0
ay = 0
mysX = 0
mysY = 0
while True:
mys = event.poll()
if mys.type == MOUSEMOTION:
mysX = mys.pos[0]
mysY = mys.pos[1]
ax = mysX - x
ay = mysY - y
length = hypot(ax, ay)
ax = 0.5 * (ax / length)
ay = 0.5 * (ay / length)
vx = vx + ax
vy = vy + ay
x = x + vx
y = y + vy
draw.circle(okno, CERVENA, [int(x), int(y)], 2)
display.update()
time.delay(10)