python pygame 在屏幕上以 blit sprite.Group() 的不同方式

标签 python pygame

我想创建一艘船并为生命设置 3 个小船符号,因此我将图像的不同大小设置为 self.ship=pygame.transform.smoothscale(temp,(200,200))self.image=pygame.transform.smoothscale(temp,(70,70)) 大号(200,200)用于移动船舶和小型(70,70)对于 left_ship 作为符号

我使用两种方法来做到这一点:

  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)

关于python pygame 在屏幕上以 blit sprite.Group() 的不同方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60069066/

相关文章:

python - 如何检查 pygame 上的天气或大写锁定是否打开

python - Tkinter:是否可以将输入框嵌入文本段落来完成完形填空任务?

python - 无法找到 TIFF 头文件(python 上的 tifflib 错误 - Ubuntu 20.04)

python - 如何访问类内部的全局对象?

class - 如何通过 while/for 循环创建一个类的多个精确实例(以访问它们的属性)?

python - 使用SimpleCV库findBlob函数时Pygame Segmentation错误

python - 在pygame应用程序中显示文本文件中的单词

python - 如何从输入行中提取两个数字并用制表符分隔它们?

python - 在 Python 中一致地格式化数字

python - 通过另一个函数访问一个函数的返回值