0

This is my first "real" graphics based game I will create, but I know I need assistance.

def update(self):
        if games.keyboard.is_pressed(games.K_w):
            self.y -= 1
        if games.keyboard.is_pressed(games.K_a):
            self.x -= 1
        if games.keyboard.is_pressed(games.K_s):
            self.y += 1
        if games.keyboard.is_pressed(games.K_d):
            self.x += 1
        if games.keyboard.is_pressed(games.K_i):
            Inventory()

The above is in my update function of my player class, checking to see if "i" is pressed.

def Inventory():
    global LEVEL, TOTAL_XP, MAX_HEALTH, MAX_MAGICKA, viewing_inv
    viewing_inv = True
    inventory_items = []
    while viewing_inv == True:
        print "yo"
        score = games.Text(value = "Points", size = 25, color = color.green, top = 5,
                      left = games.screen.width/2 + 14)
        games.screen.add(score)
        if games.keyboard.is_pressed(games.K_i):
            games.screen.remove(score)
            viewing_inv = False

the above is a temp inventory function I am using to make sure things are working, which they are not. I added the print statement so i can view what is going on behind the scenes. I see the word "yo" print roughly 2-4 times each time I hit "i".

The Question

How can I successfully get it so that if I press "i", I go into the inventory function w/o having the computer loop through 2+ times before i can remove my finger? This question is already a lot even though I want to be able to press "i" again to exit. Any advice would be appreciated!

The Text was ripped straight out of my pong game I created if anyone was curious (which is why points was shown when viewing inventory)

Community
  • 1
  • 1
  • 3
    You should read [the docs for `pygame.key`](http://www.pygame.org/docs/ref/key.html). You can detect the `KEYDOWN` event instead of `is_pressed`, and `set_repeat` lets you determine how long the key has to be pressed to send another `KEYDOWN` event. – jonrsharpe Apr 28 '14 at 10:20
  • Care a bit more about properly formatting your question, things like "i" instead of "I" are mixed with "i" for "i" as a key on keyboard. With programming you shall make such care a habit, it will speed up your work a lot. – Jan Vlcinsky Apr 28 '14 at 10:23
  • @jonrsharpe i see what you are trying to say, but i am failing to see how to implement it EDITED: didnt see this updated comment, will check right now! –  Apr 28 '14 at 10:31
  • @JanVlcinsky i have made an edit to my question, i realized it may have been confusing as it was –  Apr 28 '14 at 10:31
  • @AnthonyBarcume your edit touched only "i" but ignored the "I" for "I do something". Other have edited it faster than me. You can make the text more readable by adding backticks around code related text, like `i`. – Jan Vlcinsky Apr 28 '14 at 10:36
  • @AnthonyBarcume I recommend you to read PEP 8 http://legacy.python.org/dev/peps/pep-0008/ which explains naming convention in Python. E.g. names with first letter being uppercase are usually used for name of classes, not functions. Following these conventions will make your code more readable. Btw - now I realize, all the formatting comments are somehow bound to "I" or "i". Funny. – Jan Vlcinsky Apr 28 '14 at 10:38
  • @JanVlcinsky i was actually going to make it a class but decided to just quickly make it a function to see if it would work :P, still i will check it out as im always willing to learn something new –  Apr 28 '14 at 10:42

0 Answers0