pygame plane war game (python big job)

 

 1. Project background

Python big homework, after checking the link given by the teacher, I found that the teaching video was incomplete, so I borrowed a project from my classmate "Python Programming from Introduction to Practice" to learn to imitate.

2. The specific introduction of the game

  • This is a small space battle game created by Huihui himself.
  • Game background: In the vast expanse of space, there is a group of Smurfs (not) a group of evil villains, and your task is to drive a spaceship to destroy them. Destroy a group of aliens, your level will be higher, and your bullet speed and enemies will become faster, try to exceed that high score!
  • Life: There are four in total, cherish life!
  • How to play: Click a on the keyboard to go left, click d on the keyboard to go right, and click the left mouse button to fire bullets.
  • Function (not recommended to press more, because it will crash): i key: control background music; o key: control sound effect; q key: stop the game.
  • Easter eggs: w key: fire bullets continuously; p key: can summon super weapons;

Three, the main code

main function:

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 17:44:22 2022

@author: 18705
"""
'''self就是创建可以引用的一个值
可以传递的实参,其他形参靠他。
同时,self.name是属性,self.name()是方法。super是父类等等。'''

import sys
from time import sleep

import pygame

from settings import Settings
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button
from buttonhelp import ButtonHelp
from ship import Ship
from bg import Bg
from bullet import Bullet
from alien import Alien
from music import Music

class AlienInvasion:
    '''管理游戏资源和行为的类'''
    def __init__(self):
        '''初始化游戏并且创建游戏资源'''
        pygame.init()
        
        self.settings = Settings()
        
        self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height),pygame.RESIZABLE)
        icon = pygame.image.load("images/plant.png")
        pygame.display.set_icon(icon)
        pygame.display.set_caption("星球大战")
        #创建一个用于统计游戏信息的示例
        #并创建记分牌
        self.stats = GameStats(self)
        self.sb = Scoreboard(self)
        #外星飞船
        self.bg =Bg(self)
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()
        self.music = Music(self)
        #背景音乐
        #背景音乐的播放不能放到主循环里面
        self.music.bgm2()
        #尝试换音乐,失败了
        '''
        self.control_bgm_t = True
        if self.control_bgm_t:
            self.music.bgm2()
        else:
            self.music.bgm1()
        '''
        self.control_bgm = False
        self.n=False
        #设置背景颜色
        #self.bg_color = (230,230,230)
        #创建外星人群
        self._create_fleet()
        
        #创建Play按钮
        self.play_button = Button(self,'play')
        #创建Help按钮
        self.help_button = ButtonHelp(self,'help')
        #连发子弹
        self.control_bullet = False
        #关闭音效
        self.control_sound = True
        self.nn = False
        #大子弹
        self.control_bullet_big = False
        
    def run_game(self):
        '''开始游戏的主循环'''
        
        while True:
            self._check_events()
            if self.stats.game_active:
                self.ship.update()
                self._update_bullets()
                self._update_aliens()
                
            self._update_screen()
            #自动发射子弹
            if self.control_bullet:
                self._fire_bullet()
            #控制bgm开关
            if self.control_bgm:
                self.music.bgm_pause()
                self.n=True
            elif not self.control_bgm and self.n==True:
                self.music.bgm_unpause()
                self.n= not self.n
    def _check_events(self):
            #监视键盘和鼠标事件
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    #将高分添加到txt中
                    with open('high_score.txt','w') as file_object:
                        file_object.write(str(self.stats.high_score))
                    file_object.close()
                    pygame.quit()
                    sys.exit()
                    #设置长按移动
                elif event.type == pygame.KEYDOWN:
                    self._check_keydown_events(event)
                elif event.type == pygame.KEYUP:
                    self._check_keyup_events(event)
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    self._check_mousebuttondown_events(event)
                    
    def _check_keydown_events(self,event):
        '''相应按键'''
        if event.key == pygame.K_d:
        #向右移动飞船
            self.ship.moving_right = True
        elif event.key == pygame.K_a:
        #向左移动飞船
            self.ship.moving_left = True
        elif event.key == pygame.K_q:
            #将高分添加到txt中
            with open('high_score.txt','w') as file_object:
                file_object.write(str(self.stats.high_score))
            file_object.close()
            pygame.quit()
            sys.exit()
        elif event.key == pygame.K_w:
            self.control_bullet =not self.control_bullet 
        elif event.key == pygame.K_i:
            self.control_bgm = not self.control_bgm           
        elif event.key == pygame.K_o:
            self.control_sound = not self.control_sound 
        #elif event.key == pygame.K_p:
            #self.control_bgm_t = not self.control_bgm_t
        elif event.key == pygame.K_p:
            self.control_bullet_big = not self.control_bullet_big
            
    def _check_keyup_events(self,event):
        '''响应松开'''
        if event.key == pygame.K_d:
        #向右移动飞船
            self.ship.moving_right = False
        elif event.key == pygame.K_a:
        #向右移动飞船
            self.ship.moving_left = False
    
    def _check_mousebuttondown_events(self,event):
        '''响应鼠标'''
        if event.button == 1:
            self._fire_bullet()
        #设置play的功能和help的功能,其实应该封装一下的,哈哈哈哈。偷懒了
        button_clicked = self.play_button.rect.collidepoint(event.pos)
        buttonhelp_clicked = self.help_button.rect.collidepoint(event.pos)
        if button_clicked and not self.stats.game_active:
            #重置游戏设置
            self.settings.initialize_dynamic_settings()
            #重置游戏统计·信息
            self.stats.reset_stats()
            self.stats.game_active = True 
            self.sb.prep_score()
            self.sb.prep_level()
            self.sb.prep_ships()
            #清空余下的外星人和子弹
            self.aliens.empty()
            self.bullets.empty()
            
            #创建一群新外星人并且居中
            self._create_fleet()
            self.ship.center_ship()
            
            #隐藏鼠标光标
            pygame.mouse.set_visible(False)
        if buttonhelp_clicked and not self.stats.game_active:
            with open('help.txt',encoding='utf-8') as helptext:
                content = helptext.read()
            print(content)
            helptext.close()
    def _fire_bullet(self):
        if len(self.bullets)<self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)
            #控制子弹音效
            if self.control_sound:
                self.music.bullet()
                self.nn = True
            elif not self.control_sound and self.nn:
                self.music.bullet_stop()
                self.nn = not self.nn
    def _update_bullets(self):
         self.bullets.update()
            
            #删除消失的子弹
         for bullet in self.bullets.copy():
             if bullet.rect.bottom <=0:
                 self.bullets.remove(bullet)
         #print(len(self.bullets))
         #检查是否有子弹击中外星人
         self._check_bullet_alien_collisions()
    
    def _check_bullet_alien_collisions(self):
        '''响应子弹和外星人碰撞'''
        collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True)
        if collisions:
            for aliens in collisions.values():
                #控制子弹和外星人碰撞音效
                if self.control_sound:
                    self.music.alien()
                    self.nn = True
                elif not self.control_sound and self.nn:
                    self.music.alien_stop()
                    self.nn = not self.nn
                self.stats.score += self.settings.alien_points*len(aliens)
                self.sb.prep_score()
                self.sb.check_high_score()
                
        if not self.aliens:
             #删除现有的子弹并新建一群外星人
             self.bullets.empty()
             self._create_fleet()
             self.settings.increase_speed()
             
             #提高等级
             self.stats.level +=1
             self.sb.prep_level()
         
         
    def _update_aliens(self):
        '''更新外星人位置'''
        self._check_fleet_edges()
        self.aliens.update()
        
        #检测外星人和飞船之间的碰撞
        if pygame.sprite.spritecollideany(self.ship, self.aliens):
            #print("Ship hit!!!")
            self._ship_hit()
        
        #检查是否有外星人到达了屏幕底端
        self._check_aliens_bottom()
        
    def _create_fleet(self):
        '''创建外星人群。'''
        alien = Alien(self)
        alien_width,alien_height = alien.rect.size
        available_space_x = self.settings.screen_width - (2*alien_width)
        number_aliens_x = available_space_x // (2*alien_width)
        
        #计算屏幕可以容纳多少外星人
        ship_height = self.ship.rect.height
        available_space_y = (self.settings.screen_height - (3*alien_height) 
                             - ship_height)#每行不超过79个字符
        number_rows = available_space_y // (2*alien_height)
        
        #创建外星人群
        for row_number in range(number_rows):
            for alien_number in range(number_aliens_x):
                self._create_alien(alien_number,row_number)
            
    def _create_alien(self,alien_number,row_number):
        alien = Alien(self)
        alien_width,alien_height = alien.rect.size
        alien.x = alien_width + 2*alien_width*alien_number
        alien.rect.x = alien.x
        alien.rect.y = alien.rect.height + 2*alien.rect.height*row_number
        self.aliens.add(alien)
    
    def _check_fleet_edges(self):
        '''有外星人到达边缘采取响应的措施'''
        for alien in self.aliens.sprites():
            if alien.check_edges():
                self._change_fleet_direction()
                break
    def _change_fleet_direction(self):
        '''整群下移,改变方向'''
        for alien in self.aliens.sprites():
            alien.rect.y += self.settings.fleet_drop_speed
        self.settings.fleet_direction *=-1
        
    def _ship_hit(self):
        '''响应飞船被外星人撞到'''
        if self.stats.ships_left >0:
            
        #将ship_left减一并更新记分牌
            self.stats.ships_left -=1
            self.sb.prep_ships()
        #清空余下的外星人和子弹
            self.aliens.empty()
            self.bullets.empty()
        
        #创建一群新的外星人,并将飞船放到屏幕底端的中央
            self._create_fleet()
            self.ship.center_ship()
        #爆炸效果
            self.music.blitme()
            pygame.display.flip()
            #控制飞船和外星人碰撞音效
            if self.control_sound:
                self.music.bow()
                self.nn = True
            elif not self.control_sound and self.nn:
                self.music.bow_stop()
                self.nn = not self.nn
            
        #暂停
            sleep(2)
        else:
            self.stats.game_active = False
            pygame.mouse.set_visible(True)
    
    def _check_aliens_bottom(self):
        '''检查外星人是否到达了屏幕底端'''
        screen_rect = self.screen.get_rect()
        for alien in self.aliens.sprites():
            if alien.rect.bottom >=screen_rect.bottom:
                #像飞船悲壮一样处理
                self._ship_hit()
                break
        
    
    def _update_screen(self):
                
            #每次循环都重绘屏幕
            self.screen.fill(self.settings.bg_color)
            self.bg.action()
            # 绘制元素图片
            self.bg.blitme()
            #self.bg.blitme()
            self.ship.blitme()
            #让最近绘制的屏幕可见
            for bullet in self.bullets.sprites():
                bullet.draw_bullet()
            self.aliens.draw(self.screen)
            
            #显示得分
            self.sb.show_score()
            
            #如果游戏处于非活动状态,就绘制Play按钮
            if not self.stats.game_active:
                self.play_button.draw_button()
            if not self.stats.game_active:
                self.help_button.draw_button()
            
            pygame.display.flip()
        
if __name__ == '__main__':
    #创建游戏实例并运行游戏
    ai = AlienInvasion()
    ai.run_game()

Bullet function:

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 21:42:02 2022

@author: 18705
"""

