python开发飞机大战游戏

Farrah ·
更新时间:2024-09-21
· 275 次阅读

本文实例为大家分享了python开发飞机大战游戏的具体代码,供大家参考,具体内容如下

import pygame import random import math # 数学模块 # 初始化界面 pygame.init() # 设置窗口大小 windows = pygame.display.set_mode((800, 600)) # 设置窗口标题 pygame.display.set_caption("小赵同学") # 引入图片 logo icon = pygame.image.load('logo.jpg') pygame.display.set_icon(icon) # 4.游戏获取背景 bgcolor = pygame.image.load('bj.png') # 5.设置玩家飞机 playerimg = pygame.image.load('fj.png') X = 350 # 设置玩家X轴 Y = 480 # 设置玩家Y轴 # 停止移动就可以将palyerStep改为0。控制一个变量来指定飞机指定移动 playerStep = 0 # 添加背景音乐 pygame.mixer.music.load('bj.mp3') pygame.mixer.music.play(-1) # 添加射中的音效 # bao_music = pygame.mixer.Sound('bj.mp3') # 分数 score = 0 # 添加字体和大小 font = pygame.font.Font('freesansbold.ttf', 32) # 字体类 def show_score(): # 显示的文字 text = f"Score:{score}" # 渲染然后显示 显示text True表示24位的字 score_render = font.render(text, True, (0, 255, 0)) # 指定字体放到那个位置 windows.blit(score_render, (10, 10)) # 游戏结束的变量 over = False over_font = pygame.font.Font('freesansbold.ttf', 64) # 结束的提示语 def check_over(): if over: text = "Game Over" render = font.render(text, True, (255, 0, 0)) windows.blit(render, (320, 200)) # 8.添加敌人. # 11.添加多个敌人 number_enemy = 6 # 敌人类 class Enemy: def __init__(self): # self.img = pygame.image.load('enemy.png') self.x = random.randint(200, 600) # 随机产生X self.y = random.randint(50, 250) # 随机产生Y self.step = random.randint(2, 4) # 随机产生速度 # 当被射中时恢复位置 def reset(self): self.x = random.randint(200, 600) self.y = random.randint(50, 180) def distance(bx, by, ex, ey): a = bx - ex b = by - ey return math.sqrt(a * a + b * b) # 开根号 # 保存所有的敌人 enemis = [] for i in range(number_enemy): # 每次循环都都在class Enemy中过一边,所以随机产生一个敌人的参数并且保存到列表中 enemis.append(Enemy()) # 显示敌人并且实现敌人的移动下沉 def enemy(): # 循环保存敌人的列表,每个敌人都过在这个for循环里被限制了移动的轨迹 global over for e in enemis: windows.blit(e.img, (e.x, e.y)) e.x += e.step if e.x > 750 or e.x < 0: # 判断敌人是否到了边界 e.step *= -1 # 敌人碰到界面往返 e.y += 40 # 设置敌人往下沉 # 判断敌人的位置如果到达指定的地方则游戏结束 if e.y > 436: over = True print("游戏结束啦") enemis.clear() # 设置飞机及飞机移动范围的函数 == 飞机类型 def fiji_type(): # 设置飞机的坐标和飞机X Y轴最大的移动位置 global X, Y # 5. 设置飞机 windows.blit(playerimg, (X, Y)) # 6.飞机移动 X += plagerStep # 预防飞机出界 if X > 680: X = 680 if X < 0: X = 0 # 子弹的类 class Bullet: def __init__(self): self.img = pygame.image.load('bullet.png') self.x = X + 55 # 设置子弹的X轴 self.y = Y + 5 # 子弹出现在玩家的上方 self.step = 2 # 子弹移动的速度 # 击中敌人 def hit(self): global score for e in enemis: if distance(self.x, self.y, e.x, e.y) < 30: # 射中了 bullets.remove(self) e.reset() # 重置敌人 # 没击中加10分 score += 10 bullets = [] # 保存现有的子弹 # 显示子弹移动 def show_bullets(): for b in bullets: windows.blit(b.img, (b.x, b.y)) b.hit() # 查看是否击中了敌人 b.y -= b.step # 往上移动 # 判断子弹是否出了界面 if b.y < 0: bullets.remove(b) # 3.游戏主循环 running = True while running: # 4.背景 # 每个循环是画一张画组成的 # 画出来bgcolor windows.blit(bgcolor, (0, 0)) # 调用这个字体 show_score() # event.get操作事件 for event in pygame.event.get(): # 判断操作类型是不是QUIT if event.type == pygame.QUIT: # 如果程序为False就会停止则关闭 running = False # 7.控制飞机的移动 # 通过控制键盘的事件来控制(playerStep值)飞机的移动 if event.type == pygame.KEYDOWN: # 判断按下键盘右键,按下则移动 if event.key == pygame.K_RIGHT: plagerStep = 3 # 判断按下左键 elif event.key == pygame.K_LEFT: plagerStep = -3 # 判断按下空格健的反应 elif event.key == pygame.K_SPACE: # 创建一个子弹 b = Bullet() bullets.append(b) # 判断松来按键停止, if event.type == pygame.KEYUP: plagerStep = 0 # 调用飞机的类型的函数 fiji_type() # 调用敌人这个函数 enemy() show_bullets() # 显示子弹 # 游戏结束语 check_over() # 刷新更新数据 pygame.display.update() # global 设置全局变量 ''' 游戏结构 1.设置窗口大小 2.背景图 3.显示飞机 4.移动飞机 5.控制出界 6.获取键盘事件 7.显示敌人 8.敌人移动 9.下沉和随机位置 10.显示多个敌人 11.响应空格键 12.添加子弹 13.发射子弹 14.射中检测之距离 15.射中检测 16.添加音效 17.添加并显示分数 18.游戏结束 19.结束提示 '''



python开发 飞机大战 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号