def _update_scene(self): """ 更新屏幕中的场景 :return: None """ virus_image_path1 = '/Users/songyunlong/Desktop/c++程序设计实践课/picture1.png' virus_image_path2 = '/Users/songyunlong/Desktop/c++程序设计实践课/picture2.png' virus_image_path3 = '/Users/songyunlong/Desktop/c++程序设计实践课/picture3.png' bg_color = (100, 100, 100) self._screen.fill(bg_color) if self._start_playing and not self._pause_playing: # 如果游戏不停值或者未达到停止标志,比如赢得游戏和达到本等级病毒最大数量则病毒一直会有 if self._virus_count <= self._NumOfViruses: # 增加病毒数量计数,到达一定数量之后就投入新病毒 # print(len(self._viruses)) # 打印病毒个数,打死的不算在内 print(len(self._viruses)) try: if self._virus_count <= int(self._NumOfViruses // 4): new_virus = Virus(screen=self._screen, virus_image=virus_image_path1, pos_x=next(self._init_posxes)) elif self._virus_count <= int(self._NumOfViruses // 4 * 2): new_virus = VirusStyle2(screen=self._screen, virus_image=virus_image_path2, pos_x=next(self._init_posxes)) new_virus.virus_speed = 2 * new_virus.virus_speed #二号病毒的速度是普通病毒的两倍 new_virus.ImageOfVirus = virus_image_path2 else: new_virus = VirusStyle3(screen=self._screen, virus_image=virus_image_path3, pos_x=next(self._init_posxes)) self._viruses.add(new_virus) self._virus_count += 1 except StopIteration: #需要修改成病毒下放完后不自动退出! #不加入病毒全部消失前提前出现庆祝标志 # print('消灭病毒数量为:', self._total_score) # print('消灭v1病毒数为{0},v2病毒数为{1},v3病毒数为{2}'.format(self._virus1_num, self._virus2_num, self._virus3_num)) # #添加数据库写入操作,在游戏自己结束后也需要将数据存入数据库 # # sys.exit() # self._need_congratulation = True # self._start_playing = False # if not self._is_finished_storing: # with Score(name=self._usrname) as score: # score.virus1 = self._virus1_num # score.virus2 = self._virus2_num # score.virus3 = self._virus3_num # score.scoring() # score.create_data() # 如果用户第一次玩,需要先为此用户建立数据表 # score.update_table() # score.update_usr_info() # #将存储数据标志置True # self._is_finished_storing = True #病毒全部消失后出现 print('病毒已全部加载完成!') flag = True # 病毒全部消失标记 for virus in self._viruses.sprites(): #病毒上边界全部低于飞行器下边界结束 if virus.rect.top <= self._aircraft.rect.bottom: flag = False break #病毒全部从屏幕消失结束 # if virus.rect.top <= self._screen.get_rect().bottom: # flag = False # break if flag: print('消灭病毒数量为:', self._total_score) print('消灭v1病毒数为{0},v2病毒数为{1},v3病毒数为{2}'.format( self._virus1_num, self._virus2_num, self._virus3_num)) # 添加数据库写入操作,在游戏自己结束后也需要将数据存入数据库 # sys.exit() self._need_congratulation = True self._start_playing = False if not self._is_finished_storing: with Score(name=self._usrname) as score: score.virus1 = self._virus1_num score.virus2 = self._virus2_num score.virus3 = self._virus3_num score.scoring() score.create_data() # 如果用户第一次玩,需要先为此用户建立数据表 score.update_table() score.update_usr_info() # 将存储数据标志置True self._is_finished_storing = True # 更新所有病毒 self._viruses.update() # 更新所有子弹 self._bullets.update() # 飞船位置更新和边界限制 if self._can_up and self._aircraft.rect.top >= self._aircraft.RectBorderOfScreen.top: if self._aircraft.rect.top - self._aircraft.RectBorderOfScreen.top <= self._aircraft.v: self._aircraft.rect.top = self._aircraft.RectBorderOfScreen.top else: pre_pos = float(self._aircraft.rect.centery) self._aircraft.rect.centery = pre_pos - self._aircraft.v elif self._aircraft.rect.top < self._aircraft.RectBorderOfScreen.top: # 控制飞行器大小切换后机身不出边界 self._aircraft.rect.top = self._aircraft.RectBorderOfScreen.top if self._can_down and self._aircraft.rect.bottom <= self._aircraft.RectBorderOfScreen.bottom: if self._aircraft.RectBorderOfScreen.bottom - self._aircraft.rect.bottom <= self._aircraft.v: self._aircraft.rect.bottom = self._aircraft.RectBorderOfScreen.bottom else: pre_pos = float(self._aircraft.rect.centery) self._aircraft.rect.centery = pre_pos + self._aircraft.v elif self._aircraft.rect.bottom > self._aircraft.RectBorderOfScreen.bottom: # 同上 self._aircraft.rect.bottom = self._aircraft.RectBorderOfScreen.bottom if self._can_left and self._aircraft.rect.left >= self._aircraft.RectBorderOfScreen.left: if self._aircraft.rect.left - self._aircraft.RectBorderOfScreen.left <= self._aircraft.v: self._aircraft.rect.left = self._aircraft.RectBorderOfScreen.left else: pre_pos = float(self._aircraft.rect.centerx) self._aircraft.rect.centerx = pre_pos - self._aircraft.v elif self._aircraft.rect.left < self._aircraft.RectBorderOfScreen.left: # 同上 self._aircraft.rect.left = self._aircraft.RectBorderOfScreen.left if self._can_right and self._aircraft.rect.right <= self._aircraft.RectBorderOfScreen.right \ - Settings().boundary_pos: if self._aircraft.RectBorderOfScreen.right - Settings().boundary_pos \ - self._aircraft.rect.right <= self._aircraft.v: self._aircraft.rect.right = self._aircraft.RectBorderOfScreen.right - Settings( ).boundary_pos else: pre_pos = float(self._aircraft.rect.centerx) self._aircraft.rect.centerx = pre_pos + self._aircraft.v elif self._aircraft.rect.right > self._aircraft.RectBorderOfScreen.right - Settings( ).boundary_pos: self._aircraft.rect.right = self._aircraft.RectBorderOfScreen.right - Settings( ).boundary_pos # 击中病毒的子弹以及被击中的病毒 collisions = pygame.sprite.groupcollide( #collisions返回当前时刻的子弹和病毒碰撞字典 groupa=self._bullets, groupb=self._viruses, dokilla=True, dokillb=True) # dokill是碰撞后是否立即删除 #如果有子弹和病毒的碰撞存在 if len(collisions.values()) > 0: for group_virus in list(collisions.values()): for per_virus in group_virus: per_virus.dead = True self._total_score += per_virus.score self._score_boarder.msg = self._total_score # 记分牌 self._score_boarder.msg_set() self._score_boarder.draw_button() # if type(per_virus) == Virus: self._virus1_num += 1 elif type(per_virus) == VirusStyle2: self._virus2_num += 1 else: #加入病毒分裂的操作,在原位置向斜下方随机炸开 virus_sub1, virus_sub2 = VirusStyle3.spliting( self._screen, virus_image_path3, per_virus.virus_rect_right) # print(type(virus_sub1), type(virus_sub2)) virus_sub1.rect.bottom = virus_sub2.rect.bottom = per_virus.virus_rect_bottom + 20 virus_sub1.blit_virus() virus_sub2.blit_virus() self._viruses.add(virus_sub1, virus_sub2) self._virus3_num += 1 # print(list(collisions.values())[0][0].dead) # 删除消失的子弹 for bullet in self._bullets.sprites(): if bullet.rect_.bottom < 0: self._bullets.remove(bullet) # 删除被击中且被达到击毙条件或到达屏幕底端的病毒 for virus in self._viruses.sprites(): if virus.rect.bottom == self._screen.get_rect().bottom: self._viruses.remove(virus) # # 判断飞行器是否触碰到病毒,即飞行器变大 for virus in self._viruses.sprites(): # 病毒上边界与飞行器下边界y值相等且横边有交集 if virus.rect.top == self._aircraft.rect.bottom: if not Main.have_intersection( (virus.rect.left, virus.rect.right), (self._aircraft.rect.left, self._aircraft.rect.right)): self._judge_state_of_aircraft() # sys.exit()#改成飞行器变大,如果在变大状态下再次与病毒相撞,则退出 # 病毒下边界与飞行器上边界y值相等且横边有交集 if virus.rect.bottom == self._aircraft.rect.top: if not Main.have_intersection( (virus.rect.left, virus.rect.right), (self._aircraft.rect.left, self._aircraft.rect.right)): self._judge_state_of_aircraft() # sys.exit()#改成飞行器变大 # 病毒左边界与飞行器右边界x值相等且纵边有交集 if virus.rect.left == self._aircraft.rect.right: if not Main.have_intersection( (virus.rect.top, virus.rect.bottom), (self._aircraft.rect.top, self._aircraft.rect.bottom)): self._judge_state_of_aircraft() # sys.exit()#改成飞行器变大 # 病毒有边界与飞行器左边界x值相等且纵边有交集 if virus.rect.right == self._aircraft.rect.left: if not Main.have_intersection( (virus.rect.top, virus.rect.bottom), (self._aircraft.rect.top, self._aircraft.rect.bottom)): self._judge_state_of_aircraft() # sys.exit() # 改成飞行器变大 # 飞行器,子弹和病毒应该时刻在屏幕上绘制 if self._start_playing: self._aircraft.blitAircraft() # 绘制所有病毒 for virus in self._viruses.sprites(): virus.blit_virus() # 在飞行器和病毒后面重绘所有子弹 for bullet in self._bullets.sprites(): # 返回编组中的所有精灵的列表 bullet.draw_Bullet() if self._is_game_over: self._game_over_button.draw_button() #将玩家本次游戏数据导入数据库,输出数据库信息到历史记录按键后出现的面板上 if not self._is_finished_storing: with Score(name=self._usrname) as score: score.virus1 = self._virus1_num score.virus2 = self._virus2_num score.virus3 = self._virus3_num score.scoring() score.create_data() # 如果用户第一次玩,需要先为此用户建立数据表 score.update_table() score.update_usr_info() # 将存储数据标志置True self._is_finished_storing = True # 将用户的历史信息更新到历史记录按键后出现的面板上,需要呈现的是 if self._look_over_history: with Score(name=self._usrname) as score: #将历史记录打印到面板上 content = score.outprint(self._usrname) # print(content) self._score_screen.msg_set(*content) self._score_screen.draw_button() # 将用户的历史信息更新到历史记录按键后出现的面板上,需要呈现的是 if self._look_over_history2: with Score(name=self._usrname) as score: # 将历史记录打印到面板上 content = score.outprint_usr_info() # print(content) self._score_screen2.msg_set(*content) self._score_screen2.draw_button() if self._need_congratulation: self._congratulations.draw_button()
def _event_checking(self): """ 事件队列检查 :return: None """ #判断光标是否在按钮区域中 def mouse_in_button_region(mouse_x, mouse_y, button): station1 = mouse_x >= button.rect.left station2 = mouse_x <= button.rect.right station3 = mouse_y >= button.rect.top station4 = mouse_y <= button.rect.bottom return station1 and station2 and station3 and station4 #鼠标位置定位 mouse_pressed = pygame.mouse.get_pressed() #先选游戏等级再开始游戏 if mouse_pressed[0] == 1: mouse_x, mouse_y = pygame.mouse.get_pos() if mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._level_button1): self._NumOfViruses = Main.level_virus['easy'] self._level_button1.pressed = True # 病毒初始位置生成器 self._init_posxes = PositionInit( low=self._screen.get_rect().left, high=self._screen.get_rect().right - Settings().boundary_pos, count=self._NumOfViruses) # elif mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._level_button2): self._NumOfViruses = Main.level_virus['middle'] self._level_button2.pressed = True # 病毒初始位置生成器 self._init_posxes = PositionInit( low=self._screen.get_rect().left, high=self._screen.get_rect().right - Settings().boundary_pos, count=self._NumOfViruses) # elif mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._level_button3): self._NumOfViruses = Main.level_virus['defficult'] self._level_button3.pressed = True # 病毒初始位置生成器 self._init_posxes = PositionInit( low=self._screen.get_rect().left, high=self._screen.get_rect().right - Settings().boundary_pos, count=self._NumOfViruses) # if mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._history_record): if not self._look_over_history: self._look_over_history = True else: self._look_over_history = False if mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._history_record2): if not self._look_over_history2: self._look_over_history2 = True else: self._look_over_history2 = False if mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._button): self._start_playing = True self._bullets = Group() # 创建存储子弹的编组 self._viruses = Group() # 创建存储病毒的编组 #重置 if mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._reset_button): self._reset = True #游戏不暂停了 self._pause_playing = False #游戏表示为未开始 self._start_playing = False #庆祝标语不显示 self._need_congratulation = False #game over标语不显示 self._is_game_over = False #游戏等级重置 self._level_button1.pressed = False self._level_button2.pressed = False self._level_button3.pressed = False #分数清零 self._total_score = 0 self._virus1_num = 0 self._virus2_num = 0 self._virus3_num = 0 #记分牌清零 self._score_boarder.msg = 0 #飞船置于底端中心,大小为原始大小 self._aircraft.reset() #子弹清零 del self._bullets #病毒清零 del self._viruses self._virus_count = 0 #游戏等级按键复原 for button in [ self._level_button1, self._level_button2, self._level_button3 ]: if button.pressed: button.pressed = False #用户输入名处,需要修改 if mouse_in_button_region(mouse_x=mouse_x, mouse_y=mouse_y, button=self._usr): self._can_input_usrname = True # keys_pressed = pygame.key.get_pressed() if self._can_input_usrname: content = self._usr.input_event_checking(keys_pressed) # print(content) if content == 'finish': self._usrname = self._usr.username # self._usr.draw_button() self._can_input_usrname = False elif content != 'delete': self._usr.text_change(added_word=content) # self._usr.draw_button() else: self._usr.text_change(is_delete=True) # self._usr.draw_button() else: # 游戏是否暂停 if keys_pressed[pygame.K_p]: if not self._pause_playing: self._pause_playing = True else: self._pause_playing = False # 退出键 if keys_pressed[pygame.K_q]: print('消灭病毒数量为:', self._total_score) print('消灭v1病毒数为{0},v2病毒数为{1},v3病毒数为{2}'.format( self._virus1_num, self._virus2_num, self._virus3_num)) sys.exit() #用户直接按q键时成绩不计入,直接退出 for event in pygame.event.get(): if event.type == pygame.QUIT: print('消灭病毒数量为:', self._total_score) print('消灭v1病毒数为{0},v2病毒数为{1},v3病毒数为{2}'.format( self._virus1_num, self._virus2_num, self._virus3_num)) sys.exit() #用户按退出键时成绩不计入,直接退出 if self._start_playing and not self._pause_playing: # 对飞行器 if keys_pressed[pygame.K_RIGHT]: self._can_right = True else: self._can_right = False if keys_pressed[pygame.K_LEFT]: self._can_left = True else: self._can_left = False if keys_pressed[pygame.K_UP]: self._can_up = True else: self._can_up = False if keys_pressed[pygame.K_DOWN]: self._can_down = True else: self._can_down = False if keys_pressed[pygame.K_1]: if not self._is_original: self._aircraft.change_size( pos=(self._aircraft.rect.centerx, self._aircraft.rect.centery), size=self._size_small) self._is_original = True else: self._aircraft.change_size( pos=(self._aircraft.rect.centerx, self._aircraft.rect.centery), size=self._size_original) self._is_original = False # 对子弹 if keys_pressed[pygame.K_SPACE]: new_bullet = Bullet(screen=self._screen, aircraft=self._aircraft) self._bullets.add(new_bullet) # 将新子弹加入编组进行管理