-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbullet.py
More file actions
68 lines (53 loc) · 1.89 KB
/
bullet.py
File metadata and controls
68 lines (53 loc) · 1.89 KB
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
#coding: utf-8
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""对飞船所出的位置设置子弹进行管理的类"""
def __init__(self, scrren, ship):
"""对飞船所处的位置创建一个子弹对象"""
super(Bullet, self).__init__()
self.__scrren = scrren
self.__width = ship.get_bullets_width()
self.__height = ship.get_bullets_height()
self.__color = ship.get_bullets_color()
self.__speed = ship.get_bullets_speed()
# 在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
self.rect = pygame.Rect(0, 0, self.__width, self.__height)
self.rect.centerx = ship.get_self_center()
rect = ship.get_rect()
self.rect.top = rect.top
# 存储用小数表示的子弹的位置
self.__y = float(self.rect.y)
def update(self):
"""向上移动子弹"""
# 更新表示子弹位置的小数值
self.__y -= self.__speed
# 更新表示子弹的rect位置
self.rect.y = self.__y
def draw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.__scrren, self.__color, self.rect)
def set_info(self, width, height, speed, color):
"""设置子弹基本属性"""
self.__width = width
self.__height = height
self.__color = color
self.__speed = speed
def get_width(self):
"""获取宽度"""
return self.__width
def get_hight(self):
"""获取高度"""
return self.__height
def get_speed(self):
"""获取速度"""
return self.__speed
def get_color(self):
"""获取颜色"""
return self.__color
def get_rect(self):
return self.rect
def delete_bullte(bullet, bulltes):
rect = bullet.get_rect()
if 0 >= rect.bottom:
bullet.remove(bulltes)