python笔记(三十六) pygame(3)边界,大小调整

一.全屏

import pygame
import sys

# 初始化pygame
pygame.init()

size = width ,height = 900, 500  #设置窗口大小,也是活动范围
speed = [-2,1]
bg = (255,255,255)

fullscreen = False   #全屏判断

clock = pygame.time.Clock()

# 创建指定大小的窗口
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption("闲得发慌")

# 加载图片
people = pygame.image.load("p.gif")
# 获得图像位置矩形
position = people.get_rect()

# 图片方向
l_head = people
r_head = pygame.transform.flip(people,True,False)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.KEYDOWN:           
            if event.key == pygame.K_a:
                people = l_head
                speed = [-2,0]
            if event.key == pygame.K_d:
                people = r_head
                speed = [2,0]
            if event.key == pygame.K_w:
                speed = [0,-2]
            if event.key == pygame.K_s:
                speed = [0,2]

            # 全屏操作(F11)
            if event.key == pygame.K_F11:
                fullscreen = not fullscreen   
                if fullscreen:
                    size = width, height = 1600, 900
                    # 全屏是 FULLSCREEN ,同时硬件加速 HWSURFACE
                    screen = pygame.display.set_mode(size,pygame.FULLSCREEN | pygame.HWSURFACE)                   
                else:
                    size = width, height = 900, 500
                    screen = pygame.display.set_mode(size)
                    position = people.get_rect()  #解决退出全屏时图片反复横跳的bug
            

    # 移动图像
    position = position.move(speed)

    if position.left < 0 or position.right > width:
        # 反转图像
        people = pygame.transform.flip(people,True,False)
        # 反向移动
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]

    # 填充背景
    screen.fill(bg)
    # 更新图像位置
    screen.blit(people,position)
    # 更新界面
    pygame.display.flip()
    # 延迟10毫秒
    #pygame.time.delay(5)

    # 控制帧率
    clock.tick(200)

如果出现边界bug,换个分辨率就可以了
关于set_mode()的第二个参数
在这里插入图片描述
OPENGL是3D游戏的渲染
resizable可以调整游戏边框,但是不推荐
在这里插入图片描述

二.调整大小

import pygame
import sys

# 初始化pygame
pygame.init()

size = width ,height = 900, 500
speed = [-2,1]
bg = (255,255,255)

fullscreen = False

clock = pygame.time.Clock()

# 创建指定大小的窗口
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption("闲得发慌")

# 设置放大缩小比率
rat = 1.0

# 加载图片
o_people = pygame.image.load("p.png")
people = o_people

# 获得图像位置矩形
o_people_rect = o_people.get_rect()
position = people_rect = o_people_rect

# 图片方向
l_head = people
r_head = pygame.transform.flip(people,True,False)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.KEYDOWN:           
            if event.key == pygame.K_a:
                people = l_head
                speed = [-2,0]
            if event.key == pygame.K_d:
                people = r_head
                speed = [2,0]
            if event.key == pygame.K_w:
                speed = [0,-2]
            if event.key == pygame.K_s:
                speed = [0,2]

            # 全屏操作(F11)
            if event.key == pygame.K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    size = width, height = 1600, 900
                    screen = pygame.display.set_mode(size,pygame.FULLSCREEN | pygame.HWSURFACE)                   
                else:
                    size = width, height = 900, 500
                    screen = pygame.display.set_mode(size)
                    position = people.get_rect()

            # 放大 缩小 空格恢复(+ -)
            if event.key == pygame.K_EQUALS or event.key == pygame.K_MINUS or event.key == pygame.K_SPACE:
                 # 最大放大一倍 最小缩小一半
                if event.key == pygame.K_EQUALS and rat < 2:
                    rat += 0.1
                if event.key == pygame.K_MINUS and rat > 0.5:
                    rat -= 0.1
                if event.key == pygame.K_SPACE:
                    rat = 1.0

                people = pygame.transform.smoothscale(o_people,(int(o_people_rect.width * rat),int(o_people_rect.height * rat)))
                    
            

    # 移动图像
    position = position.move(speed)

    if position.left < 0 or position.right > width:
        # 反转图像
        people = pygame.transform.flip(people,True,False)
        # 反向移动
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]

    # 填充背景
    screen.fill(bg)
    # 更新图像位置
    screen.blit(people,position)
    # 更新界面
    pygame.display.flip()
    # 延迟10毫秒
    #pygame.time.delay(5)

    # 控制帧率
    clock.tick(200)

