【问题标题】:pygame multiple removable itempygame 多个可移动项目
【发布时间】:2022-12-10 20:27:35
【问题描述】:

我想在 pygame 中制作可收集和易碎的物品,例如箱子、板条箱、桶、头盔、硬币、钥匙。 然后在与角色互动时;当角色接触到它或弄坏它时,我希望它被移除。 我可以根据需要在屏幕上多次绘制所有项目,但是当我尝试删除它们时,要么全部删除,要么一个都不删除。 这次我尝试了另一种方法并使用了一个类,在 for 循环中我创建了一个类项目并将其添加到列表中,在主循环中我将列表中的项目绘制到屏幕上。如果有任何干扰,我会将其从列表中删除。问题是,当我这样做时,我的 fps 严重下降。 我无法理解如何解决它以及这项工作的逻辑。抱歉,如果标题或问题不是不言自明的,我感谢您的帮助。 我的最后一个代码;

import pygame
from pygame.locals import *

pygame.init()
surface = pygame.display.set_mode((640,256))
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 32)

map="0b0b0\n11111"
gameMap=[(list(row)) for row in map.split("\n")]

position=(0,0)
barrelList=[]
condition=True

class Barrel:
    def __init__(self,coord):
        self.coord=coord
        self.image=pygame.transform.scale(pygame.image.load("barrel.png"),(64,80))
        self.rect=pygame.Rect((coord[0],coord[1],64,80))
        
    def draw(self,surface):
        surface.blit(self.image,self.coord)

while True:
    surface.fill((0,0,0))
    for ev in pygame.event.get():
        if ev.type == QUIT:
            pygame.quit()
        if ev.type == MOUSEBUTTONDOWN:
            position=pygame.mouse.get_pos()
            
    y=0
    for layer in gameMap:
        x=0
        for tile in layer:
            if tile=="1":#tiles ... etc.
                pygame.draw.rect(surface,"cyan",(x*128,y*128,128,128))
            if tile=="b":#chest, crate, barrel, healt, coin, key ... etc.
                if condition:
                    barrelList.append(Barrel((x*128,y*128)))
                
            x+=1
        y+=1
        
    for barrel in barrelList:
        barrel.draw(surface)
        if barrel.rect.collidepoint(position):
            barrelList.remove(barrel)
            condition=False
    
    surface.blit(font.render("fps:{}".format(int(clock.get_fps())), 1, (255, 255, 255)), (0, 0))
    pygame.display.flip()
    clock.tick(60)

barrelImage=

【问题讨论】:

    标签: python pygame


    【解决方案1】:
    1. 在应用程序循环之前创建一次桶列表就足够了。
    2. 在发生MOUSEBUTTONDOWN 事件时移除事件循环中的桶。
    3. 查看如何How to remove items from a list while iterating?。删除项目时遍历列表的浅表副本:
      import pygame
      from pygame.locals import *
      
      pygame.init()
      surface = pygame.display.set_mode((640,256))
      clock = pygame.time.Clock()
      font = pygame.font.SysFont("Arial", 32)
      
      mapDef = "0b0b0
      11111"
      gameMap = [(list(row)) for row in mapDef.split("
      ")]
      barrelList=[]
      
      class Barrel:
          def __init__(self,coord):
              self.coord=coord
              self.image=pygame.transform.scale(pygame.image.load("barrel.png"),(64,80))
              self.rect=pygame.Rect((coord[0],coord[1],64,80))  
          def draw(self,surface):
              surface.blit(self.image,self.coord)
      
      for y, layer in enumerate(gameMap):
          for x, tile in enumerate(layer):
              if tile=="b":#chest, crate, barrel, healt, coin, key ... etc.
                  barrelList.append(Barrel((x*128,y*128)))
      
      run = True
      while run:
          for ev in pygame.event.get():
              if ev.type == QUIT:
                  run = False
              if ev.type == MOUSEBUTTONDOWN:
                  for barrel in barrelList[:]:
                      if barrel.rect.collidepoint(ev.pos):
                          barrelList.remove(barrel)
      
          surface.fill((0,0,0))
          for y, layer in enumerate(gameMap):
              for x, tile in enumerate(layer):
                  if tile=="1":#tiles ... etc.
                      pygame.draw.rect(surface,"cyan",(x*128,y*128,128,128))
          for barrel in barrelList:
              barrel.draw(surface)
          surface.blit(font.render("fps:{}".format(int(clock.get_fps())), 1, (255, 255, 255)), (0, 0))
          pygame.display.flip()
      
          clock.tick(60)
      
      pygame.quit()
      

    【讨论】:

    • 谢谢你的回答,我明白了。那么如何创建示例项目类呢?喜欢类 Barrel:,因为我认为我的项目类太弱了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2014-12-16
    • 1970-01-01
    相关资源
    最近更新 更多