2015-01-28 5 views
1

마우스 클릭으로 새로운 스프라이트를 만드는 방법을 모르며 모든 새로운 스프라이트에 움직임을 추가한다.스프라이트가 아래로 움직이지 않는다

  • 다이아몬드 : 당신은 마우스 당신을 클릭하면/마우스 오른쪽

으로 왼쪽으로 이동 : K_RIGHT

  • 패드,/우 K_LEFT으로 왼쪽으로 이동

    나는 간단한 게임을 만들었습니다 마우스 클릭 수에 따라 하트를 추가하고 스프라이트 heart0, heart1 등을 그립니다.

    당신은 다이아몬드이고 마음이 아래로 이동해야하는 위치 장소에 떨어지는 스프라이트 마음을 그리는 마우스 ...

    문제를 클릭하면 : 마음이 아래로 이동되지 않습니다. 나는 여러 개의 마음을 그릴 수 없다. 현재 나는 다이아 몬이 있고 마음을 덧붙인 곳에서만 심장을 그릴 수 있습니다.

    내 코드가 잘못되어 필요한 기능을 얻을 수 없습니까?

    for myfigure in hearts: 
        myfigure = HeartSprite(diamondx , diamondy) 
        myfigure.update(time, 480) 
        myfigure.draw(screen) 
    

    가 잠시 중지하고이 루프가 정말 무엇을 생각 :

    import pygame, sys 
    from pygame.locals import * 
    
    pygame.init() 
    pygame.mouse.set_visible(1) 
    clock = pygame.time.Clock() 
    
    screen_width = 640 
    screen_height = 480 
    screen = pygame.display.set_mode((screen_width, screen_height)) 
    
    myfont = pygame.font.SysFont("Arial", 15) 
    
    hearts = [] 
    clicks = [] 
    
    class HeartSprite(object): 
    
        def __init__(self, pozx, pozy): 
    
         self.image = pygame.image.load("heart.png") 
         self.posx = pozx 
         self.posy = pozy 
         self.rect = self.image.get_rect() 
         self.going_down = True 
         self.next_update_time = 0 
    
        def update(self, current_time, bottom): 
         if self.next_update_time < current_time: 
          ### bounce back 
          #if self.rect.bottom >= bottom - 1: 
          # self.going_down = False 
          ### kill it 
          if self.posy > screen_height: 
           del hearts[0] 
          elif self.posy <= 85: 
           self.going_down = True  
          if self.going_down: 
           self.posy += 2 
          else: 
           self.posy -= 2 
          self.next_update_time = current_time + 10 
    
        def draw(self, surface): 
         surface.blit(self.image, (self.posx, self.posy)) 
    
    class Diamond(object): 
        def __init__(self): 
         self.image = pygame.image.load("diamond.png").convert() 
         self.rect = self.image.get_rect() 
    
        def handle_keys(self): 
         key = pygame.key.get_pressed() 
         dist = 3 
         if key[pygame.K_LEFT]: 
          self.rect[0] -= dist 
         elif key[pygame.K_RIGHT]: 
          self.rect[0] += dist 
    
        def draw(self, surface): 
         surface.blit(self.image, (self.rect[0], self.rect[1]+18)) 
    
    class Pad(object): 
        def __init__(self): 
         self.image = pygame.image.load("pad.png") 
         self.rect = self.image.get_rect() 
         self.top = screen.get_height() - self.rect[1] 
         self.shoot_y = 0 
         self.shoot_x = 0 
        def draw(self, surface, posx, posy): 
         surface.blit(self.image, (posx, posy)) 
    
    class DisplayText(object): 
        def __init__(self): 
         self.myfont = pygame.font.SysFont("Tahoma", 15) 
         self.heartsleft = 10 - len(hearts) 
         self.label = self.myfont.render("hearts left : %d" %  self.heartsleft, 1, (0,0,0)) 
    
        def draw(self, surface): 
         surface.blit(self.label, (3, 1)) 
    
    def main(): 
    
        diamond = Diamond() 
        pad = Pad() 
    
        while True: 
    
         white = (255, 255, 255) 
         screen.fill(white) 
         time = pygame.time.get_ticks() 
    
         x, y = pygame.mouse.get_pos() 
         pad.draw(screen, x - pad.rect[2]/2, screen_height - pad.rect[2]/2) 
    
         for event in pygame.event.get(): 
          if event.type == pygame.QUIT: 
           pygame.quit() 
           sys.exit() 
          elif event.type == MOUSEBUTTONDOWN: 
           diamondy = diamond.rect[1]+18 
           diamondx = diamond.rect[0] 
           count = "clicks" 
           clicks.append(count) 
           myvar = len(clicks) 
           for i in range(myvar): 
            ii = "heart" + str(i) 
           hearts.append(ii) 
    
         for myfigure in hearts: 
          myfigure = HeartSprite(diamondx , diamondy) 
          myfigure.update(time, 480) 
          myfigure.draw(screen) 
    
         diamond.draw(screen) 
         diamond.handle_keys() 
    
         displaytext = DisplayText() 
         displaytext.draw(screen) 
    
         pygame.display.update()    
         clock.tick(100) 
        pygame.quit() 
    main() 
    
  • 답변

    1

    문제는이 루프입니다. 하트의 각 요소에 대해 diamondxdiamondyHeartSprite의 새 인스턴스를 만듭니다. 루프가 실행될 때마다 화면을 클릭하고 목록에서 해당 인스턴스를 유지하면 당신이 정말로 원하는 무엇

    는 (각각의 마음의 상태를 유지)를 HeartSprite 인스턴스를 생성하는 것입니다 :이

    ... 
         elif event.type == MOUSEBUTTONDOWN: 
          diamondy = diamond.rect[1]+18 
          diamondx = diamond.rect[0] 
          hearts.append(diamondx, diamondy) 
    ... 
        for myfigure in hearts: 
         myfigure.update(time, 480) 
         myfigure.draw(screen) 
    ... 
    

    이 코드에 대해 더 많은 문제가 있지만이 답변의 범위를 벗어납니다.

    +0

    감사합니다. 당신의 대답은 제가 앞으로 나아갈 수 있도록 도와주었습니다. 코드를 배치하고 도움을 요청하기 전에 문제에 대해 더 많이 생각할 것입니다. – BlueTomato

    관련 문제