import pygame
from pygame.sprite import Sprite


class Bullet(Sprite):
    '''管理飞船所发子弹的类'''
    def __init__(self,ai_game):
        '''在飞船当前位置创建一个子弹对象'''
        super().__init__()
        self.screen =ai_game.screen
        self.settings= ai_game.settings
        self.control_bullet_b = ai_game.control_bullet_big
        if  not self.control_bullet_b:
            
            self.color = self.settings.bullet_color
        #在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
            self.rect = pygame.Rect(0,0,self.settings.bullet_width,self.settings.bullet_height)
            self.rect.midtop = ai_game.ship.rect.midtop
        #存储用小鼠表示的子弹位置
            self.y = float(self.rect.y)
        else:
            self.color = self.settings.bullet_color_s
        #在(0,0)处创建一个表示子弹的矩形,再设置正确的位置
            self.rect = pygame.Rect(0,0,self.settings.bullet_width_s,self.settings.bullet_height_s)
            self.rect.midtop = ai_game.ship.rect.midtop
        #存储用小鼠表示的子弹位置
            self.y = float(self.rect.y)
            
    def update(self):
        self.y -= self.settings.bullet_speed
        self.rect.y = self.y
        
    def draw_bullet(self):
        pygame.draw.rect(self.screen, self.color, self.rect)

