pygame,Rect对象问题
本帖最后由 qin_yin 于 2021-1-1 00:12 编辑请问pygame中的rect对象其中某一个属性发生改变,其他的属性是不是也会发生相应改变:
如top属性 = 10,rect对象会自动根据这个属性,对其他相关的属性作出改变, 提出这个疑惑的原因是,我发现33行到48行的内容中被注释的地方,发现注释和没注释运行起来效果一样,而且属性的值也是一样的
import pygame
import sys
bg = (15, 180, 44)
speed =
size = width, height = 600, 400
set_full_screen = False
# 实例化Clock,后续将用CLock类的tick方法控制帧数
fcolck = pygame.time.Clock()
# 设置窗口模式
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
# 设置窗口标题
pygame.display.set_caption('Test')
# 导入图片
ball = pygame.image.load(r'D:\python库\PYG02.gif')
# 获得图片的矩形信息
ball_rect = ball.get_rect()
resolving_power = pygame.display.list_modes()
mouse_button_up = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEMOTION:
if mouse_button_up:
new_position = ball_rect.move(event.rel)
# 限制图像移动范围
if new_position.top < 0:
new_position.top = 0
#new_position.bottom = new_position.h
elif new_position.bottom > height:
new_position.bottom = height
#new_position.top = height - new_position.h
if new_position.left < 0:
new_position.left = 0
#new_position.right = new_position.w
elif new_position.right > width:
new_position.right = width
# new_position.left = width - new_position.w
print(new_position.left)
ball_rect = new_position
elif event.type == pygame.MOUSEBUTTONUP:
mouse_button_up = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if ball_rect.left < event.pos < ball_rect.right and ball_rect.top < event.pos < ball_rect.bottom:
mouse_button_up = True
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.w, event.h
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
speed = speed + 1 if speed >= 0 else speed - 1
print(speed, '上')
elif event.key == pygame.K_DOWN:
speed = speed if speed == 0 else (abs(speed) - 1) * int((speed / abs(speed)))
print(speed, '下')
elif event.key == pygame.K_LEFT:
speed = speed if speed == 0 else (abs(speed) - 1) * int((speed / abs(speed)))
print(speed, '左')
elif event.key == pygame.K_RIGHT:
speed = speed + 1 if speed > 0 else speed - 1
print(speed, '右')
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_F11:
set_full_screen = not set_full_screen
if set_full_screen:
side = width, height = 1920, 870
pygame.display.set_mode(side, pygame.FULLSCREEN)
else:
side = width, height = 600, 400
pygame.display.set_mode(side)
if ball_rect.left < 0 or ball_rect.right > width:
speed = -speed
if ball_rect.top < 0 or ball_rect.bottom > height:
speed = -speed
# 判断窗口是否被显示界面(屏幕绘制/非图标化,最小化)
# if pygame.display.get_active():
# ball_rect = ball_rect.move(speed)
screen.fill(bg)
screen.blit(ball, ball_rect)
pygame.display.update()
fcolck.tick(30) 其他属性是会变化的 你可以把它理解成一个窗口 ,width和height一般是不变的 所以当你任意改 top bottom, left right中一个另外对应的一个属性也会改变
页:
[1]