pygame:简易游戏(飞机大战)

import math
import random
import pygame
import sys

# pygame初始化
pygame.init()

# 得分初始化
score = 0

# 创建surface主窗体
screen = pygame.display.set_mode((1061, 658))
pygame.display.set_caption('planeGame 1.0 author:Keven Duan')   # 给主窗体命名

# 添加背景音乐
pygame.mixer.music.load('bg.mp3')
pygame.mixer.music.play(-1)
baopo = pygame.mixer.Sound("baopo.mp3")

# 导入素材图片
icon = pygame.image.load('planeicon.png')
bg = pygame.image.load("background.png")
plane = pygame.image.load("plane.png")
gameover_img = pygame.image.load('gameover.png')
new_gameover = pygame.transform.scale(gameover_img, (500, 500)) # 调整图片大小
pygame.display.set_icon(icon)   # 设置图标

# 坐标的设置
#plane
planeX = 435
planeY = 450
planeStep = 0 # 飞机移动的参数

# 创建ufo类
class UFO():
    def __init__(self):
        self.img = pygame.image.load("ufo.png")
        self.x = random.randint(0, 800)
        self.y = random.randint(0, 200)
        self.step = 0.1*random.randint(5, 20)

    def reset(self):    # 重新加载坐标点
        self.x = random.randint(0, 800)
        self.y = random.randint(0, 200)

# 创建子弹类
class Bulltet():
    def __init__(self):
        self.img = pygame.image.load("bullet.png")
        self.x = planeX + 65
        self.y = planeY - 10
        self.step = 1

    def hit(self):
        for u in ufo_list:
            if distance(u.x, u.y, self.x, self.y) < 30:
                global score
                score += 1
                baopo.play()
                bulltets.remove(self)
                u.reset()

bulltets = []

# 创建UFO对象
ufo_list=[]
def create_ufo():
    ufo_num = 8
    for i in range(ufo_num):
        ufo_list.append(UFO())
create_ufo()

# 两点之间距离
def distance(ux ,uy, bx, by):
    a = bx - ux
    b = by - uy
    return math.sqrt(a * a + b * b)

# 子弹的移动
def show_belltet():
    for b in bulltets:
        screen.blit(b.img, (b.x, b.y))
        b.y -= b.step
        b.hit()  # 每次显示判断是否击中
        if b.y < 0:
            bulltets.remove(b)

# 飞机的移动与添加
def plane_move():
    global planeX
    # 控制飞机横坐标
    planeX -= planeStep
    # 防止飞机出界
    if planeX > 880:
        planeX = 880
        gameover()
    if planeX < 0:
        planeX = 0
        gameover()

    screen.blit(plane, (planeX, planeY))  # 添加飞机

# ufo移动与添加
def ufo_move():
    global ufo_list
    for e in ufo_list:
        screen.blit(e.img, (e.x, e.y))  # 添加ufo
        e.x += e.step
        # ufo 循环运动
        if e.x < 0:
            e.step *= -1
            e.y += 30
        elif e.x > 880:
            e.step *= -1
            e.y += 30
        elif e.y > planeY:
            gameover()
            ufo_list = []
            create_ufo()

# 游戏失败画面
def gameover():
    """
    游戏结束界面
    :param planeX: 飞机的横坐标
    :return: None
    """
    global score, ufo_list
    screen.blit(new_gameover, (290, 100))
    score = 0

# 循环游戏体
while True:
    screen.blit(bg, (0, 0)) #添加背景图
    # 创建文字对象
    ft = pygame.font.SysFont(['方正粗黑宋简体', 'microsoftsansserif'], 40)
    text = ft.render(f"score:{score}", True, (0, 255, 0))
    screen.blit(text, (10, 10))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                planeStep = 3
            elif event.key == pygame.K_RIGHT:
                planeStep = -3
            elif event.key == pygame.K_SPACE:
                # 添加子弹到列表
                bulltets.append(Bulltet())

    if score == 100:
        score = 1000
    ufo_move() # ufo的添加与移动
    plane_move()  # 飞机的添加与移动
    show_belltet()  # 显示子弹
    # 刷新界面
    pygame.display.update()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Keven1Duan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值