forked from YouMakeTech/Pico-GB-MicroPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicoGB.py
104 lines (85 loc) · 3.13 KB
/
PicoGB.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
# PicoGB.py by YouMakeTech
# A class to easily write games for the Raspberry Pi Pico GB
from machine import Pin, PWM, I2C, Timer
from ili9225 import ILI9225
from framebuf import FrameBuffer, RGB565
import time
import random
class PicoGB(ILI9225):
def __init__(self):
self.__up = Pin(2, Pin.IN, Pin.PULL_UP)
self.__down = Pin(3, Pin.IN, Pin.PULL_UP)
self.__left = Pin(4, Pin.IN, Pin.PULL_UP)
self.__right = Pin(5, Pin.IN, Pin.PULL_UP)
self.__button_A = Pin(6, Pin.IN, Pin.PULL_UP)
self.__button_B = Pin(7, Pin.IN, Pin.PULL_UP)
self.__button_select = Pin(8, Pin.IN, Pin.PULL_UP)
self.__button_start = Pin(9, Pin.IN, Pin.PULL_UP)
self.__buzzer = PWM(Pin(15))
super().__init__(width=220, height=176, id_=0, clk=18,
sdi=19, rs=20, rst=21, cs=17, led=22, baudrate=32000000)
self.__fb=[] # Array of FrameBuffer objects for sprites
self.__w=[]
self.__h=[]
def center_text(self, s, color = 1):
x = int(self.width/2)- int(len(s)/2 * 8)
y = int(self.height/2) - 8
self.text(s, x, y, color)
def top_right_corner_text(self, s, color = 1):
x = self.width - int(len(s) * 8)
y = 0
self.text(s, x, y, color)
def add_sprite(self, buffer, w, h):
fb = FrameBuffer(buffer, w, h, RGB565)
self.__fb.append(fb)
self.__w.append(w)
self.__h.append(h)
def sprite(self, n, x, y):
self.blit(self.__fb[n], x, y)
def sprite_width(self,n):
return self.__w[n]
def sprite_height(self,n):
return self.__h[n]
def button_up(self):
return self.__up.value()==0
def button_down(self):
return self.__down.value()==0
def button_left(self):
return self.__left.value()==0
def button_right(self):
return self.__right.value()==0
def button_A(self):
return self.__button_A.value()==0
def button_B(self):
return self.__button_B.value()==0
def button_select(self):
return self.__button_select.value()==0
def button_start(self):
return self.__button_start.value()==0
def any_button(self):
# returns True if any button is pressed
button_pressed=False
if self.button_up():
button_pressed = True
if self.button_down():
button_pressed = True
if self.button_left():
button_pressed = True
if self.button_right():
button_pressed = True
if self.button_A():
button_pressed = True
if self.button_B():
button_pressed = True
if self.button_select():
button_pressed = True
if self.button_start():
button_pressed = True
return button_pressed
def sound(self, freq, duty_u16 = 2000):
# Make a sound at the selected frequency in Hz
if freq>0:
self.__buzzer.freq(freq)
self.__buzzer.duty_u16(duty_u16)
else:
self.__buzzer.duty_u16(0)