Simple backpack with python.wx bug!

Grab your favourite IDE and tinker with the innards of game engines

Simple backpack with python.wx bug!

Postby BubbleQ on Thu Dec 02, 2010 3:21 pm

Hi again :)

So I am on a python class at school each monday, we have just gone through the whole The python tutorial. I have started to experiment with diffrent modules and some with pygames. My ultimate goal is to create a ripoff on Chip challenge^^

Yesterday I started to build an backpack solution for use in pygame, or anything really where you need to store something. I though that the WX moudle would do the grapichal trick, sins it is very easy to make one in just plain text.

Anyway, I came pretty far, though I got a bug. I am not sure if it is pygame or wx causing the bug, but it feels like it could be pygame, don really can explain why but from my little experience, it feel that way :wink:

Here is the code:
Code: Select all
    import wx, pygame
    from pygame.locals import *
    pygame.init()

    if __name__ == '__main__':
        app=wx.PySimpleApp()
       
        size = width, height = 800, 600
        screen = pygame.display.set_mode(size)
       
        backpack = ['Medallion',  'Knife']     # This should be a reguluar list.
       
        while 1:
           
            keyinput = pygame.key.get_pressed()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:sys.exit()
           
            if keyinput[K_UP]:
                a = wx.SingleChoiceDialog(None,  "5 item slots:",  "Backpack",  backpack)
                if a.ShowModal() == wx.ID_OK:
                    print "You select %s" % a.GetStringSelection()
           
            if keyinput[K_DOWN]:
                backpack.insert( 0,'Arrow')
                            #These booth lines seems to do the same thing, only that insert uses a position.
            if keyinput[K_LEFT]:
                backpack.append('Arrow')

The bug is that whenever I press the down key( as is just a random thing for adding stuff), the backpack will be filled with the 'Arrow'. I just want 'Arrow' to be inserted onceeach key press :P
So do any have any idea of how I can solove this?
---------------------------------
I am a happy mapper!
Archlinux
User avatar
BubbleQ
1337 p0st3r
1337 p0st3r
 
Joined: Sat Jun 07, 2008 11:03 pm
Location: Sweden, Steungsund

Re: Simple backpack with python.wx bug!

Postby zombie@computer on Thu Dec 02, 2010 6:03 pm

i have absolutely no knowlegde of python, but you are currently checking almost infinitely often if a number of keys is pressed, causing a great number of hits when one of these buttons is pressed. It seems 'pygame.key.get_pressed()' returns keys currently pressed, not keys that have been pressed and released. So you are either going to have to see if there is another method of getting keypress information (via the events perhaps?), or keep track of the keyinputs you already handled and ignore those.
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Simple backpack with python.wx bug!

Postby BubbleQ on Thu Dec 02, 2010 6:36 pm

Hmm, ye. You are probaly right :) I think that there is some other kind of keypress thing I could use, ye, something with event as you said. Gonna look that up!
Should be this one who needs new desing^^
keyinput = pygame.key.get_pressed()

But when I reed you post it came to me that also maybe, I don´t have to got these keypresses in the while loop? I wonder if they coule be executed outside of if. But then it will only be executed once? Your suloution seems better atm.

I don´t have time to build today but I will deffenetly check it out tomorow, have also posted on some other python forums too^^

Thanks :)
---------------------------------
I am a happy mapper!
Archlinux
User avatar
BubbleQ
1337 p0st3r
1337 p0st3r
 
Joined: Sat Jun 07, 2008 11:03 pm
Location: Sweden, Steungsund

Return to Programming

Who is online

Users browsing this forum: No registered users