简体   繁体   中英

python pygame different ways to blit sprite.Group() on the sceen

I want to create an ship and setting 3 small ship symbol for lives so i set the different size of the image as self.ship=pygame.transform.smoothscale(temp,(200,200)) and self.image=pygame.transform.smoothscale(temp,(70,70)) The big size (200,200) for moving ship and the small size (70,70) for left_ship as symbol

I use two way to do this:

  1. self.ships.draw(self.setting.screen)

    this way need set the ship as image and the position as rect

  2. for left_ship in self.ships: self.setting.screen.blit(left.ship.image,left.ship.rect)

But finally when i create the whole code and run it there's no left_ship symbol on the screen,what cause it and how can i fix it?? here's the codes:

#!/usr/bin/python
import sys
import pygame 

class Setting():
    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")                  
        self.ship_left = 3


class Lifes():
    '''create a Group for left_ships '''
    def __init__(self,setting):
        self.setting=setting
        self.left_ships()

    def left_ships(self):
        self.ships=pygame.sprite.Group()
        '''set the different position and add into the Group'''
        for ship_number in range(self.setting.ship_left):
            ship=Ship(self.setting)
            ship.rect.x= 100 + ship_number*ship.rect.width 
            ship.rect.y= self.setting.screen.get_rect().height  
            self.ships.add(ship)

    def blit(self):
        '''test two way to blit the left_ship on the screen but all failed'''
        self.ships.draw(self.setting.screen)
        for left_ship in self.ships:
            self.setting.screen.blit(left_ship.image,left_ship.rect)


class Ship(pygame.sprite.Sprite):
    def __init__(self,setting):
        super().__init__()
        '''set two size of ship and set the big ship postion is on the middle bottom of the screen'''
        self.setting=setting
        temp=pygame.image.load("/home/finals/python/alien/image/title.jpg").convert_alpha()
        self.ship=pygame.transform.smoothscale(temp,(200,200))
        self.image=pygame.transform.smoothscale(temp,(70,70))
        self.screen_rect=self.setting.screen.get_rect()
        self.rect=self.ship.get_rect()

        self.rect.centerx=self.screen_rect.centerx
        self.rect.bottom=self.screen_rect.bottom

    def blit(self):
        self.setting.screen.fill((255,255,255))
        self.setting.screen.blit(self.ship,self.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):
        for event in pygame.event.get():
           if event.type == pygame.QUIT:
               sys.exit()


def game():
    pygame.init()
    setting=Setting(1200,800)
    ship=Ship(setting) 
    check_event=Check_event()
    lifes=Lifes(setting)


    while True:
        check_event.event()
        lifes.blit()
        ship.blit()
        pygame.display.flip()
game()

There are 2 issues. The screen is cleared in Ship.blit , hence everything what is drawn before ship.blit() is not visible. Clear the screen in the main application loop before drawing the ships:

class Ship(pygame.sprite.Sprite):
    # [...]
    
    def blit(self):
        self.setting.screen.blit(self.ship,self.rect)
def game():
    # [...]

    while True:
        check_event.event()
        setting.screen.fill((255,255,255))
        ship.blit()
        lifes.blit()
        pygame.display.flip()

Furthermore the small ships are out of the window at the bottom. That is caused by ship.rect.y = self.setting.screen.get_rect().height . Change the position of the ships. eg:

class Lifes():
    # [...]

    def left_ships(self):
        self.ships=pygame.sprite.Group()
        '''set the different position and add into the Group'''
        for ship_number in range(self.setting.ship_left):
            ship=Ship(self.setting)
            ship.rect = ship.image.get_rect()
            ship.rect.x = 100 + ship_number*ship.rect.width*2 
            ship.rect.bottom = self.setting.screen.get_rect().height
            self.ships.add(ship)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM