飞机大战
# 精灵模块
import pygame import random #常量 #屏幕尺寸 PINGMU_CHICUN = pygame.Rect(0,0,400,700) # 屏幕刷新频率 PINGMU_SHUAXIN = 60 A = 0 # 敌机出现定时 DIJI_DINGSHIQI = pygame.USEREVENT ZIDAN_DINGSHIQI = pygame.USEREVENT+1 class JingLing(pygame.sprite.Sprite): def __init__(self,img_name,speed=1): super().__init__() self.image = pygame.image.load(img_name) self.rect = self.image.get_rect() self.speed = speed def update(self): self.rect.y += self.speed # 背景 class BgImg(JingLing): def __init__(self,set = False): super().__init__("./image/bg.jpg",speed=0) if set: self.rect.y = -self.rect.height def update(self): super().update() if self.rect.y >= PINGMU_CHICUN.height: self.rect.y = -self.rect.height # 敌机 class DiJi(JingLing): def __init__(self): # 调用父类 super().__init__("./image/diren.png",speed=random.randint(1,3)) self.rect.y = -self.rect.height #敌机出现速度 #敌机出现位置 self.rect.x = random.randint(0,310) def __del__(self): print("敌机销毁%s" % self.rect) def update(self): super().update() if self.rect.y > PINGMU_CHICUN.height: self.kill() # 英雄 class YingXiong(JingLing): def __init__(self): #调用父类初始化英雄 super().__init__("./image/renwu.png",0) #英雄初始位置 self.rect.centerx = PINGMU_CHICUN.centerx self.rect.bottom = PINGMU_CHICUN.bottom - 120 self.zidan_group = pygame.sprite.Group() def update(self): # if self.rect.left>0 and self.rect.right<PINGMU_CHICUN.right: self.rect.x += self.speed if self.rect.left < 0: self.rect.left = 0 if self.rect.right > PINGMU_CHICUN.right: self.rect.right = PINGMU_CHICUN.right def kaihuo(self): zidan = ZiDan() zidan.rect.bottom = self.rect.top +20 zidan.rect.centerx = self.rect.centerx zidan.add(self.zidan_group) class ZiDan(JingLing): def __init__(self): super().__init__("./image/zidan.png",-4) def update(self): self.rect.y += self.speed if self.rect.bottom < PINGMU_CHICUN.top: self.kill() print("子弹销毁")
# 主模块
import pygame from game_jingling import * class GamePlay(object): def __init__(self): print("游戏初始化...") self.a = 0 #创建游戏窗口 self.game_win = pygame.display.set_mode(PINGMU_CHICUN.size) # 初始化时钟 self.clock = pygame.time.Clock() #调用精灵和精灵组 self.__chuangjian_jingling() #敌机出场定时器 pygame.time.set_timer(DIJI_DINGSHIQI,100) #子弹定时器 pygame.time.set_timer(ZIDAN_DINGSHIQI,500) #创建精灵与精灵组 def __chuangjian_jingling(self): #创建背景精灵 bg1 = BgImg() bg2 = BgImg(set = True) self.back_group = pygame.sprite.Group(bg1,bg2) #创建敌机精灵 self.diji_group = pygame.sprite.Group() #创建英雄 self.yingxiong = YingXiong() self.yingxiong_group = pygame.sprite.Group(self.yingxiong) #开始游戏 def game_start(self): print("游戏开始...") while True: #刷新屏幕 self.clock.tick(PINGMU_SHUAXIN) #监听事件 self.__jianting() #碰撞检测 self.__pengzhuan() #更新精灵 self.__gengxin() #计数器 self.__jishuqi() #更新屏幕 pygame.display.update() #监听函数 def __jianting(self): #遍历监听 for event in pygame.event.get(): #监听到游戏结束 if event.type == pygame.QUIT: self.__youxijieshu() #监听到敌机事件定时器,生成敌机 elif event.type == DIJI_DINGSHIQI: # print("敌机出场") diji = DiJi() self.diji_group.add(diji) #监听到子弹事件定时器,生成子弹 elif event.type == ZIDAN_DINGSHIQI: self.yingxiong.kaihuo() #监听键盘事件 key_wd = pygame.key.get_pressed() #按下右方向键 if key_wd[pygame.K_RIGHT]: # if self.yingxiong.rect.right < PINGMU_CHICUN.right: self.yingxiong.speed = 2 print("向右") #按下左方向键 elif key_wd[pygame.K_LEFT]: # if self.yingxiong.rect.left>0: self.yingxiong.speed = -2 print("向左") #按下上方向键 elif key_wd[pygame.K_UP]: self.yingxiong.rect.y -= 2 if self.yingxiong.rect.top + 50 < PINGMU_CHICUN.top: self.yingxiong.rect.bottom = PINGMU_CHICUN.bottom #按下下方向键 elif key_wd[pygame.K_DOWN]: self.yingxiong.rect.y += 2 if self.yingxiong.rect.bottom > PINGMU_CHICUN.bottom: self.yingxiong.rect.bottom = PINGMU_CHICUN.bottom #如果没按键盘,速度为0 else: self.yingxiong.speed = 0 #碰撞检测 def __pengzhuan(self): diji_pz = pygame.sprite.groupcollide(self.diji_group, self.yingxiong.zidan_group, True,True) if len(diji_pz)>0: self.a += 1 pz = pygame.sprite.spritecollide(self.yingxiong, self.diji_group,True) if len(pz)>0: print("英雄牺牲") self.yingxiong.kill() self.__youxijieshu() #更新位置 def __gengxin(self): self.back_group.update() self.back_group.draw(self.game_win) self.diji_group.update() self.diji_group.draw(self.game_win) # key_wd = pygame.key.get_pressed() # if key_wd[pygame.K_RIGHT] or key_wd[pygame.K_LEFT]: self.yingxiong_group.update() self.yingxiong_group.draw(self.game_win) self.yingxiong.zidan_group.update() self.yingxiong.zidan_group.draw(self.game_win) def __jishuqi(self): pygame.font.init() text = pygame.font.SysFont("Microsoft Yahei",50) text_fm = text.render(str(self.a),1,(0,255,255)) self.game_win.blit(text_fm,(PINGMU_CHICUN.left,PINGMU_CHICUN.bottom-50)) @staticmethod def __youxijieshu(): print("游戏结束...") pygame.quit() exit() if __name__ == '__main__': # 创建游戏对象 game = GamePlay() # 开始游戏 game.game_start()