由于每一次放大缩小会丢失一点精确值
所以我们搞一个原版o_people,再搞一个改版people

图形位置也是一样

# 获得图像位置矩形
o_people_rect = o_people.get_rect()
position = people_rect = o_people_rect

最后使用放大缩小函数people = pygame.transform.smoothscale(o_people,(int(o_people_rect.width * rat),int(o_people_rect.height * rat)))
第二个参数是一个元组,边框像素是整数,用int,这里每次变换都用原始版o_people,将变动后的复制给新版people

注:出现24bit,32bit多半是图片的问题

三.旋转图片

import pygame
import sys

# 初始化pygame
pygame.init()

size = width ,height = 900, 500
speed = (2,0)
bg = (255,255,255)

fullscreen = False

clock = pygame.time.Clock()

# 创建指定大小的窗口
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption("闲得发慌")

# 加载图片
people = pygame.image.load("p.gif")
# 获得图像位置矩形
position = people.get_rect()

# 图片旋转 逆时针旋转
people_r = pygame.transform.rotate(people,90)
people_t = pygame.transform.rotate(people,180)
people_l = pygame.transform.rotate(people,270)
people_b = people
people = people_t

# 图片方向
l_head = people
r_head = pygame.transform.flip(people,True,False)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == pygame.KEYDOWN:           
            # 全屏操作(F11)
            if event.key == pygame.K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    size = width, height = 1600, 900
                    screen = pygame.display.set_mode(size,pygame.FULLSCREEN | pygame.HWSURFACE)
                    position = people.get_rect()
                else:
                    size = width, height = 900, 500
                    screen = pygame.display.set_mode(size)
                    position = people.get_rect()
            

    # 移动图像
    position = position.move(speed)

    if position.right > width:
        people = people_r
        position = people_rect = people.get_rect()
        print(position.left,position.top)
        position.left = width - people_rect.width
        speed = [0,2]

    if position.bottom > height:
        people = people_b
        position = people_rect = people.get_rect()
        position.left = width - people_rect.width
        position.top = height - people_rect.height
        speed = [-2,0]

    if position.left < 0:
        people = people_l
        position = people_rect = people.get_rect()
        position.top = height - people_rect.height
        speed = [0,-2]

    if position.top < 0:
        people = people_t
        position = people_rect = people.get_rect()
        speed = [2,0]

    # 填充背景
    screen.fill(bg)
    # 更新图像位置
    screen.blit(people,position)
    # 更新界面
    pygame.display.flip()
    # 延迟10毫秒
    #pygame.time.delay(5)

    # 控制帧率
    clock.tick(200)

旋转图像

# 图片旋转 逆时针旋转
people_r = pygame.transform.rotate(people,90)
people_t = pygame.transform.rotate(people,180)
people_l = pygame.transform.rotate(people,270)
people_b = people
people = people_t

移动图像

 if position.right > width:
        people = people_r
        position = people_rect = people.get_rect()
        position.left = width - people_rect.width
        speed = [0,2]

这里重新获取位置复制给position 和 people_rect,这里是个浅复制
由于positionpeople_rect只有四个数据成员topleftwidthheight
获取位置后topletf都是0

所以在从上面变成左边的时候,top没变,一直是0
只有left变化了,这个left是相对于左上角的,所以position.left = width - people_rect.width
也就是活动宽度 减去 图片宽度


上面那个浅复制当然也可以不要,直接所有属性都用position的 position.left = width - position.width
原来的代码是增强可读性
在这里插入图片描述
在这里插入图片描述

图片变换函数

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值