-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplane_main.py
139 lines (113 loc) · 3.89 KB
/
plane_main.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
"""
游戏主程序
1. 封装 主游戏类
2. 创建 游戏对象
3. 启动游戏
"""
import sys
import pygame
import background
from plane_sprites import SCREEN_RECT, FRAME_PER_SEC
from enemy import *
from plane_sprites import *
from hero import *
class PlaneGame:
"""用于配置及初始化游戏内容"""
def __init__(self):
"""初始化"""
print("游戏初始化...")
pygame.init()
print("设置游戏窗口中...")
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
print("创建游戏时钟中...")
self.clock = pygame.time.Clock()
print("调用私有方法,创建精灵和精灵组...")
self.__create_sprites()
print("设置定时器事件")
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
def start_game(self):
"""开始游戏"""
print("游戏开始...")
while True:
# 设置刷新帧率
self.clock.tick(FRAME_PER_SEC)
# 事件监听
self.__event_handler()
# 碰撞检测
self.__check_collide()
# 更新/绘制精灵
self.__update_sprites()
# 更新显示
pygame.display.update()
@property.setter
def __screen(self, wide, height):
"""屏幕"""
@property.setter
def __clock(self, time):
"""游戏时钟"""
def __create_sprites(self):
"""创建精灵组"""
bg1 = background.Background()
bg2 = background.Background(True)
self.back_group = pygame.sprite.Group(bg1, bg2)
# 创建敌机的精灵组
self.enemy_group = pygame.sprite.Group()
# 创建英雄
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def __event_handler(self):
"""事件监听"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
PlaneGame.__game_over() # 使用类名的方式调用静态方法
elif event.type == CREATE_ENEMY_EVENT:
print("敌机出场")
# 创建敌机精灵
enemy = Enemy()
# 敌机精灵添加到敌机组
self.enemy_group.add(enemy)
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
# elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
# self.hero.rect.x += 1 这种方法,只能检测按下
# 应该使用键盘模块,可以按下键盘一直不放
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_RIGHT]:
self.hero.speed = 2
elif keys_pressed[pygame.K_LEFT]:
self.hero.speed = -2
else:
self.hero.speed = 0
def __check_collide(self):
"""碰撞检测"""
# 子弹摧毁敌机
pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
# 敌机撞毁英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
if len(enemies) != 0:
# 英雄牺牲
self.hero.kill()
# 结束游戏
self.__game_over()
def __update_sprites(self):
"""更新精灵组"""
self.back_group.update()
self.back_group.draw(self.__screen)
self.enemy_group.update()
self.enemy_group.draw(self.__screen)
self.hero_group.update()
self.hero_group.draw(self.__screen)
self.hero.bullets.update()
self.hero.bullets.draw(self.__screen)
@staticmethod
def __game_over():
"""游戏结束"""
print("游戏结束...")
pygame.quit()
sys.exit()
if __name__ == '__main__':
# 创建游戏对象
game = PlaneGame()
# 开始游戏
game.start_game()