def __init__(self): #初始化游戏 pygame.init() # 屏幕宽度,高度 self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载 # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # bg_ready = pygame.image.load(constants.BG_IMG_READY) #图片一样不用实现 # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取游戏标题的宽度和高度 t_width, t_height = self.img_game_title.get_size() # 标题摆放的位置 self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() # 获取按钮的宽度和高度 btn_width, btn_height = self.btn_start.get_size() # 开始按钮摆放的位置 self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) #游戏文字对象 self.score_font = pygame.font.Font(constants.FONT_SCORE, 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) # 循环播放音乐 pygame.mixer.music.play(-1) # 设置音量的大小 pygame.mixer.music.set_volume(0.2) #我放飞机实例 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() #上次按的键盘的某一键,用于控制飞机 self.key_down = None
def __init__(self): # 游戏初始化 pygame.init() """屏幕设定""" # 设置屏幕大小,这里根据游戏背景图片大小即可 self.width, self.height = 480, 852 # 获取屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置屏幕标题 pygame.display.set_caption('飞机大战') """图片设定""" # 加载背景图片,获取背景surface对象 self.bg_img = pygame.image.load(constants.BG_IMG) # 加载游戏开始标题, 设置图片位置 self.game_start_title = pygame.image.load( constants.GAME_START_TITLE_IMG) self.game_start_title_rect = self.game_start_title.get_rect() t_width, t_height = self.game_start_title.get_size() self.game_start_title_rect.topleft = (int( (self.width - t_width) / 2), int((self.height / 2 - t_height))) # 加载游戏开始按钮,设置按钮位置 self.game_start_btn = pygame.image.load(constants.GAME_START_BTN_IMG) self.game_start_btn_rect = self.game_start_btn.get_rect() b_width, b_height = self.game_start_btn.get_size() self.game_start_btn_rect.topleft = (int( (self.width - b_width) / 2), int((self.height / 2 + b_height))) # 加载游戏结束图片 self.game_over_img = pygame.image.load(constants.GAME_OVER_IMAGE) # 加载得分字体 self.score_font = pygame.font.SysFont("songtittc", 21, False, False) """音乐设定""" self.play_bg_music() """游戏状态设定""" self.status = self.READY """调节主循环运行帧数""" # 获取游戏时钟对象 self.clock = pygame.time.Clock() # 设置主循环计数变量 self.frame = 0 """创建我方飞机""" # 创建我方飞机 self.our_plane = OurPlane(self.screen) """创建敌方飞机""" # 创建敌方精灵组 self.enemies = pygame.sprite.Group() # 创建敌方小型飞机精灵族 self.small_enemy_planes = pygame.sprite.Group() """创建游戏结果实例""" self.result = PlaneResult() """上一次键盘按键,用于移动优化""" self.key_down = None
def __init__(self): # 初始化游戏 pygame.init() # 指定屏幕高度、宽度 self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 加载游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 标题高度和宽度 self.t_width, self.t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - self.t_width) / 2), int(self.height / 2 - self.t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() self.btn_width, self.btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int((self.width - self.btn_width) / 2), int(self.height / 2 + self.btn_height)) # 游戏文字对象 self.score_font = pygame.font.SysFont('arial', 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) # 无限循环播放 pygame.mixer.music.play(-1) # 设置音量 pygame.mixer.music.set_volume(0.3) # 上次按的键盘 self.key_down = None # 实例化飞机对象 self.our_plane = OurPlane(self.screen, speed=8) self.clock = pygame.time.Clock()
def __init__(self): pygame.init() self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption('Ryna的飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMAGE) self.bg_over = pygame.image.load(constants.BG_IMAGE_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 宽度和高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # ryna头像 self.img_ryna = pygame.image.load(constants.IMG_RYNA) self.img_ryna_rect = self.img_ryna.get_rect() r_width, r_height = self.img_ryna.get_size() self.img_ryna_rect.topleft = (int( (self.width - r_width) / 2), int(self.height / 4 - r_height)) # 游戏文字对象 self.score_font = pygame.font.Font(constants.PLAY_FONT, 40) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) pygame.mixer.music.set_volume(0.2) # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() # 上次按的键盘上的某一个键,用于控制飞机 self.key_down = None
def __init__(self): pygame.init() self.width, self.height = 480, 852 # 屏幕对象,设计窗口大小 self.screen = pygame.display.set_mode((self.width, self.height)) # 设计窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 游戏标题的宽和高 self.t_width, self.t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - self.t_width) / 2), int((self.height - self.t_height) / 2 - 40)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START) self.btn_start_rect = self.btn_start.get_rect() self.btn_width, self.btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - self.btn_width) / 2), int((self.height / 2 + self.btn_height))) # 游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限的循环 pygame.mixer.music.set_volume(0.2) # 我放飞机对象 self.our_plane = OurPlane(self.screen, speed=5) self.clock = pygame.time.Clock() # 上次按得键盘上的某一个,用于控制飞机 self.key_down = None
def __init__(self): # 初始化游戏 pygame.init() self.width, self.height = 480, 852 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 标题图片 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # t_width, t_height = img_game_title_rect.get_size() # 获取size值 self.img_game_title_rect.topleft = (int((self.width - self.img_game_title_rect[2]) / 2), # X值 int(self.height / 2 - self.img_game_title_rect[3])) # Y值 # 开始按钮图片 self.img_game_start_btn = pygame.image.load(constants.IMG_GAME_START_BTN) self.img_game_start_btn_rect = self.img_game_start_btn.get_rect() self.b_width, self. b_height = self.img_game_start_btn.get_size() # 获取size值 self.img_game_start_btn_rect.topleft = (int((self.width - self.b_width) / 2), # X值 int(self.height / 2 + self.b_height) # Y值 ) # 游戏文字对象 self.score_font = pygame.font.SysFont('华文新魏', 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 循环播放 pygame.mixer.music.set_volume(0.2) # 设置音量 #我方飞机对象 self.our_plane = our_plane = OurPlane(self.screen, 3) self.clock = pygame.time.Clock() # 上次按的键盘上的某一个键,用于连续控制飞机 self.key_nextly = None
def __init__(self): """ 将所有需要初始化的东西初始化一遍 """ # 忘记函数应该调用什么元素时,应该将pygame官网打开进行查阅 # 初始化游戏 pygame.init() self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 宽度和高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # 游戏文字对象 self.score_font = pygame.font.Font(constants.TEXT_FONT, 35) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限循环播放 pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() # 保存上次按下的键 self.key_down = None
def __init__(self): # 初始化 pygame.init() # 设置宽度高度 self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取游戏标题的宽度高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int((self.height - t_height) / 2)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() # 获取开始按钮的宽度高度 self.btn_width, self.btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - self.btn_width) / 2), int(self.height / 2 + self.btn_height + 20)) # 游戏文字对象 self.score_font = pygame.font.SysFont('kaiti', 50) # 加载背景音乐 # pygame.mixer.music.load(constants.BG_MUSIC) # pygame.mixer.music.play(-1) # 设置循环播放 # pygame.mixer.music.set_volume(0.2) # 音量设置 # 设置窗口标题 pygame.display.set_caption('飞机大战') # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=20) # 实例化游戏结果 self.rest = PlayRest() self.clock = pygame.time.Clock() # 上次按的键盘的某一个键 self.key_down = None
def __init__(self): # 初始化游戏 pygame.init() self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(BG_IMG) self.bg_over = pygame.image.load(BG_IMG_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 宽度和高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # 游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # 加载背景音乐 # pygame.mixer.music.load(BG_MUSIC) # pygame.mixer.music.play(-1) # 无限循环播放 # pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() # 上次按的键盘上的某一个键,用于控制飞机 self.key_down = None
def __init__(self): # 初始化游戏 pygame.init() # 屏幕对象 self.width, self.height = 480, 852 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('Plane War') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取背景宽度和高度 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int(self.width - t_width) / 2, int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int(self.width - btn_width) / 2, int(self.height / 2 + btn_height)) # 游戏文字对象 self.score_font = pygame.font.SysFont('Yahei Consolas Hybrid', 20) self.score_font_all = pygame.font.SysFont('Yahei Consolas Hybrid', 80) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 循环播放 pygame.mixer.music.set_volume(0.8) # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=10) self.clock = pygame.time.Clock() # 上次按的键盘的按键 self.key_down = None
def __init__(self): # 初始化游戏 pygame.init() # 屏幕宽高 self.width, self.height = 480, 852 # 打开屏幕 self.screen = pygame.display.set_mode((self.width, self.height)) # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) # 加载结束图片 self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 加载开始标题图片 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 标题的宽高 t_width, t_height = self.img_game_title.get_size() # 设置标题的位置 self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int((self.height - t_height) / 2)) # 加载开始按钮 self.img_game_start_btn = pygame.image.load( constants.IMG_GAME_START_BTN) self.img_game_start_btn_rect = self.img_game_start_btn.get_rect() btn_width, btn_height = self.img_game_start_btn.get_size() # 设置按钮开始按钮位置 self.img_game_start_btn_rect.topleft = (int( (self.width - btn_width) / 2), int( (self.height - btn_height) / 2) + 100) # 游戏文字对象 self.score_font = pygame.font.SysFont('fangsong', 32) # 加载背景音乐 pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.set_volume(0.8) pygame.mixer.music.play(-1) # 装载我方飞机 self.our_plan = OurPlane(self.screen, 8) self.clock = pygame.time.Clock()
def __init__(self): # 初始化游戏 pygame.init() self.width, self.height = 480, 852 # 加载游戏屏幕 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置标题 pygame.display.set_caption('PlaneWar') # 背景图片 self.bg = pygame.image.load(constants.BG_IMG) # 结束背景 self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 开始标题的位置设置 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮的位置设置 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() b_width, b_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - b_width) / 2), int(self.height / 2 + b_height)) # 游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # background music pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限循环播放 pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=8) # 设置帧数 self.clock = pygame.time.Clock() self.key_down = None
def __init__(self): #初始化游戏 pygame.init() self.width, self.height = 480, 852 # 屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置标题 pygame.display.set_caption('飞机大战') # 加载背景图片 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏的标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取标题的长和宽 t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 开始按钮 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height)) #游戏文字对象 self.score_font = pygame.font.SysFont('方正兰亭超细黑简体', 32) #我方飞机对象 self.our_plane = OurPlane(self.screen, speed=4) self.clock = pygame.time.Clock() #上次按的键盘上的某一个键,用于控制飞机 self.key_down = None
def __init__(self): #初始化游戏 pygame.init() self.width, self.height = 480, 852 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption("飞机大战") # 加载背景 self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 游戏标题 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # 获取图片位置对象 t_width, t_height = self.img_game_title.get_size() # 获取图片宽度和高度 self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height) ) # 设置图片的位置 # 开始按钮, self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int( (self.width - btn_width) / 2), int(self.height / 2 + btn_height) ) # 设置图片的位置 #游戏文字对象 self.score_font = pygame.font.SysFont('华文隶书', 32) # # 背景音乐 # pygame.mixer.music.load(constants.BG_MUSIC) # # 循环播放 # pygame.mixer.music.play(-1) # # 音量 # pygame.mixer.music.set_volume(0.2) #我方飞机对象 self.our_plane = OurPlane(self.screen, speed=20) self.clock = pygame.time.Clock() self.key_down = None
def __init__(self): pygame.init() self.width, self.height = 480, 852 self.screen = pygame.display.set_mode((self.width, self.height)) #name of window pygame.display.set_caption('Aircraft Game') # load background images self.bg = pygame.image.load(constants.BG_IMG) self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # title self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) self.img_game_title_rect = self.img_game_title.get_rect() # start button self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) self.btn_start_rect = self.btn_start.get_rect() btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int((self.width - btn_width) / 2), int(self.height / 2 + btn_height)) # font self.score_font = pygame.font.SysFont('test_sans', 32) # load music pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # infinite loop for playing music pygame.mixer.music.set_volume(0.2) # set the volume # our plane self.our_plane = OurPlane(self.screen, speed = 40) self.clock = pygame.time.Clock() # use keyboard to control aircraft self.key_down = None
def __init__(self): # pgame游戏初始化 pygame.init() # 设置屏幕游戏宽度和高度 self.width, self.height = 480, 852 # 加载屏幕对象 self.screen = pygame.display.set_mode((self.width, self.height)) # 设置窗口标题 pygame.display.set_caption('飞机大战') # 加载游戏图标 gameIcon = pygame.image.load(constants.GAME_ICON_IMG) pygame.display.set_icon(gameIcon) # 加载游戏背景图片 self.bg = pygame.image.load(constants.BG_IMG) # 加载游戏结束背景图片 self.bg_over = pygame.image.load(constants.BG_IMG_OVER) # 加载游戏标题图片 self.img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) # 设置游戏标题宽度和高度 self.img_game_title_rect = self.img_game_title.get_rect() t_width, t_height = self.img_game_title.get_size() self.img_game_title_rect.topleft = (int( (self.width - t_width) / 2), int(self.height / 2 - t_height)) # 加载游戏开始按钮图片 self.btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) # 设置游戏开始按钮宽度和高度 self.btn_start_rect = self.btn_start.get_rect() self.btn_width, btn_height = self.btn_start.get_size() self.btn_start_rect.topleft = (int((self.width - self.btn_width) / 2), int(self.height / 2 + btn_height)) # 加载暂停按钮图片 self.btn_paused_nor = pygame.image.load( constants.IMG_GAME_PAUSE_NOR_BTN) self.btn_paused = pygame.image.load(constants.IMG_GAME_PAUSE_BTN) # 加载暂停恢复按钮图片 self.btn_resume_nor = pygame.image.load( constants.IMG_GAME_RESUME_NOR_BTN) self.btn_resume = pygame.image.load(constants.IMG_GAME_RESUME_BTN) # 暂停键按钮范围值 self.paused_rect = self.btn_paused_nor.get_rect() self.paused_rect.left, self.paused_rect.top = self.width - self.paused_rect.width - 10, 10 self.paused_image = self.btn_paused_nor self.resume_image = self.btn_resume_nor # 加载暂停标识符 self.img_game_paused = pygame.image.load(constants.IMG_GAME_STOP) # 暂停标识符位置 self.img_game_paused_rect = self.img_game_paused.get_rect() # 游戏文字对象 self.score_font = pygame.font.Font(constants.TEXT_SCORE_FONT, 35) # 加载背景音乐s pygame.mixer.music.load(constants.BG_MUSIC) pygame.mixer.music.play(-1) # 无限循环播放 pygame.mixer.music.set_volume(0.2) # 设置音量 # 我方飞机对象 self.our_plane = OurPlane(self.screen, speed=5) # 炸弹补给对象 self.bomb = BombSupply(self.screen, 3) # 炸弹包每隔一定随机出现 self.bomb_appear_time = pygame.USEREVENT pygame.time.set_timer(self.bomb_appear_time, constants.BOMB_APPEAR_TIME) # 血包补给对象 self.hp_supply = HPSupply(self.screen, 3) # 子弹补给对象 self.bullet_supply = BulletsSupply(self.screen, 3) # 炸弹包每隔一定随机出现 self.hp_supply_appear_time = pygame.USEREVENT + 1 pygame.time.set_timer(self.hp_supply_appear_time, constants.HP_APPEAR_TIME) # 设置帧速率 self.clock = pygame.time.Clock() # 上次按的键盘上的某一个键,用于控制飞机 self.key_down = None
def main(): pygame.init() width, height = 480, 852 #设置游戏屏幕宽和高 screen = pygame.display.set_mode((width, height)) #加载屏幕对象 pygame.display.set_caption('飞机大战') #设置标题 bg = pygame.image.load(constants.BG_IMG) #加载游戏背景图片 img_game_title = pygame.image.load(constants.IMG_GAME_TITLE) #加载游戏标题的图片 t_width, t_height = img_game_title.get_size() #获取游戏标题图片的宽和高 img_game_title_rect = img_game_title.get_rect() #获取游戏标题图片的位置 img_game_title_rect.left = (width - t_width) // 2 #使游戏标题图片位于屏幕中央 img_game_title_rect.top = (height // 2 - t_height) #使游戏标题图片位于屏幕中央 btn_start = pygame.image.load(constants.IMG_GAME_START_BTN) #获取开始按钮 btn_width, btn_height = btn_start.get_size() #获取开始按钮的宽高 btn_start_rect = btn_start.get_rect() #获取开始按钮的位置 btn_start_rect.left = (width - btn_width) // 2 #使开始按钮位于屏幕中央 btn_start_rect.top = (height // 2 + btn_height) #使开始按钮位于屏幕中央 pygame.mixer.music.load(constants.BG_MUSIC) #加载游戏背景音乐 pygame.mixer.music.play(-1) #设置无限播放模式 '''设置游戏的状态,并将状态设置为准备''' READY = 0 PLAYING = 1 OVER = 2 status = READY frame = 0 #设置帧数变量 ourplane = OurPlane(screen) clock = pygame.time.Clock() #设置帧数 while True: clock.tick(60) frame += 1 if frame >= 60: frame = 0 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if status == READY: status = PLAYING elif event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_UP: ourplane.move_up() elif event.key == pygame.K_s or event.key == pygame.K_DOWN: ourplane.move_down() elif event.key == pygame.K_a or event.key == pygame.K_LEFT: ourplane.move_left() elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: ourplane.move_right() elif event.key == pygame.K_SPACE: ourplane.shoot() if status == READY: screen.blit(bg, bg.get_rect()) screen.blit(img_game_title, img_game_title_rect) screen.blit(btn_start, btn_start_rect) elif status == PLAYING: screen.blit(bg, bg.get_rect()) ourplane.update(frame) ourplane.bullets.update() pygame.display.flip()
def main(): """ 游戏入口,main方法 """ pygame.init() # 游戏状态 # 0 代表等待中 # 1 代表游戏中 # 2 代表游戏结束 READY = 0 PLAYING = 1 END = 2 # 窗口窗口大小配置 width, height = 480, 852 # 创建屏幕对象 screen = pygame.display.set_mode((width, height)) # 设置窗口名称 pygame.display.set_caption('打飞机') # 加载背景图片 bg = pygame.image.load(BG_IMG) bg_over = pygame.image.load(BG_IMG_OVER) # 加载标题图片 title_img = pygame.image.load(TITLE_IMG) # 加载开始按钮图片 start_btn_img = pygame.image.load(START_BTN_IMG) # 加载背景音乐 pygame.mixer.music.load(BG_MUSIC) # 设置背景音乐的音量 pygame.mixer.music.set_volume(0.2) # 无限循环播放背景音乐 pygame.mixer.music.play(-1) # 获取游戏标题图片位置 title_img_rect = title_img.get_rect() # 获取游戏标题图片尺寸 title_img_width, title_img_height = title_img.get_size() # 设置初始位置 title_img_rect.topleft = ((width - title_img_width) / 2, (height - title_img_height) / 4) # 获取游戏开始按钮图片位置 start_btn_img_rect = start_btn_img.get_rect() start_btn_img_width, start_btn_img_height = start_btn_img.get_size() # 设置初始位置 start_btn_img_rect.topleft = ((width - start_btn_img_width) / 2, (height - start_btn_img_height) / 2) # 初始化状态 status = READY # 帧数计数器 frame = 0 clock = pygame.time.Clock() # 创建积分实例 score = Score() # 我方飞机实例 our_plane = OurPlane(screen) # 创建敌机精灵组 enemies_group = pygame.sprite.Group() # 生成小型敌方飞机 for i in range(6): small_enemy_plane = SmallEnemyPlane(screen) enemies_group.add(small_enemy_plane) # 创建积分文字 score_font = pygame.font.Font('./demo/my_font.ttf', 32) # 获取显示屏的宽高 s_wd, s_ht = screen.get_size() # 记录键盘按下的按钮 key_down = None while True: clock.tick(60) frame += 1 if frame == 60: frame = 0 for event in pygame.event.get(): # 退出游戏 if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if status == READY: status = PLAYING elif event.type == pygame.KEYDOWN: if status == PLAYING: key_down = event.key if event.key == pygame.K_DOWN: our_plane.move_down() elif event.key == pygame.K_UP: our_plane.move_up() elif event.key == pygame.K_LEFT: our_plane.move_left() elif event.key == pygame.K_RIGHT: our_plane.move_right() elif event.key == pygame.K_SPACE: our_plane.shoot() # 准备状态 if status == READY: screen.blit(bg, bg.get_rect()) screen.blit(title_img, title_img_rect) screen.blit(start_btn_img, start_btn_img_rect) elif status == PLAYING: screen.blit(bg, bg.get_rect()) our_plane.blit_self() # 更新飞机 if our_plane.update(frame, enemies_group, key_down): status = END # 更新绘制子弹 our_plane.bullets.update(enemies_group, score) enemies_group.update() # 绘制当前的积分 score_text = score_font.render('积分: {}'.format(score.score), False, SCORE_COLOR) screen.blit(score_text, (10, 10)) elif status == END: screen.blit(bg_over, bg_over.get_rect()) # 显示此次积分数值 score_text = score_font.render('{}'.format(score.score), False, SCORE_COLOR) score_text_wd, score_text_ht = score_text.get_size() screen.blit(score_text, ((s_wd - score_text_wd) / 2, (s_ht - score_text_ht) / 2)) # 绘制 pygame.display.flip()