pygame自己实现、精灵实现 左右移动与发射子弹

自己实现

import sys
import pygame

pygame.init()

# 初始

# 窗口大小
screen_image = pygame.display.set_mode((800, 600))
screen_rect = screen_image.get_rect()
# 窗口标题
pygame.display.set_caption('游戏窗口标题')

# 元素

# 读取图像  图像不可中文
ship_image = pygame.image.load("2.png")
ship_rect = ship_image.get_rect()
# 图像 中间底部 = 窗口 中间底部
ship_rect.midbottom = screen_rect.midbottom
# ship_rect.bottom = screen_rect.bottom  # 底部默认初始 左边

# 自定义颜色
bg_color1 = (0, 128, 128)
bg_color2 = (60, 60, 60)
bg_color3 = (255, 0, 0)

moving_left = False
moving_right = False

# 子弹
bullets = []

while True:

    # 捕获 键盘鼠标操作 pygame.event.get()
    for event in pygame.event.get():

        # 点击❌号关闭,退出游戏
        if event.type == pygame.QUIT:
            sys.exit()
        # 监控 键盘 按下
        elif event.type == pygame.KEYDOWN:
            # 键盘 左
            if event.key == pygame.K_LEFT:
                moving_left = True

            # 键盘 右
            if event.key == pygame.K_RIGHT:
                moving_right = True

            # 键盘 空格 K_SPACE
            if event.key == pygame.K_SPACE:
                # 让整个屏幕只存在 2个子弹 才会生成新的
                if len(bullets) < 2:
                    # 生成子弹
                    new_bullet_rect = pygame.Rect(0, 0, 4, 15)
                    # 绑定位置  子弹中间底部 = 图片顶部
                    new_bullet_rect.midbottom = ship_rect.midtop
                    bullets.append(new_bullet_rect)

            # # 键盘 上
            # if event.key == pygame.K_UP:
            #     ship_rect.y -= 10
            # # 键盘 下
            # if event.key == pygame.K_DOWN:
            #     ship_rect.y += 10
        # 监控 键盘 松开
        elif event.type == pygame.KEYUP:
            # 键盘 左
            if event.key == pygame.K_LEFT:
                moving_left = False

            # 键盘 右
            if event.key == pygame.K_RIGHT:
                moving_right = False
	# 防止出屏幕
    if moving_left and ship_rect.left > 0:
        ship_rect.x -= 1
    if moving_right and ship_rect.right < screen_rect.right:
        ship_rect.x += 1
    # 绘制

    # 绘制游戏窗口背景(颜色三通道)
    screen_image.fill(bg_color1)

    # 在游戏窗口 绘制 加载的图像
    # 窗口 绘制图像(图, 图的坐标)
    screen_image.blit(ship_image, ship_rect)

    # 绘制 列表中的子弹
    for bullet_rect in bullets:
        # 在窗口 绘制(颜色, 1个子弹)
        pygame.draw.rect(screen_image, bg_color2, bullet_rect)
        # 子弹移动
        bullet_rect.y -= 1
        # 删除游戏窗口外的子弹
        #
        if bullet_rect.bottom < 0:
            bullets.remove(bullet_rect)

    # 刷新屏幕
    pygame.display.flip()

使用精灵

import sys
import pygame

pygame.init()

# 初始

# 窗口大小
screen_image = pygame.display.set_mode((800, 600))
screen_rect = screen_image.get_rect()
# 窗口标题
pygame.display.set_caption('游戏窗口标题')

# 元素

# 读取图像  图像不可中文
ship_image = pygame.image.load("2.png")
ship_rect = ship_image.get_rect()
# 图像 中间底部 = 窗口 中间底部
ship_rect.midbottom = screen_rect.midbottom
# ship_rect.bottom = screen_rect.bottom  # 底部默认初始 左边

# 自定义颜色
bg_color1 = (0, 128, 128)
bg_color2 = (60, 60, 60)
bg_color3 = (255, 0, 0)

moving_left = False
moving_right = False

# 子弹
bullets = pygame.sprite.Group()  # 生成 精灵盒
# bullets = []

while True:

    # 捕获 键盘鼠标操作 pygame.event.get()
    for event in pygame.event.get():

        # 点击❌号关闭,退出游戏
        if event.type == pygame.QUIT:
            sys.exit()
        # 监控 键盘 按下
        elif event.type == pygame.KEYDOWN:
            # 键盘 左
            if event.key == pygame.K_LEFT:
                moving_left = True

            # 键盘 右
            if event.key == pygame.K_RIGHT:
                moving_right = True

            # 键盘 空格 K_SPACE
            if event.key == pygame.K_SPACE:
                # 让整个屏幕只存在 2个子弹 才会生成新的
                if len(bullets) < 2:
                    # 生成精灵
                    new_bullet = pygame.sprite.Sprite()
                    # 生成精灵形状
                    new_bullet.rect = pygame.Rect(0, 0, 4, 15)
                    # 绑定位置  精灵中间底部 = 图片顶部
                    new_bullet.rect.midbottom = ship_rect.midtop
                    # 精灵放入 盒中
                    bullets.add(new_bullet)

            # # 键盘 空格 K_SPACE
            # if event.key == pygame.K_SPACE:
            #     # 让整个屏幕只存在 2个子弹 才会生成新的
            #     if len(bullets) < 2:
            #         # 生成子弹
            #         new_bullet_rect = pygame.Rect(0, 0, 4, 15)
            #         # 绑定位置  子弹中间底部 = 图片顶部
            #         new_bullet_rect.midbottom = ship_rect.midtop
            #         bullets.append(new_bullet_rect)

        # 监控 键盘 松开
        elif event.type == pygame.KEYUP:
            # 键盘 左
            if event.key == pygame.K_LEFT:
                moving_left = False

            # 键盘 右
            if event.key == pygame.K_RIGHT:
                moving_right = False

    # 防止出屏幕
    if moving_left and ship_rect.left > 0:
        ship_rect.x -= 1
    if moving_right and ship_rect.right < screen_rect.right:
        ship_rect.x += 1
    # 绘制

    # 绘制游戏窗口背景(颜色三通道)
    screen_image.fill(bg_color1)

    # 在游戏窗口 绘制 加载的图像
    # 窗口 绘制图像(图, 图的坐标)
    screen_image.blit(ship_image, ship_rect)

    # 绘制
    for bullet in bullets:
        # 在窗口 绘制(颜色, 1个子弹)
        pygame.draw.rect(screen_image, bg_color2, bullet.rect)
        # 子弹移动
        bullet.rect.y -= 1
        # 删除游戏窗口外的子弹
        if bullet.rect.bottom < 0:
            bullets.remove(bullet)

    # # 绘制 列表中的子弹
    # for bullet_rect in bullets:
    #     # 在窗口 绘制(颜色, 1个子弹)
    #     pygame.draw.rect(screen_image, bg_color2, bullet_rect)
    #     # 子弹移动
    #     bullet_rect.y -= 1
    #     # 删除游戏窗口外的子弹
    #     #
    #     if bullet_rect.bottom < 0:
    #         bullets.remove(bullet_rect)

    # 刷新屏幕
    pygame.display.flip()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

默执_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值