-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.py
180 lines (155 loc) · 5.26 KB
/
sprites.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
# coding:utf-8
from util import merge_image
__author__ = 'cupen'
import pygame
from pygame.rect import Rect
class AnimalSprite(pygame.sprite.Sprite):
"""
:type image: pygame.surface.Surface
"""
def __init__(self, image, animConfig = ()):
"""
:param animConfig: 动画配置。
比如 ((4,4), (12,13,14,15)) 表示横向切割4块,纵向切割4块, 播放按块的编号进行
"""
super(AnimalSprite, self).__init__()
self.image = image
self.rect = self.image.get_rect()
self.__animImages = None
self.__animImagesIdx = 0
if len(animConfig) > 0:
_wCount = animConfig[0][0]
_hCount = animConfig[0][1]
_w = self.rect.width / _wCount
_h = self.rect.height / _hCount
_list = []
for i in animConfig[1]:
_tmpRect = Rect(int(i % _wCount) * _w, int((i / _hCount)) * _h, _w, _h)
print(i, _tmpRect)
_list.append(self.image.subsurface(_tmpRect))
self.__animImages = tuple(_list)
self.image = self.__animImages[0]
self.rect = self.image.get_rect()
pass
def update(self):
self.__animImagesIdx += 1
self.__animImagesIdx %= len(self.__animImages)
self.image = self.__animImages[self.__animImagesIdx]
class Plane(AnimalSprite):
def __init__(self, image,position = (0,0), animConfig = ()):
super(Plane, self).__init__(image, animConfig)
self.rect = self.image.get_rect()
self.rect.x = position[0]
self.rect.y = position[1]
self.speed = 4
self.status = {"up":False, "down": False, "left":False, "right": False}
pass
def create_bullet(self, image, area):
bullet = Bullet(image, area)
bullet.rect.x = self.rect.x + ((self.rect.width - bullet.rect.width) / 2)
bullet.rect.y = self.rect.y
bullet.speedX = 0
bullet.speedY = -(self.speed * 2)
return bullet
def handleEvent(self, e):
if e.key == pygame.K_w:
self.status['up'] = (e.type == pygame.KEYDOWN)
elif e.key == pygame.K_s:
self.status['down'] = (e.type == pygame.KEYDOWN)
elif e.key == pygame.K_a:
self.status['left'] = (e.type == pygame.KEYDOWN)
elif e.key == pygame.K_d:
self.status['right'] = (e.type == pygame.KEYDOWN)
elif e.key == pygame.K_j:
pass
pass
def add_bullet(self, bulletSprite):
for group in self.groups():
group.add(bulletSprite)
pass
def update(self):
super(Plane, self).update()
# print(self.status)
for key, value in self.status.items():
if not value:
continue
if key == 'up':
self.move_up()
elif key == 'down':
self.move_down()
elif key == 'left':
self.move_left()
elif key == 'right':
self.move_right()
pass
def move_up(self, speed = 0):
self.rect.y -= self.speed
pass
def move_down(self, speed = 0):
self.rect.y += self.speed
pass
def move_left(self, speed = 0):
self.rect.x -= self.speed
pass
def move_right(self, speed = 0):
self.rect.x += self.speed
pass
class Player(Plane):
pass
class Enemy(Plane):
"""
AI
"""
def __init__(self, image, position = (0,0), speed = 5):
super(Enemy, self).__init__(image, position)
self.speed = speed
pass
def update(self):
self.rect.y += self.speed
pass
class Bullet(pygame.sprite.Sprite):
def __init__(self, image, area):
super(Bullet, self).__init__()
self.image = image
self.rect = image.get_rect()
self.speedX = 0
self.speedY = 0
self.areaRect = area
pass
def update(self):
self.rect.x += self.speedX
self.rect.y += self.speedY
# 子弹出界(bullet is out of area)
if not self.areaRect.contains(self.rect):
self.kill()
pass
class Background(pygame.sprite.Sprite):
def __init__(self, images, width, height, speed = 2):
super(Background, self).__init__()
self.image = merge_image(*images)
self.image = merge_image(self.image, images[0])
multiple = width / self.image.get_width()
newWidth = width
newHeight = int(self.image.get_height() * multiple)
self.image = self.image = pygame.transform.scale(self.image, (newWidth, newHeight))
self.rect = self.image.get_rect()
self.screenWidth = width
self.screenHeight = height
self.speed = speed
self.resetImagePostion()
pass
def resetImagePostion(self):
self.rect.y = (self.screenHeight - self.rect.height)
pass
def update(self):
self.rect.y += self.speed
if self.rect.y > 0:
print(self.rect)
self.resetImagePostion()
print(self.rect)
# if self.rect.y > 0:
# self.image = self.__images[0]
# self.__images[0] = self.__images[1]
# self.__images[1] = self.image
# self.resetCurImage()
pass