-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsp2d.py
320 lines (248 loc) · 7.13 KB
/
psp2d.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
""" Wrapper for PyGame, which exports the psp2d API on non-PSP systems. """
__author__ = 'Boris Buegling, <[email protected]>'
import pygame, sfont, sys, time, threading
pygame.init()
pygame.display.set_caption('PythonPSP player')
pygame.key.set_repeat(1, 1)
def Color (*args):
# TODO: This should be a class w/ rw attrs red, green, blue, alpha.
return args
class Controller:
""" Mapping the PSP controlls to a keyboard and a mouse (for the analog stick):
Triangle: Keypad s
Square: Keypad x
Circle: Keypad x
Cross: Keypad z
Start: Keypad v
Select: Keypad c
L-Trigger: Keypad q
R-Trigger: Keypad w
Home: F4 (immediately quits)
If someone needs it, I will add support for joy{stick,pads}. """
def __init__ (self, grab=False):
if grab:
pygame.event.set_grab(True)
pygame.mouse.set_visible(False)
else:
pygame.event.set_grab(False)
pygame.mouse.set_visible(True)
self._key = [0] * 323
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
self._key = pygame.key.get_pressed()
if self._key[pygame.K_F4]:
sys.exit(0)
@property
def circle (self):
#return self._key[pygame.K_KP6]
return self._key[pygame.K_x]
@property
def cross (self):
#return self._key[pygame.K_KP2]
return self._key[pygame.K_z]
@property
def triangle (self):
#return self._key[pygame.K_KP8]
return self._key[pygame.K_s]
@property
def square (self):
#return self._key[pygame.K_KP4]
return self._key[pygame.K_a]
@property
def l (self):
return self._key[pygame.K_q]
#return self._key[pygame.K_KP_DIVIDE]
@property
def r (self):
return self._key[pygame.K_w]
#return self._key[pygame.K_KP_MINUS]
@property
def start (self):
#return self._key[pygame.K_KP0]
return self._key[pygame.K_v]
@property
def select (self):
#return self._key[pygame.K_KP_PERIOD]
return self._key[pygame.K_c]
@property
def left (self):
return self._key[pygame.K_LEFT]
@property
def right (self):
return self._key[pygame.K_RIGHT]
@property
def up (self):
return self._key[pygame.K_UP]
@property
def down (self):
return self._key[pygame.K_DOWN]
@property
def analogX (self):
xpos = pygame.mouse.get_pos()[0] * 256 / 480 - 127
return 0 #xpos
@property
def analogY (self):
ypos = pygame.mouse.get_pos()[1] * 256 / 272 - 127
return 0 #ypos
class Font (sfont.Font):
def __init__ (self, fname):
sfont.Font.__init__(self, fname)
def drawText (self, img, x, y, text):
self.write(img, (x, y), text)
def textHeight (self, c):
return self.size(str(c))[1]
def textWidth (self, c):
return self.size(str(c))[0]
class Image (pygame.Surface):
def __init__ (self, *args):
if len(args) == 2:
pygame.Surface.__init__(self, (args[0], args[1]), pygame.SRCALPHA, 32)
self.set_alpha(255)
else:
if type(args[0]) is Image:
surf = args[0]
else:
surf = pygame.image.load(args[0])
pygame.Surface.__init__(self, (surf.get_width(), surf.get_height()),
pygame.SRCALPHA, 32)
self.blit(surf, surf.get_rect())
def clear (self, color):
self.fill(color)
def blit (self, src, sx = 0, sy = 0, w = -1, h = -1, dx = 0, dy = 0, blend = False):
# TODO: Implement 'blend'
if type(dx) is float:
print("dx is flota, should be integer")
if type(dy) is float:
print("dx is flota, should be integer")
if type(sx) is tuple:
pygame.Surface.blit(self, src, sx)
return
if dx == -1:
dx = src.get_rect()[0]
if dy == -1:
dy = src.get_rect()[1]
if type(sx) is int:
if w == -1: w = src.width
if h == -1: h = src.height
tmp = src.subsurface(sx, sy, w, h)
pygame.Surface.blit(self, tmp, (dx, dy))
return
pygame.Surface.blit(self, src, (dx, dy))
def fillRect (self, x, y, w, h, color):
# TODO: Implement this.
pass
def saveToFile (self, filename):
pygame.image.save(self, filename)
def putPixel (self, x, y, Color):
# TODO: Implement this
pass
def getPixel (self, x, y):
# TODO: Implement this
pass
@property
def width (self):
rect = self.get_rect()
return rect[2] - rect[0]
@property
def height (self):
rect = self.get_rect()
return rect[3] - rect[1]
"""Flip image on X and/or Y axis
"""
def flip(self, flip_x, filp_y):
print("FLIP!!!!!")
return pygame.transform.flip(self, flip_x, filp_y)
class Screen:
def __init__ (self, width=480, height=272):
self._screen = pygame.display.set_mode((width, height))
def blit (self, img, sx=0, sy=0, sw=-1, sh=-1, dx=-1, dy=-1, blend=False, dw=-1, dh=-1):
# TODO: Implement 'blend'
if type(sx) is tuple:
dx, dy = sx
sx = 0
rect = img.get_rect()
h = rect[3] - rect[1]
w = rect[2] - rect[0]
if sw == -1: sw = w
if sh == -1: sh = h
if dx == -1: dx = rect[0]
if dy == -1: dy = rect[1]
if dw == -1: dw = w
if dh == -1: dh = h
tmp = img.subsurface(sx, sy, sw, sh)
if dw != w or dh != h:
tmp = pygame.transform.scale(tmp, (dw, dh))
self._screen.blit(tmp, (dx, dy))
def swap (self):
pygame.display.flip()
#time.sleep(0.005)
pygame.event.pump()
def clear (self, color):
self._screen.fill(color)
def fillRect (self, x, y, w, h, color):
self._screen.fill((x,y,w,h), color)
def saveToFile (self, filename):
surf = pygame.display.get_surface()
pygame.image.save(surf, filename)
def putPixel (self, x, y, Color):
# TODO: Implement this
pass
def getPixel (self, x, y):
# TODO: Implement this
pass
@property
def width (self):
rect = self._screen.get_rect()
return rect[2] - rect[0]
@property
def height (self):
rect = self._screen.get_rect()
return rect[3] - rect[1]
class Mask:
""" This is a 2-dimensionnal bit field, intended to be used in collision detection.. """
# TODO: Implement this.
def __init__ (self, img, x, y, w, h, threshold):
pass
def collide (self, msk):
pass
def union (self, msk):
pass
def isIn (self, x, y):
pass
TR_PLUS = 1
TR_MULT = 2
class Transform:
""" A class used to make pixel-based transformations on images. """
# TODO: Implement this.
# Constants for type
TR_PLUS, TR_MULT = range(0, 2)
def __init__ (self, type, param=None):
pass
# def __init__ ((self, cb):
def apply (self, img):
pass
class BlitBatch:
def __init__ (self):
self._lst = []
def add (self, img):
self._lst.append(img)
def blit (self):
for img in self._lst:
screen.blit(img.data.img, dx=img.data.x, dy=img.data.y)
class TimerThread (threading.Thread):
def __init__ (self, func, timeout):
threading.Thread.__init__(self)
self.function = func
self.timeout = timeout / 1000
def run (self):
while True:
time.sleep(self.timeout)
if not self.function():
break
class Timer:
def __init__ (self, timeout):
self.thr = TimerThread(self.fire, timeout)
def fire (self):
raise NotImplementedError('Override in your subclasse.')
def run (self):
self.thr.start()