簡體   English   中英

python pygame 在屏幕上對 sprite.Group() 進行 blit 的不同方法

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

我想創建一艘船並為生命設置 3 個小船符號,所以我將圖像的不同大小設置為self.ship=pygame.transform.smoothscale(temp,(200,200))self.image=pygame.transform.smoothscale(temp,(70,70))移動船的大尺寸(200,200)(70,70)的小尺寸(70,70)作為符號

我使用兩種方法來做到這一點:

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

    這種方式需要將船設置為image並將位置設置為rect

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

但最后當我創建整個代碼並運行它時,屏幕上沒有 left_ship 符號,是什么原因造成的,我該如何解決? 這是代碼:

#!/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()

有2個問題。 屏幕在Ship.blit被清除,因此在ship.blit()之前繪制的所有內容都不可見。 在繪制船舶之前清除主應用程序循環中的屏幕:

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

此外,小船在底部的窗外。 這是由ship.rect.y = self.setting.screen.get_rect().height 改變船只的位置。 例如:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM