#python galaxy.py
from kivy.app import App
from kivy.graphics import Line, Rectangle, Color
from kivy.metrics import dp
from kivy.properties import NumericProperty, Clock
from kivy.uix.widget import Widget
class Galaxy(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.n_vlines = 11
self.n_hlines = 9
self.space_vlines = dp(100)
self.speed_hl = 10
self.step_hl = 0
self.clock = 0.2
self.speed_vl = 10
self.step_vl = 0
self.direction_vl = 1
with self.canvas:
self.vlines = []
self.hlines = []
for i in range(self.n_vlines):
vline = Line(points=(0,0,0,100))
self.vlines.append(vline)
for j in range(self.n_hlines):
hline = Line(points=(0,0,100,0))
self.hlines.append(hline)
self.rect_l = Rectangle(pos =(0,0), size=(0,0))
self.rect_r = Rectangle(pos=(0, 0), size=(0, 0))
#Clock.schedule_interval(self.update, self.clock)
def on_size(self, *args):
self.update(0.2)
def on_touch_down(self, touch):
if touch.x < self.width/2:
self.rect_l.pos = (0, self.center_y-dp(50))
self.rect_l.size = (dp(100), dp(100))
self.direction_vl = abs(self.direction_vl)
self.update(0.2)
else:
self.rect_r.pos = (self.width-dp(100), self.center_y - dp(50))
self.rect_r.size = (dp(100), dp(100))
self.direction_vl = -abs(self.direction_vl)
self.update(0.2)
def on_touch_up(self, touch):
self.rect_l.size=(0,0)
self.rect_r.size = (0, 0)
def perspective_transform(self, x, y):
#return x, y
ratio = (self.height-y)/self.height
tx = ratio*(self.center_x-x)
ty = ratio**2
return self.center_x-tx, 0.8*self.height*(1-ty)
def update(self, dt):
start_x = self.center_x - int(self.n_vlines / 2) * self.space_vlines
shift_x = self.speed_vl * self.step_vl * dt / self.clock
"""
if shift_x > self.width or shift_x < -self.width:
self.direction_vl = -self.direction_vl
"""
self.step_vl += self.direction_vl
for i in range(self.n_vlines):
x = start_x + self.space_vlines * i + shift_x
x1, y1, x2, y2 = x, 0, x, self.height
x1, y1 = self.perspective_transform(x1, y1)
x2, y2 = self.perspective_transform(x2, y2)
self.vlines[i].points = (x1, y1, x2, y2)
space_hlines = self.height / self.n_hlines
shift = self.speed_hl * self.step_hl * dt / self.clock
if shift > space_hlines:
self.step_hl = 0
shift = 0
self.step_hl += 1
for j in range(self.n_hlines):
y = j * space_hlines - shift
x1, y1, x2, y2 = start_x + shift_x, y, self.width - start_x + shift_x, y
_, y1 = self.perspective_transform(x1, y1)
x1, _ = self.perspective_transform(x1, y1 / 0.8)
_, y2 = self.perspective_transform(x2, y2)
x2, _ = self.perspective_transform(x2, y2 / 0.8)
self.hlines[j].points = (x1, y1, x2, y2)
class GalaxyApp(App):
pass
GalaxyApp().run()
reference:
No comments:
Post a Comment