Spaceship function:

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 19:41:00 2022

@author: 18705
"""

import pygame
from pygame.sprite import Sprite

class Ship(Sprite):
    '''管理飞船的类'''
    #ai_game就是主函数,可能因为这个函数是个子函数
    def __init__(self,ai_game):
        '''初始化飞船并设置其初始位置'''
        super().__init__()
        self.screen = ai_game.screen
        #self.settings = ai_game.settings
        self.screen_rect = ai_game.screen.get_rect()
        
        #加载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')
        #self.image = pygame.transform.scale(self.image, (128, 128))
        self.rect = self.image.get_rect()
        
        #对于没搜辛飞船,都将其放在屏幕底部中央
        self.rect.midbottom = self.screen_rect.midbottom
        
        #在飞船的x属性中存储小数值
        #self.x=float(self.rect.x)
        
        #移动标志
        self.moving_right = False
        self.moving_left = False
    
    def update(self):
        '''根据移动标志调整飞船的位置'''
        #更新飞船而不是rect对象的x值
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.rect.x += 1
        if self.moving_left and self.rect.left > 0:
            self.rect.x -= 1
        
        #根据self.x更新rect对象
        #self.rect.x = self.x
    
    def blitme(self):
        '''在指定位置绘制飞船'''
        self.screen.blit(self.image,self.rect)
    
    def center_ship(self):
        '''让飞船在屏幕底端居中'''
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)

Setup function:

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 19:19:42 2022

@author: 18705
"""

