pygame 图像--键盘上下左右

将图像初始在窗口中心位置, 不然就会初始在左上角

# 初始窗口中心
ship_rect.center = screen_rect.center

监控键盘 上下左右

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("./R-C.jpg")
ship_rect = ship_image.get_rect()
# # 初始窗口中心
# ship_rect.center = screen_rect.center

# # 自定义颜色
bg_color1 = (0, 128, 128)


while True:

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

        # 点击❌号关闭,退出游戏
        if event.type == pygame.QUIT:
            sys.exit()
        # 监控 键盘输入   按下.KEYDOWN
        elif event.type == pygame.KEYDOWN:
            # 键盘 左
            if event.key == pygame.K_LEFT:
                ship_rect.x -= 10
            # 键盘 右
            if event.key == pygame.K_RIGHT:
                ship_rect.x += 10
            # 键盘 上
            if event.key == pygame.K_UP:
                ship_rect.y -= 10
            # 键盘 下
            if event.key == pygame.K_DOWN:
                ship_rect.y += 10
    # 绘制

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

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

    # # 窗口 绘制图像(图, 图的坐标)
    # screen_image.blit(txt_image, txt_rect)
    #
    # # 在游戏窗口 绘制 自定义的图像
    # # 画 (根据(窗口), 颜色, 画的对象)
    # pygame.draw.rect(screen_image, bg_color2, 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

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

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:
                ship_rect.x -= 10
            # 键盘 右
            if event.key == pygame.K_RIGHT:
                ship_rect.x += 10
            # 键盘 上
            if event.key == pygame.K_UP:
                ship_rect.y -= 10
            # 键盘 下
            if event.key == pygame.K_DOWN:
                ship_rect.y += 10
    # 绘制

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

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

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_42102546

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

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

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

打赏作者

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

抵扣说明:

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

余额充值