-
Notifications
You must be signed in to change notification settings - Fork 13
/
demo_sprite.py
115 lines (96 loc) · 3.3 KB
/
demo_sprite.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
112
113
114
115
"""SSD1351 demo (bouncing sprite)."""
from ssd1351 import Display
from machine import Pin, SPI
from utime import sleep_us, ticks_us, ticks_diff
class BouncingSprite(object):
"""Bouncing Sprite."""
def __init__(self, path, w, h, screen_width, screen_height,
speed, display):
"""Initialize sprite.
Args:
path (string): Path of sprite image.
w, h (int): Width and height of image.
screen_width (int): Width of screen.
screen_height (int): Width of height.
size (int): Square side length.
speed(int): Initial XY-Speed of sprite.
display (SSD1351): OLED display object.
color (int): RGB565 color value.
"""
self.buf = display.load_sprite(path, w, h)
self.w = w
self.h = h
self.screen_width = screen_width
self.screen_height = screen_height
self.display = display
self.x_speed = speed
self.y_speed = speed
self.x = self.screen_width // 2
self.y = self.screen_height // 2
self.prev_x = self.x
self.prev_y = self.y
def update_pos(self):
"""Update sprite speed and position."""
x = self.x
y = self.y
w = self.w
h = self.h
x_speed = abs(self.x_speed)
y_speed = abs(self.y_speed)
if x + w + x_speed >= self.screen_width:
self.x_speed = -x_speed
elif x - x_speed < 0:
self.x_speed = x_speed
if y + h + y_speed >= self.screen_height:
self.y_speed = -y_speed
elif y - y_speed <= 0:
self.y_speed = y_speed
self.prev_x = x
self.prev_y = y
self.x = x + self.x_speed
self.y = y + self.y_speed
def draw(self):
"""Draw sprite."""
x = self.x
y = self.y
prev_x = self.prev_x
prev_y = self.prev_y
w = self.w
h = self.h
x_speed = abs(self.x_speed)
y_speed = abs(self.y_speed)
# Determine direction and remove previous portion of sprite
if prev_x > x:
# Left
self.display.fill_vrect(x + w, prev_y, x_speed, h, 0)
elif prev_x < x:
# Right
self.display.fill_vrect(x - x_speed, prev_y, x_speed, h, 0)
if prev_y > y:
# Upward
self.display.fill_vrect(prev_x, y + h, w, y_speed, 0)
elif prev_y < y:
# Downward
self.display.fill_vrect(prev_x, y - y_speed, w, y_speed, 0)
self.display.draw_sprite(self.buf, x, y, w, h)
def test():
"""Bouncing sprite."""
try:
# Baud rate of 14500000 seems about the max
spi = SPI(2, baudrate=14500000, sck=Pin(18), mosi=Pin(23))
display = Display(spi, dc=Pin(17), cs=Pin(5), rst=Pin(16))
display.clear()
# Load sprite
logo = BouncingSprite('images/Python41x49.raw',
41, 49, 128, 128, 1, display)
while True:
timer = ticks_us()
logo.update_pos()
logo.draw()
# Attempt to set framerate to 30 FPS
timer_dif = 33333 - ticks_diff(ticks_us(), timer)
if timer_dif > 0:
sleep_us(timer_dif)
except KeyboardInterrupt:
display.cleanup()
test()