This commit is contained in:
206530304 2021-06-20 20:52:19 +08:00
parent bf02cd2264
commit bdcda2028a
2 changed files with 269 additions and 0 deletions

4
__init__/__init__.py Normal file
View File

@ -0,0 +1,4 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-

265
__init__/main.py Normal file
View File

@ -0,0 +1,265 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from pygame.locals import *
from config.settings import *
from src.plane import OurPlane # 导入我们的飞机
from src.enemy import SmallEnemy
from src.bullet import Bullet
import threading
# 解决中文标题报错问题
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
bg_size = 280, 552 # 初始化游戏背景大小(宽, 高)
screen = pygame.display.set_mode(bg_size) # 设置背景对话框
pygame.display.set_caption("外星飞船侵略") # 设置标题
background = pygame.image.load(os.path.join(BASE_DIR, "material/image/background.png")) # 加载背景图片,并设置为不透明
# 获取我方飞机
our_plane = OurPlane(bg_size)
# 分数归0
global score
score = 0
global kill
kill = False
global GroupIs
GroupIs = False #军团
global topListPop
topListPop = False #排行榜弹出
global topListData
topListData = []
global small_enemies
global enemies
def add_small_enemies(group1, group2, num):
if(num==0):
#复活清空敌机
group1.empty()
group2.empty()
else:
for i in range(num):
small_enemy = SmallEnemy(bg_size)
group1.add(small_enemy)
group2.add(small_enemy)
def main():
# 线程监听军团停留时间
t1 = threading.Thread(target=Group)
t1.start()
running = True
switch_image = False # 切换飞机的标识位(使飞机具有喷气式效果)
delay = 60 # 对一些效果进行延迟, 效果更好一些
global small_enemies
global enemies
enemies = pygame.sprite.Group() # 生成敌方飞机组(一种精灵组用以存储所有敌机精灵)
small_enemies = pygame.sprite.Group() # 敌方小型飞机组(不同型号敌机创建不同的精灵组来存储)
add_small_enemies(small_enemies, enemies, 6) # 生成若干敌方小型飞机
# 定义子弹, 各种敌机和我方敌机的毁坏图像索引
bullet_index = 0
e1_destroy_index = 0
me_destroy_index = 0
# 定义子弹实例化个数
bullet1 = []
bullet_num = 6
for i in range(bullet_num):
bullet1.append(Bullet(our_plane.rect.midtop))
while running:
global kill
global topListPop
global topListData
if kill!=True:
# 绘制背景图
screen.blit(background, (0, 0))
clock = pygame.time.Clock()
clock.tick(60)
# 绘制我方飞机的两种不同的形式
if not delay % 3:
switch_image = not switch_image
for each in small_enemies:
if kill!=True and topListPop!=True:
if each.active:
# 随机循环输出小飞机敌机
each.move()
screen.blit(each.image, each.rect)
else:
screen.blit(each.destroy_images[e1_destroy_index], each.rect)
e1_destroy_index = (e1_destroy_index + 1) % 2
if e1_destroy_index == 0:
each.reset()
# 当我方飞机存活状态, 正常展示
if our_plane.active:
if switch_image:
screen.blit(our_plane.image_one, our_plane.rect)
else:
screen.blit(our_plane.image_two, our_plane.rect)
# 飞机存活的状态下才可以发射子弹
if not (delay % 10): # 每十帧发射一颗移动的子弹
bullets = bullet1
bullets[bullet_index].reset(our_plane.rect.midtop)
bullet_index = (bullet_index + 1) % bullet_num
for b in bullets:
if b.active and kill!=True and topListPop!=True: # 不是死亡状态下子弹发射
b.move()
screen.blit(b.image, b.rect)
enemies_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
if enemies_hit: # 如果子弹击中飞机
b.active = False # 子弹损毁
for e in enemies_hit:
global score
global GroupIs
e.active = False # 小型敌机损毁
score = score + 1
if score%10==0 :
add_small_enemies(small_enemies, enemies, 1) #逐渐增加游戏难度
elif score%95==0 and GroupIs!=True:
add_small_enemies(small_enemies, enemies, 0) # 清空敌机
add_small_enemies(small_enemies, enemies, 30) # 初始化敌机
backgroundKill = pygame.image.load(os.path.join(BASE_DIR, "material/image/background1.png"))
screen.blit(backgroundKill, (0, 0))
pygame.display.flip()
pygame.time.wait(1000)
GroupIs = True
# 毁坏状态绘制爆炸的场面
else:
if not (delay % 3) and kill!=True and topListPop!=True:
screen.blit(our_plane.destroy_images[me_destroy_index], our_plane.rect)
me_destroy_index = (me_destroy_index + 1) % 1
if me_destroy_index == 0:
our_plane.reset()
kill = True
GroupIs = False
add_small_enemies(small_enemies, enemies, 0) # 清空敌机
add_small_enemies(small_enemies, enemies, 6) # 初始化敌机
topListData.append(score)#加入最新的分数
topListData.sort()#排行榜列表排序
topListData.reverse()#颠倒排序
print('坠毁')
# 调用 pygame 实现的碰撞方法 spritecollide (我方飞机如果和敌机碰撞, 更改飞机的存活属性)
enemies_down = pygame.sprite.spritecollide(our_plane, enemies, False, pygame.sprite.collide_mask)
if enemies_down:
our_plane.active = False
for row in enemies:
row.active = False
# 响应用户的操作
for event in pygame.event.get():
if event.type == 256: # 如果用户按下屏幕上的关闭按钮触发QUIT事件程序退出
pygame.quit()
sys.exit()
elif event.type==1025:
if event.pos[0]>=256 and event.pos[1]<23:
#print '排行榜'
topListPop = bool(1-topListPop)
if delay == 0:
delay = 60
delay -= 1
# 获得用户所有的键盘输入序列(如果用户通过键盘发出“向上”的指令,其他类似)
key_pressed = pygame.key.get_pressed()
if kill!=True:
if topListPop!=True:
if key_pressed[K_w] or key_pressed[K_UP]:
our_plane.move_up()
if key_pressed[K_s] or key_pressed[K_DOWN]:
our_plane.move_down()
if key_pressed[K_a] or key_pressed[K_LEFT]:
our_plane.move_left()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
our_plane.move_right()
else :
if key_pressed[K_SPACE]:
# 初始化
kill = False
score = 0
# 更新分数
font = pygame.font.Font(None, 25) # 显示中文的设置和字体,及路径
text = font.render(str(score), 1, (255,255,255))
screen.blit(text, (2, 2))
if kill:
#死亡后显示
backgroundKill = pygame.image.load(os.path.join(BASE_DIR, "material/image/game_over.png"))
screen.blit(backgroundKill, (0, 0))
pygame.font.init()
font = pygame.font.SysFont("仿宋_GB2312", 40)
text = font.render(str(score), 1, (255, 255, 255))
screen.blit(text, (132, 220))
elif topListPop:
#排行榜显示
backgroundKill = pygame.image.load(os.path.join(BASE_DIR, "material/image/topListBackground.png"))
screen.blit(backgroundKill, (0, 0))
topList = pygame.image.load(os.path.join(BASE_DIR, "material/image/Close.png"))
screen.blit(topList, (258, 4))
if len(topListData)>=1:
pygame.font.init()
font = pygame.font.SysFont("仿宋_GB2312", 30)
text = font.render(str(topListData[0]), 1, (255, 255, 255))
screen.blit(text, (65, 158))
if len(topListData)>=2:
pygame.font.init()
font = pygame.font.SysFont("仿宋_GB2312", 30)
text = font.render(str(topListData[1]), 1, (255, 255, 255))
screen.blit(text, (65,202))
if len(topListData)>=3:
pygame.font.init()
font = pygame.font.SysFont("仿宋_GB2312", 30)
text = font.render(str(topListData[2]), 1, (255, 255, 255))
screen.blit(text, (65, 246))
if len(topListData)>=4:
pygame.font.init()
font = pygame.font.SysFont("仿宋_GB2312", 30)
text = font.render(str(topListData[3]), 1, (255, 255, 255))
screen.blit(text, (65, 295))
else:
#正常显示
topList = pygame.image.load(os.path.join(BASE_DIR, "material/image/topList.png"))
screen.blit(topList, (258, 4))
pygame.display.flip()
def Group():
while True:
global GroupIs
if GroupIs:
pygame.time.wait(15000)
global small_enemies
global enemies
add_small_enemies(small_enemies, enemies, 0) # 清空敌机
add_small_enemies(small_enemies, enemies, 6) # 初始化敌机7
pygame.display.flip()
GroupIs = False