forked from HaoZhang95/Python24
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy_plane.py
More file actions
32 lines (27 loc) · 1001 Bytes
/
Copy pathenemy_plane.py
File metadata and controls
32 lines (27 loc) · 1001 Bytes
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
import pygame, random
# 定义一个常量类,赋值后不能修改,Python中崇尚的是一切靠自觉
WINDOW_WIDTH = 512
WINDOW_HEIGHT = 768
# 敌机
class EnemyPlane(object):
def __init__(self):
self.num = str(random.randint(1, 7))
# 敌机图片
self.img = pygame.image.load("res/img-plane_" + self.num + ".png")
# 外框矩形 -> (x,y, 宽像素,高像素)
self.img_rect = self.img.get_rect()
# 随机的初始化位置
self.reset()
# 敌机向下移动
def move_down(self):
self.img_rect.move_ip(0, self.speed)
# 敌机位置重置,进行回收
if self.img_rect[1] >= WINDOW_HEIGHT:
self.reset()
# 随机的初始化位置
def reset(self):
# 随机的初始化位置
self.img_rect[0] = random.randint(0, WINDOW_WIDTH - self.img_rect[2])
self.img_rect[1] = -self.img_rect[3]
# 敌机移动的速度
self.speed = random.randint(3,5)