更新Pygam中的“接口”时出现问题

2024-09-25 02:40:03 发布

您现在位置:Python中文网/ 问答频道 /正文

注意:我知道我发布的代码很多,但是你不需要全部通读。请跳到我的帖子下面

我是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是否为真。我试着使用一些while语句,但它们冻结了我的游戏,我什么也做不了(我很可能用错了) 有没有人能给我一些建议,让这个工作

我的目标是点击一个箱子,打开一个界面,直到X被点击


Tags: therectimageselfreturnifinitdef