2013-12-18 3 views
0

스프라이트 그룹을 화면에 표시하려고하는데 아래 코드는 빈 화면을 표시합니다. 나는 잠시 동안이 일을 해왔다. 왜이 논리가 올바르지 않은지 알 수 없다. 주요 문제는 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() 

답변

0
@classmethod 
def setImage(self,Image): 
    self.image=pygame.image.load(Image) 

, 당신은하지 self 첫 번째 매개 변수 cls를 이름한다. 또한 매개 변수 이름은 소문자 여야합니다 (Image 대신 image). 여기


@classmethod  
def gameLoop(self): 
    screen = pygame.display.set_mode([640, 400]) 
    while True: 
     ... 

동일합니다. 또한, 귀하의 gameloop이 귀하의 Sprite 클래스의 일부인지 나는 알지 못합니다. 또한, 당신은 클래스 Sprite의 이름을 지정하지 않아야합니다. pygame의 Sprite 클래스와 혼동을 일으킬뿐입니다. 여기


class Sprite(pygame.sprite.Sprite): 
    ... 
    sprite = pygame.sprite.Group() 
    ... 

    @classmethod 
    def sprite(self): 
     ... 

당신은 필드 sprite 및 클래스 수준의 기능 sprite라고합니다. 메서드가 필드를 덮어 쓰게되므로이 방법은 작동하지 않습니다. 따라서 언제든지 sprite 필드에 액세스하려면 sprite 기능에 액세스하십시오.

@classmethod  
def gameLoop(self): 
    ... 
    self.actor.update() 

Sprite.actor


은 종료되지 않습니다. 너는 그런 분야를 결코 창조하지 않았다. 여기


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 

, 당신은 인수가 Image라고 전달합니다. 그러나 그것을 사용하지 않고 비어있는 Surface을 사용합니다.


여기 코드의 실행 버전입니다 : 이것에 대한

import pygame, sys, random 

pygame.init() 
troll = pygame.image.load("image.png") 
black = pygame.color.Color('black') 
SCREEN_WIDTH = 640 
SCREEN_HEIGHT = 400 
spriteCount = 5 


class Sprite(pygame.sprite.Sprite): 
    actors = 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 = image 
     self.rect = self.image.get_rect() 
     self.rect.topleft = pos 

    @classmethod 
    def init(self): 
     for i in range(spriteCount): 
      tmp_x = random.randrange(0, SCREEN_WIDTH) 
      tmp_y = random.randrange(0, SCREEN_HEIGHT) 
      self.actors.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 * self.rect.height 
     if self.rect.x > SCREEN_WIDTH: 
      self.rect.x = -1 * self.rect.width 

    @classmethod  
    def gameLoop(self): 
     screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) 
     while True: 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        sys.exit() 
      screen.fill(black) 

      self.actors.update() 
      self.actors.draw(screen) 

      pygame.display.update() 
      self.clock.tick(20) 

Sprite.init() 
Sprite.gameLoop() 
+0

감사합니다, 내가 클래스 메소드를 사용하려는 해달라고 말하고 내가 대신 '트롤 = 스프라이트() 트롤을 사용했다. init() troll.gameLoop()' init 메소드는 첫 번째 클래스 만 클래스가 오버로드를 수행하지 않습니다. 어떻게이 문제를 해결할 수 있습니까? –

관련 문제