gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:27:25 26 4
gpt4 key购买 nike

我想创建一艘船并为生命设置 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com