无法在Pygame中更新“接口”

时间:2014-08-16 01:54:58

标签: python pygame

注意:我知道我发布的代码很多,但您并不需要仔细阅读。请跳到我的帖子底部。

我是Pygame的新手,我最近刚刚发现如何制作可点击的精灵。所以我想做的就是制作一个可点击的胸部,当胸部打开时,打开一个有一堆插槽的接口。我已设法打开界面(还没有显示项目),但我无法弄清楚如何关闭界面。我希望它在右上方有一个红色的X,我已经添加了,但是当用户点击X时,没有任何反应。这是我的代码。 (重要部分旁边有评论)

import pygame

size = (700,700)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

class X(pygame.sprite.Sprite): #this class creates an X at the top right of the interface
    def __init__(self,image,x,y):
        super(X,self).__init__()
        self.image = pygame.image.load(image)
        self.x = x
        self.y = y
        self.isclicked = False
    @property
    def rect(self):
        return self.image.get_rect(topleft=(self.x,self.y))
    def clickActivity(self): #if the 'X' is clicked
        self.isclicked = True

class Chest_Screen(pygame.sprite.Sprite): #this is the interface that opens when a chest is clicked
    def __init__(self,image,x,y):
        super(Chest_Screen,self).__init__()
        self.image = pygame.image.load(image)
        self.x = x
        self.y = y
        self.x_slot = 20
        self.y_slot = 20
    def rect(self):
        return self.image.get_rect(topleft=(self.x,self.y))
    def clickActivity(self):
        pass
    @property
    def rect(self):
        return self.image.get_rect(topleft=(self.x,self.y))

class Chest(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        super(Chest,self).__init__()
        self.image = pygame.image.load(image)
        self.x = x
        self.y = y
        self.Open = False
    def isOpen(self):
        if self.Open == True:
            return True
        else:
            return False
    def switchOpen(self):
        if self.isOpen():
            self.image = pygame.image.load('chest.png')
            self.Open = False
        else:
            self.image = pygame.image.load('chest_open.png')
            self.Open = True
    def clickActivity(self):
        self.switchOpen()
        if self.isOpen():
            print('Chest is open')
            interfaceManager(chest_screen) #this is what I'm having trouble with here
        else:
            print('chest is closed')
    @property
    def rect(self):
        return self.image.get_rect(topleft=(self.x, self.y))

def interfaceManager(interface):
    global spriteGroup
    x = X('x.png',interface.rect.right, interface.rect.top - 15)
    oldGroup = spriteGroup #to open the old spritegroup when done
    spriteGroup = pygame.sprite.Group() #start a new spritegroup
    spriteGroup.add(interface)
    spriteGroup.add(x)
    if x.isclicked:
        spriteGroup = oldGroup #load back in the old group

chest_screen = Chest_Screen('chest_screen.png',20,20)
chest = Chest('chest.png',350,350)
spriteGroup = pygame.sprite.Group()
spriteGroup.add(chest)


while 1:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            clicked_sprites = [s for s in spriteGroup if s.rect.collidepoint(pos)]
            for i in clicked_sprites:
                i.clickActivity() #this sees what happens when sprites are clicked
    screen.fill((0,0,0))
    spriteGroup.draw(screen)
    pygame.display.flip()
    clock.tick(60)

所以我非常肯定我知道精灵为什么不会这样做。这是因为当interfaceManager出现时,它只会通过代码一次,所以如果x.isclicked为True,它就无法再次检查。我尝试使用一些while语句,但是他们冻结了我的游戏而且我无法做任何事情。 (我很可能错误地使用它) 是否有任何提示可以让我让这个工作?

我的目标是单击一个胸部,然后打开一个界面,直到单击X.

0 个答案:

没有答案