-
Notifications
You must be signed in to change notification settings - Fork 0
/
spritecontainer.py
124 lines (89 loc) · 2.76 KB
/
spritecontainer.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
import pygame
class ExtendSprite(pygame.sprite.Sprite):
def __init__(self):
super(ExtendSprite, self).__init__()
self.x, self.y = 0, 0
def setimage(self, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (self.x, self.y)
def bottom(self):
return self.rect.bottom
def right(self):
return self.rect.right
def left(self):
return self.rect.left
def top(self):
return self.rect.top
def width(self):
return self.rect.width
def height(self):
return self.rect.height
def contains(self, x, y):
return self.rect.contains(pygame.Rect(x, y, 1, 1))
def setx(self, x):
self.x = x
self.rect.topleft = (self.x, self.y)
def sety(self, y):
self.y = y
self.rect.topleft = (self.x, self.y)
def setpos(self, x, y):
self.x, self.y = x, y
self.rect.topleft = (self.x, self.y)
def getpos(self):
return self.rect.topleft
def onMouseDown(self, x, y, button):
pass
def onMouseUp(self, x, y, button):
pass
def onMouseMove(self, x, y, button1=None, button2=None, button3=None):
pass
def onKeyDown(self, keyCode, mod):
pass
def onKeyUp(self, keyCode, mod):
pass
class SpriteContainer(pygame.sprite.Group):
def __init__(self):
super(SpriteContainer, self).__init__()
self.spriteset = []
self.x, self.y = 0, 0
def addSprite(self, sprite, x=0, y=0):
self.spriteset.append(sprite)
sprite.offsetx = x
sprite.offsety = y
sprite.setpos(self.x+sprite.offsetx, self.y+sprite.offsety)
def setx(self, x):
self.x = x
self.updateSprites()
def sety(self, y):
self.y = y
self.updateSprites()
def setpos(self, x, y):
self.x, self.y = x, y
self.updateSprites()
def movex(self, dx):
self.setx(self.x+dx)
def movey(self, dy):
self.sety(self.y+dy)
def move(self, dx, dy):
self.setpos(self.x+dx, self.y+dy)
def updateSprites(self):
for sprite in self.spriteset:
sprite.setpos(self.x+sprite.offsetx, self.y+sprite.offsety)
def draw(self, screen):
super(SpriteContainer, self).draw(screen)
def onMouseDown(self, x, y, button):
for sprite in self.spriteset:
sprite.onMouseDown(x, y, button)
def onMouseUp(self, x, y, button):
for sprite in self.spriteset:
sprite.onMouseUp(x, y, button)
def onMouseMove(self, x, y, button1=None, button2=None, button3=None):
for sprite in self.spriteset:
sprite.onMouseMove(x, y)
def onKeyDown(self, keyCode, mod):
for sprite in self.spriteset:
sprite.onKeyDown(keyCode, mod)
def onKeyUp(self, keyCode, mod):
for sprite in self.spriteset:
sprite.onKeyUp(keyCode, mod)