我不能多次blit同一个对象

时间:2014-02-10 17:10:26

标签: python list class pygame

我目前正在使用python中的吉他英雄游戏,我正在尝试多次blit一个音符,但我似乎无法使其工作(音符称为红色)!

#Sprite Class
class Sprite(pygame.sprite.Sprite):
    # What has to be passed when you create a sprite.
    #   image_file: The filename of the image.
    #   lacation: The initial location of where to draw the image.
    def __init__(self, image_file, location):                       
    pygame.sprite.Sprite.__init__(self) # Call Sprite initializer       
    self.image = pygame.image.load(image_file)                         
    self.rect = self.image.get_rect()                                  
    self.rect.left, self.rect.top = location
    self.xSpeed = 15 # The default xSpeed
    self.ySpeed = 15 # The default ySpeed
    self.name = "Not Assigned"
    self.speed = 10
    self.direction = 0

    def setSpeed(self, speed):
        self.speed = speed
        self.calcXYSpeeds()

    def setDirection(self, direction):
        self.direction = direction
        self.calcXYSpeeds()

    def calcXYSpeeds(self):
        angleRadians = math.radians(self.direction) # Convert the direction to radians.
        self.xSpeed= self.speed*math.cos(angleRadians)
        self.ySpeed = self.speed*math.sin(angleRadians)

    def move(self):
        self.rect = self.rect.move(self.xSpeed,self.ySpeed)

    def setDirectionTowards(self, (x,y)):
        self.direction = math.degrees(math.atan2((self.rect.x - x), (self.rect.y - y)))
        self.direction = 270 - self.direction
        self.calcXYSpeeds()

#Object Variables
Red = Sprite("Red.png", (100,25))
note1 = Sprite("note1.jpg", (50,650))
#add sprite to group
notes = pygame.sprite.Group()
notes.add(Red)
# Create an clock to keep track time
clock = pygame.time.Clock()
#Scoring Variables
font=pygame.font.Font(None,50)
score=0
score_text=font.render(str(score),1,(225,225,225))
#other Variables
Red1=0
ySpeed = 10
running = 1
while (running==1):
    # Sets the frame rate to 30 frames/second.
    clock.tick(30)

    # Gets any events like keyboard or mouse.
    event = pygame.event.poll()
    key=pygame.key.get_pressed()
    #object random spawn
    time = pygame.time.get_ticks()
    if (time==30*(random.randint(0,1000))):
        screen.blit(Red.image, Red.rect)
        pygame.display.update()

    #Object Movement
    Red.rect.y = Red.rect.y + ySpeed

    #Keys to complete notes
    if key[pygame.K_a]and Red1==0 and pygame.sprite.collide_rect(Red, note1) == True:
        font=pygame.font.Font(None,50)
        score = score+1
        score_text=font.render(str(score),1,(225,225,225))
        Red1=1
        #where i currently working to fix the problem
        notes.Group.clear(screen, background)
        notes.Group.update()
        notes.draw(screen)

    # Sets the exit flag if the X was clicked on the run window.
    if event.type == pygame.QUIT:
        running = 0

    # Color the whole screen with a solid color.
    screen.fill((0, 255, 255))
    #prints objects
    screen.blit(note1.image, note1.rect)
    screen.blit(Red.image, Red.rect)
    screen.blit(score_text,(500,50))

    # Update the window.
    pygame.display.update()

1 个答案:

答案 0 :(得分:2)

您正在同一周期内多次清洁和更新屏幕。

在每个循环中清洁屏幕都是正确的方法,但是你需要只清理一次,否则你就会删除之前的所有内容。

另外,对于在周期之间持续存在的音符,您需要将它们保存在一个集合中,例如音符,这样就可以在每个周期中对每个音符进行blit。

请记住主循环中的此顺序:

  1. 控制FPS(clock.tick())
  2. 检查事件
  3. 更新状态(即:添加/删除笔记集合等)
  4. 清除屏幕
  5. 绘制当前状态(注释,静态元素等)
  6. 更新显示
  7. 另一个重要的事情是,不要在不同的位置使用相同的Sprite()和相同的Rectangle。如果要在新位置绘制同一图像的另一个副本,请复制该矩形,然后创建一个新的Sprite()对象。