class Settings:
    '''存储游戏里的所有设置类'''
    def __init__(self):
        '''初始化游戏静态设置'''
        #屏幕设置
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)
        
        
        #飞船设置
        #self.ship_speed = 1.5
        self.ship_limit = 3#飞船的数量
        
        #子弹设置
        
        self.bullet_width =5
        self.bullet_height =20
        self.bullet_color =(255,215,0)
        self.bullets_allowed = 15
        
        #超级武器设置
        self.bullet_width_s =600
        self.bullet_height_s =40
        self.bullet_color_s =(255,0,0)
        #外星人设置
        
        self.fleet_drop_speed =10
        
        #加快游戏节奏的速度
        self.speedup_scale = 1.2
        #外行人分数提高速度
        self.score_scale = 2
        
        self.initialize_dynamic_settings()
        
    def initialize_dynamic_settings(self):
        '''初始化随游戏进行而变化的设置'''
        #self.ship_speed = 1.5
        self.bullet_speed =1.5
        self.alien_speed = 1.0
        self.bg_speed = 0.2
         #1是向右,-1是向左
        self.fleet_direction = 1
        #计分
        self.alien_points = 1
        
        
    def increase_speed(self):
        '''提高速度设置和外星人分数'''
        #self.ship_speed *= self.speedup_scale
        self.bullet_speed *= self.speedup_scale
        self.alien_speed *= self.speedup_scale
        self.bg_speed *= self.speedup_scale
        
        self.alien_points = int(self.alien_points * self.score_scale)

I want all the code packages (remember to give me a little star):

GitHub - huihuihenqiang/aircraft-battle-python-: This is a small python game written in pygame, for learning purposes only . Contribute to huihuihenqiang/aircraft-battle-python- development by creating an account on GitHub. https://github.com/huihuihenqiang/aircraft-battle-python-

4. GitHub upload project

Click the "+" in the upper right corner

Then select New repository

 

 Fill in the Repository name and Description of your own project, and the others are default.

 Finally, just click on the green thing.

 Then click Upload your own project and you're done. Of course, you can also modify your own read.me to introduce your project.

 

Guess you like

Origin blog.csdn.net/m0_57491181/article/details/128250997