python pygame使用VIDEORESIZE更新新窗口,但窗口中的项目不更新其新位置
我使用python pygame使用VIDEORESIZE更新新窗口,但窗口中的项目不更新其新位置,python,pygame,Python,Pygame,我使用VIDEORESIZE更新窗口,当我缩放窗口时,背景更新为新的大小 现在,当我将一艘飞船放入此窗口时,当我缩放窗口或全尺寸winodw时,飞船无法触及窗口底部,它无法更新到新的屏幕大小,飞船速度变得非常缓慢 我怎样才能修好它 以下是代码: #!/usr/bin/python import sys import pygame class Setting(): '''set for the screen''' def __init__(self,width,height):
VIDEORESIZE
更新窗口,当我缩放窗口时,背景更新为新的大小
现在,当我将一艘飞船放入此窗口时,当我缩放窗口或全尺寸winodw时,飞船无法触及窗口底部,它无法更新到新的屏幕大小,飞船速度变得非常缓慢
我怎样才能修好它
以下是代码:
#!/usr/bin/python
import sys
import pygame
class Setting():
'''set for the screen'''
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=pygame.RESIZABLE
self.color=(255,255,255)
self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
self_title=pygame.display.set_caption("Muhaha")
class Ship():
'''set for the background and ship'''
def __init__(self,screen,setting):
self.bk=pygame.image.load("/home/finals/python/alien/image/muha.png").convert()
self.bkg=pygame.transform.smoothscale(self.bk,(setting.w,setting.h))
temp=pygame.image.load("/home/finals/python/alien/image/title.jpg").convert_alpha()
self.ship=pygame.transform.smoothscale(temp,(200,200))
self.screen=screen
self.screen_rect=screen.get_rect()
self.ship_rect=self.ship.get_rect()
'''make the ship at the middle bottom of the screen'''
self.ship_rect.centerx=self.screen_rect.centerx
self.ship_rect.bottom=self.screen_rect.bottom
def blit(self):
self.screen.blit(self.bkg,(0,0))
self.screen.blit(self.ship,self.ship_rect)
class Check_event():
def __init__(self):
self.moving_up = False
self.moving_down = False
self.moving_left = False
self.moving_right = False
def event(self,ship,setting):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
'''set for moving ship'''
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.moving_up=True
elif event.key == pygame.K_DOWN:
self.moving_down=True
elif event.key == pygame.K_LEFT:
self.moving_left=True
elif event.key == pygame.K_RIGHT:
self.moving_right=True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.moving_up=False
elif event.key == pygame.K_DOWN:
self.moving_down=False
elif event.key == pygame.K_LEFT:
self.moving_left=False
elif event.key == pygame.K_RIGHT:
self.moving_right=False
'''set for scale the window'''
elif event.type == pygame.VIDEORESIZE:
w,h = event.w,event.h
setting.screen = pygame.display.set_mode((w,h),setting.flag,0)
ship.bkg=pygame.transform.smoothscale(ship.bk,(w,h))
'''set for the ship not move out of the screen'''
if self.moving_up == True and ship.ship_rect.top >= 0:
ship.ship_rect.centery -= 1
if self.moving_down == True and ship.ship_rect.bottom <= setting.h:
ship.ship_rect.centery += 1
if self.moving_left == True and ship.ship_rect.left >= 0:
ship.ship_rect.centerx -= 1
if self.moving_right == True and ship.ship_rect.right <= setting.w:
ship.ship_rect.centerx += 1
def game():
pygame.init()
setting=Setting(1200,800)
screen=setting.screen
ship=Ship(screen,setting)
check_event=Check_event()
while True:
check_event.event(ship,setting)
ship.blit()
pygame.display.flip()
game()
#/usr/bin/python
导入系统
导入pygame
类设置():
''为屏幕设置''
定义初始值(自身、宽度、高度):
self.w=宽度
self.h=高度
self.flag=pygame.RESIZABLE
self.color=(255255)
self.screen=pygame.display.set_模式((self.w,self.h),self.flag)
self\u title=pygame.display.set\u字幕(“Muhaha”)
船舶等级():
''背景和船舶设置''
定义初始化(自身、屏幕、设置):
self.bk=pygame.image.load(“/home/finals/python/alien/image/muha.png”).convert()
self.bkg=pygame.transform.smoothscale(self.bk,(setting.w,setting.h))
temp=pygame.image.load(“/home/finals/python/alien/image/title.jpg”).convert_alpha()
self.ship=pygame.transform.smoothscale(temp,(200200))
self.screen=screen
self.screen\u rect=screen.get\u rect()
self.ship\u rect=self.ship.get\u rect()
“让船在屏幕的中间底部”
self.ship_rect.centerx=self.screen_rect.centerx
self.ship_rect.bottom=self.screen_rect.bottom
def blit(自身):
自屏蔽blit(自屏蔽,(0,0))
self.screen.blit(self.ship,self.ship-rect)
类检查事件():
定义初始化(自):
self.moving_up=False
self.moving_down=False
self.moving_left=False
self.moving_right=False
def事件(自身、装运、设置):
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
“设置为移动船舶”
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_UP:
self.moving_up=True
elif event.key==pygame.K_向下:
self.moving_down=True
elif event.key==pygame.K_左:
self.moving_left=True
elif event.key==pygame.K_RIGHT:
self.moving_right=True
elif event.type==pygame.KEYUP:
如果event.key==pygame.K_UP:
self.moving_up=False
elif event.key==pygame.K_向下:
self.moving_down=False
elif event.key==pygame.K_左:
self.moving_left=False
elif event.key==pygame.K_RIGHT:
self.moving_right=False
''设置为缩放窗口''
elif event.type==pygame.VIDEORESIZE:
w、 h=事件.w,事件.h
setting.screen=pygame.display.set_模式((w,h),setting.flag,0)
ship.bkg=pygame.transform.smoothscale(ship.bk,(w,h))
''设置为船舶不移出屏幕''
如果self.moving_-up==True且ship.ship_-rect.top>=0:
ship.ship\u rect.centery-=1
如果self.moving_down==True且ship.ship_rect.bottom=0:
ship.ship_rect.centerx-=1
如果self.moving\u right==True和ship.ship\u rect.right更新ship.screen\u rect
并计算窗口的比例因子(比例x
,比例y
)。最后通过比例因子缩放船舶的位置:
类检查事件():
# [...]
def事件(自身、装运、设置):
对于pygame.event.get()中的事件:
# [...]
elif event.type==pygame.VIDEORESIZE:
w、 h=事件.w,事件.h
setting.screen=pygame.display.set_模式((w,h),setting.flag,0)
ship.bkg=pygame.transform.smoothscale(ship.bk,(w,h))
当前\u rect=ship.screen\u rect
ship.screen_rect=ship.bkg.get_rect()
比例x=装运屏幕宽度/当前屏幕宽度
比例y=船舶屏幕垂直高度/当前垂直高度
ship.ship\u rect.centerx=圆形(ship.ship\u rect.centerx*比例)
ship.ship_rect.bottom=圆形(ship.ship_rect.bottom*比例y)
位置是正确的,但当我将屏幕放大时,船的速度会变慢一点slowly@M_Sea当然,因为屏幕更大。你必须提高速度。