-
Notifications
You must be signed in to change notification settings - Fork 0
/
starfield.py
111 lines (87 loc) · 2.77 KB
/
starfield.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
"""A simple starfield example. Note you can move the 'center' of
the starfield by leftclicking in the window. This example show
the basics of creating a window, simple pixel plotting, and input
event management"""
import random, math
from Xlib import display
from rival_canvas import Canvas
import numpy
MAXX, MAXY = 36, 128
oled = Canvas(numpy.zeros((MAXX, MAXY), dtype=int, order='C'))
timeout = 60000
#constants
WINSIZE = [MAXX, MAXY]
WINCENTER = [MAXX/2, MAXY/2]
NUMSTARS = 60
def screensize():
x = display.Display().screen().width_in_pixels
y = display.Display().screen().height_in_pixels
return x, y
def mousepos():
"""mousepos() --> (x, y) get the mouse coordinates on the screen (linux, Xlib)."""
data = display.Display().screen().root.query_pointer()._data
return data["root_x"], data["root_y"]
def init_star():
dir = random.randrange(100000)
velmult = random.random()*.6+.4
vel = [math.sin(dir) * velmult, math.cos(dir) * velmult]
return vel, WINCENTER[:]
def initialize_stars():
stars = []
for x in range(NUMSTARS):
star = init_star()
vel, pos = star
steps = random.randint(0, WINCENTER[0])
pos[0] = pos[0] + (vel[0] * steps)
pos[1] = pos[1] + (vel[1] * steps)
vel[0] = vel[0] * (steps * .09)
vel[1] = vel[1] * (steps * .09)
stars.append(star)
move_stars(stars)
return stars
def draw_stars(stars):
for vel, pos in stars:
Canvas.setpixel(oled, int(pos[0]), int(pos[1]))
def move_stars(stars):
for vel, pos in stars:
pos[0] = pos[0] + vel[0]
pos[1] = pos[1] + vel[1]
if not 0 <= pos[0] <= WINSIZE[0] or not 0 <= pos[1] <= WINSIZE[1]:
vel[:], pos[:] = init_star()
else:
vel[0] = vel[0] * 1.05
vel[1] = vel[1] * 1.05
def main():
x, y = screensize()
xratio = x/MAXX
yratio = y/MAXY
"This is the starfield code"
#create our starfield
random.seed()
stars = initialize_stars()
#main game loop
done = 0
while not done:
draw_stars(stars)
move_stars(stars)
Canvas.drawcanvas(oled, timeout)
posx, posy = mousepos()
posx = posx / xratio
posy = posy / yratio
WINCENTER[:] = (posx, posy)
"""
for e in pygame.event.get():
if e.type == MOUSEMOTION:
print("mouse")
if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
done = 1
break
elif e.type == MOUSEMOTION:
print("mouse", pygame.mouse.get_pos())
WINCENTER[:] = list(e.pos)
"""
Canvas.tick(oled)
# if python says run, then we should run
if __name__ == '__main__':
main()