python pygame использует VIDEORESIZE для обновления нового окна, но элемент в окне не обновляет свою новую позицию - PullRequest
1 голос
/ 04 февраля 2020

Я использую VIDEORESIZE, чтобы обновить окно, и когда я масштабирую окно, фон обновляется до нового размера.

Теперь, когда я помещаю корабль в этот windows, тогда, когда я масштабирую окно или полный размер 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()

1 Ответ

1 голос
/ 04 февраля 2020

Вам необходимо обновить ship.screen_rect, а положение корабля не обновляет масштаб.

Обновите ship.screen_rect и вычислите масштабный коэффициент окна (scale_x, scale_y). Наконец, масштабируйте и положение корабля:

class Check_event():
    # [...]

    def event(self,ship,setting):
        for event in pygame.event.get():
            # [...]

            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))

              current_rect = ship.screen_rect
              ship.screen_rect = ship.bkg.get_rect()

              scale_x = ship.screen_rect.width / current_rect.width
              scale_y = ship.screen_rect.height / current_rect.height

              ship.ship_rect.centerx = round(ship.ship_rect.centerx * scale_x)
              ship.ship_rect.bottom = round(ship.ship_rect.bottom * scale_y)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...