스프라이트 그룹을 화면에 표시하려고하는데 아래 코드는 빈 화면을 표시합니다. 나는 잠시 동안이 일을 해왔다. 왜이 논리가 올바르지 않은지 알 수 없다. 주요 문제는 sprite 메서드에서 임의의 위치에 스프라이트를 추가하려고 할 때 발생합니다. 그런 다음 sprite는 gameLoop 메서드에 그려집니다.그룹에 스프라이트 추가하기
제안 사항? 이 클래스 메소드가 아닌 인스턴스 메소드이기 때문에 여기에
import pygame, sys, random
pygame.init()
troll = pygame.image.load("image.png")
black = (0, 0, 0)
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 400
sprite_width = 5
sprite_height = 5
spriteCount = 5
class Sprite(pygame.sprite.Sprite):
pygame.init()
sprite = pygame.sprite.Group()
clock = pygame.time.Clock()
def __init__(self):
pygame.sprite.Sprite.__init__(self)
def __init__(self, Image, pos):
pygame.sprite.Sprite.__init__(self)
self.image =pygame.Surface([0, 0])
self.rect = self.image.get_rect()
self.rect.topleft = pos
@classmethod
def sprite(self):
for i in range(spriteCount):
tmp_x = random.randrange(0, SCREEN_WIDTH)
tmp_y = random.randrange(0, SCREEN_HEIGHT)
# all you have to do is add new sprites to the sprite group
self.sprite.add(Sprite(troll, [tmp_x, tmp_y]))
def update(self):
self.rect.x += 1
self.rect.y += 2
if self.rect.y > SCREEN_HEIGHT:
self.rect.y = -1 * sprite_height
if self.rect.x > SCREEN_WIDTH:
self.rect.x = -1 * sprite_width
@classmethod
def setImage(self,Image):
self.image=pygame.image.load(Image)
@classmethod
def gameLoop(self):
screen = pygame.display.set_mode([640, 400])
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(black)
# to update or blitting just call the groups update or draw
# notice there is no for loop
# this will automatically call the individual sprites update method
self.actor.update()
self.actor.draw(screen)
pygame.display.update()
self.clock.tick(20)
Sprite.sprite()
Sprite.setImage("image.png")
Sprite.gameLoop()
감사합니다, 내가 클래스 메소드를 사용하려는 해달라고 말하고 내가 대신 '트롤 = 스프라이트() 트롤을 사용했다. init() troll.gameLoop()' init 메소드는 첫 번째 클래스 만 클래스가 오버로드를 수행하지 않습니다. 어떻게이 문제를 해결할 수 있습니까? –