鱼C论坛

 找回密码
 立即注册
查看: 4428|回复: 7

surface模块中,get_rect()的一些疑惑,求赐教。。。

[复制链接]
发表于 2017-12-18 14:37:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import pygame
import sys
from pygame.locals import *

# 初始化Pygame
pygame.init()

size = width, height = 600, 400
bg = (255, 255, 255) # RGB

fullscreen = False

# 创建指定大小的窗口 Surface
screen = pygame.display.set_mode(size)
# 设置窗口标题
pygame.display.set_caption("初次见面,请大家多多关照!")

# 加在图片
turtle = pygame.image.load("turtle.png")
# 获得图像的位置矩形
position = turtle.get_rect()

speed = [5, 0]
turtle_right = pygame.transform.rotate(turtle, 90)
turtle_top = pygame.transform.rotate(turtle, 180)
turtle_left = pygame.transform.rotate(turtle, 270)
turtle_bottom = turtle
turtle = turtle_top

l_head = turtle
r_head = pygame.transform.flip(turtle, True, False)

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

        if event.type == KEYDOWN:
            # 全屏(F11)
            if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode((1024, 768), FULLSCREEN | HWSURFACE)
                else:
                    screen = pygame.display.set_mode(size)


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

    if position.right > width:
        turtle = turtle_right
        position = turtle_rect = turtle.get_rect()
        position.left = width - turtle_rect.width
        speed = [0, 5]

    if position.bottom > height:
        turtle = turtle_bottom
        position = turtle_rect = turtle.get_rect()
        position.left = width - turtle_rect.width
        position.top = height - turtle_rect.height
        speed = [-5, 0]

    if position.left < 0:
        turtle = turtle_left
        position = turtle_rect = turtle.get_rect()
        position.top = height - turtle_rect.height
        speed = [0, -5]

    if position.top < 0:
        turtle = turtle_top
        position = turtle_rect = turtle.get_rect()
        speed = [5, 0]

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



有点疑惑,经过这个turtle对象的get_rect()方法后,turtle_rect.width说的是当前对象的宽度,还是宽度坐标?求指点,求指点,求指点。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-18 14:54:18 | 显示全部楼层
rect的宽度(对象宽度)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-18 14:55:29 | 显示全部楼层
当然是这个图片 turtle.png 的宽度
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-18 19:06:31 | 显示全部楼层
shigure_takimi 发表于 2017-12-18 14:54
rect的宽度(对象宽度)

请问您一下,那在这里声明位置发生变化,意义何在呢?只要满足if语句条件,不就贴边走了嘛,我可能过于纠结了,麻烦您。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-18 19:08:46 | 显示全部楼层
BngThea 发表于 2017-12-18 14:55
当然是这个图片 turtle.png 的宽度

请问您一下,那在这里声明位置发生变化,意义何在呢?只要满足if语句条件,不就贴边走了嘛,我可能过于纠结了,麻烦您。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-18 21:24:18 From FishC Mobile | 显示全部楼层
八个核桃罒 发表于 2017-12-18 19:08
请问您一下,那在这里声明位置发生变化,意义何在呢?只要满足if语句条件,不就贴边走了嘛,我可能过于纠 ...

要根据图像大小来初始化相关位置参数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-16 23:10:25 | 显示全部楼层
本帖最后由 badboy991 于 2021-7-16 23:17 编辑

import pygame
import sys
from pygame.locals import *
#初始化pygame
pygame.init()

#全屏的时候有用
size = [600,500]


bg = (255,255,255)

speed = [2,0]
#全屏设置
fullscreen = False
#全屏的分辨率
full_ratio = pygame.display.list_modes()
#创建指定窗口大小
screen = pygame.display.set_mode(size,RESIZABLE)
#设置窗口标题
pygame.display.set_caption("第一个游戏")
#加载图片
turtle = pygame.image.load("wg1.png")

turtle_right = pygame.transform.rotate(turtle,90)
turtle_top = pygame.transform.rotate(turtle,180)
turtle_left = pygame.transform.rotate(turtle,270)
turtle_bottom = turtle
turtle = turtle_top
#获取图形的位置矩形

position = turtle.get_rect()


while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            # 全屏(f3)
            if event.key == K_F3:
                fullscreen = not fullscreen
                if fullscreen:
                    size = full_ratio[0]
                    screen = pygame.display.set_mode(size,FULLSCREEN | HWSURFACE)
                else:
                    size = width,height = 700,500
                    screen = pygame.display.set_mode(size)

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

    if position.right > size[0]:
        turtle = turtle_right
        speed = [0,2]
    if position.bottom > size[1]:
        turtle = turtle_bottom
        speed = [-2,0]
    if position.left < 0:
        turtle = turtle_left
        speed = [0,-2]
    if position.top < 0:
        turtle = turtle_top
        speed = [2,0]
        if position.right > size[0]:
            turtle = turtle_right
            speed = [0,2]
    #填充背景色
    screen.fill("white")
    #更新图形
    screen.blit(turtle,position)
    #更新界面
    pygame.display.flip()
    #延迟10毫秒
    pygame.time.delay(10)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-16 23:59:26 | 显示全部楼层
本帖最后由 阿奇_o 于 2021-7-17 00:07 编辑

你要明白: pygame.image.load()  →返回的是:Surface 类对象   

turtle.get_rect() 调用的是 turtle 该Surface类对象的 get_rect 方法,

.get_rect() 返回的是 Rect类对象(也就是position其实是对象!)
——其默认放置在(0,0),即默认其左上角 位于(0,0),相当于 一个position
—— 但 "position" 只是一个Rect对象 的一个属性(部分代表整体?),Rect对象还有.center, .size, .top 等等属性

参考:
https://pygame.readthedocs.io/en/latest/3_image/image.html
《和小卡特一起学习Python》

ps: 我觉得,这里造成不好理解的,有部分原因是 "没命好变量名的锅"。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 02:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表