Python - Inventory system

Asked

Viewed 135 times

0

I’m making a mini example game to learn more about python, and I don’t know how to remove the self.canvas.create_rectangle(50, 200, 290, 80, tag='inventario', fill='yellow') (INVENTORY ACTIVATED), I will give an example... When I press the i key it creates a Rectangle that would be an inventory, but to open it is easy, I would like to know how to hide, or 'close', or inventory when I press i Again

This is my code

from pygame import mixer
mixer.init()
mixer.music.load('Sources\music.mp3')
mixer.music.play()
class janela:
    def __init__(self, janela):
        self.canvas = Canvas(janela,
                            takefocus=1, bg='black')
        janela.config(bg='red')
        janela.title('Mini jogo de bosta')

        self.canvas.bind('a', self.esquerda)
        self.canvas.bind('A', self.esquerda)
        self.canvas.bind('<Left>', self.esquerda)

        self.canvas.bind('d', self.direita)
        self.canvas.bind('D', self.direita)
        self.canvas.bind('<Right>', self.direita)

        self.canvas.bind('w', self.cima)
        self.canvas.bind('W', self.cima)
        self.canvas.bind('<Up>', self.cima)

        self.canvas.bind('s', self.baixo)
        self.canvas.bind('S', self.baixo)
        self.canvas.bind('<Down>', self.baixo)

        #Bind Inventory

        self.canvas.bind('I', self.inventario)
        self.canvas.bind('i', self.inventario)

        self.canvas.focus_force()
        self.canvas.pack()

        #Person
        #X1, Y1, X2, Y2
        self.canvas.create_oval(90, 90, 110, 110,
                                tag='Player', fill='Green')
        self.canvas.create_oval(95, 104, 105, 124, 
                                tag='Player', fill='Green')
        self.canvas.create_oval(100, 119, 105, 129,
                                tag='Player', fill='Green')
        self.canvas.create_oval(93, 119, 98, 129,
                                tag='Player', fill='Green')
                                
        self.canvas.create_oval(102, 100, 107, 95, 
                                tag='Player', fill='black')
        self.canvas.create_oval(93, 100, 98, 95,
                                tag='Player', fill='black')
            
    def esquerda(self, event): self.canvas.move('Player', -10, 0)
    def direita(self, event): self.canvas.move('Player', 10, 0)
    def cima(self, event): self.canvas.move('Player', 0, -10)
    def baixo(self, event): self.canvas.move('Player', 0, 10)

    #ação inventario

    def inventario(self, event):
        self.canvas.create_rectangle(50, 200, 290, 80,
                                    tag='inventario', fill='yellow')
        print('inventario aberto')
        
        
jan1 = Tk()
janela(jan1)
jan1.mainloop()

1 answer

0

I do not know how to solve the proposed way however pygame has indicated control:

for events in pygame.event.get():# sair
            if events.type == pygame.QUIT:
                print("faça algo")
            if events.type == pygame.KEYDOWN:# move
                if events.key == pygame.K_w:
                    print("faça algo")
                if events.key == pygame.K_s:#move
                    print("faça algo")
            if events.type == pygame.KEYUP:#move
                if events.key == pygame.K_w:
                    print("faça algo")
                if events.key == pygame.K_s:#move
                    print("faça algo")
  • I’ll try to be more specific in the problem When I tighten i creates a yellow rectangle, how can I make the rectangle disappear when I press i again?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.