Cat-God-007

导航

Python小游戏 ---- 飞机大战

本文已参与[新人创作礼]活动,一起开启掘金创作之路。

为了使广大的python爱好者来到这个学习的旅馆!接下来的小游戏是希望我能够在未来看到这笔记时,能够充满兴趣,重新系统的来学习Python的pygame模块。

python小游戏 ----飞机大战游戏

效果视频(live.csdn.net/v/216071)

菜鸡小建议:

从视频效果可以看出,这个游戏还有超大的提升空间,如鼠标,音效,撞击等方面。

import pygame
import sys
import random

# 显示窗口
screen = pygame.display.set_mode((500,600))

# 标题
pygame.display.set_caption("飞机大战")

# 背景图片显示
# 图片路径
bg = pygame.image.load('./res/img_bg_level_2.jpg')
# 图片大小
bg = pygame.transform.scale(bg, (500, 1200))
# 图片位置
bgRect = bg.get_rect()
bgRect.center = (250, 600)
# 显示图片
screen.blit(bg, bgRect)

# 飞机图片显示
plane = pygame.image.load('./res/plane_blue_01.png')
plane = pygame.transform.scale(plane, (80, 100))
planeRect = plane.get_rect()
planeRect.center = (250, 400)
screen.blit(plane, planeRect)

# 子弹图片显示
bullet = pygame.image.load('./res/bullet_01.png')
bulletRect = bullet.get_rect()
bulletRect.center = planeRect.center
screen.blit(bullet, bulletRect)

# 敌机图片展示(随机出现)
planes = pygame.image.load('./res/enemy_04.png')
planesRect = planes.get_rect()
planesRect.center = (random.randrange(50,450),-100)
screen.blit(planes, planesRect)

# 道具图片(随机出现)
item = pygame.image.load('res/img_plane_item_10.png')
itemRect = item.get_rect()
itemRect.center = (random.randrange(50,450),-100)
screen.blit(item, itemRect)

# 云彩图片(随机出现)
cloud = pygame.image.load("res/yun01.png")
cloudRect = cloud.get_rect()
cloudRect.center = (random.randrange(100,400),-300)
screen.blit(cloud,cloudRect)

# # 移动背景图片
# def moveBg():
#     global bgRect
#     bgRect = bgRect.move(0,1)
#     if bgRect.top > 0:
#         bgRect.top = -600

# 移动背景图片
bgIndex = 0
def moveBg():
    global bgRect
    global bgIndex
    if bgIndex == 1:
        bgRect = bgRect.move(0, 1)
        if bgRect.top > 0:
            bgRect.top = -600
        bgIndex = 0
    bgIndex += 1

# 移动子弹图
bulletSpeed = -10
def moveBullet():
    global bulletRect
    global bulletSpeed
    bulletRect = bulletRect.move(0,bulletSpeed)
    if bulletRect.top < -50:
        bulletRect.center = planeRect.center

# 移动敌机图
def movePlanes():
    global planesRect
    planesRect = planesRect.move(0,3)
    if planesRect.top > 700:
        planesRect.center = (random.randrange(50,450),-100)

# 移动道具图
def moveItem():
    global itemRect
    itemRect = itemRect.move(0,1)
    if itemRect.top > 700:
        itemRect.center = (random.randrange(50,450),-100)

# 移动云彩图片
def moveCloud():
    global cloudRect#
    cloudRect = cloudRect.move(0,1)
    if cloudRect.top > 700:
        cloudRect.center = (random.randrange(100,400),-300)
# 子弹与敌机碰撞
def crash():
    global bulletRect
    global planesRect
    a = bulletRect.top - planesRect.top
    b = bulletRect.left - planesRect.left
    c = 50
    if c**2 > a**2+b**2:
        bulletRect.center = planeRect.center
        planesRect.center = (random.randrange(50,450),-100)


# 飞机与道具碰撞
def itemCrash():
    global planeRect
    global itemRect
    global bulletSpeed

    a = planeRect.top - itemRect.top
    b = planeRect.left - itemRect.left
    c = 50

    if c**2 > a**2+b**2:
        itemRect.center = (random.randrange(50, 450),-100)
        bulletSpeed -= 10
        if bulletSpeed < -30:
            bulletSpeed = -30
# 创建时钟对象
time = pygame.time.Clock()
# 程序循环
while True:
    # 遍历所有事件
    for event in pygame.event.get():
        # 点击叉,退出程序
        if event.type == pygame.QUIT:
            sys.exit()

        # 鼠标位置
        elif event.type == pygame.MOUSEMOTION:
            # 飞机跟鼠标
            planeRect.center = event.pos
    # 移动背景图片
    moveBg()
    # 移动子弹图片
    moveBullet()
    # 移动敌机图片
    movePlanes()
    # 道具移动图片
    moveItem()
    # 云彩移动图片
    moveCloud()
    # 子弹与敌机碰撞
    crash()
    # 飞机与道具碰撞
    itemCrash()
    # 重新加载背景图片
    screen.blit(bg,bgRect)
    # 重新加载飞机图片
    screen.blit(plane,planeRect)
    # 重新加载敌机图片
    screen.blit(planes, planesRect)
    # 重新加载子弹图片
    screen.blit(bullet, bulletRect)
    # 重新加载道具图片
    screen.blit(item, itemRect)
    # 重新加载云彩图片
    screen.blit(cloud, cloudRect)
    # 更新窗口显示内容
    pygame.display.update()
    # 控制当前循环执行频率
    time.tick(60)

21.jpg 感谢大家的支持,关注,白嫖,评论!

posted on 2022-06-13 14:52  CatGod007  阅读(3)  评论(0)    收藏  举报  来源