Event has not attribute key?

Event has not attribute key?

Postby spamynator_1 » Fri Sep 20, 2013 7:08 pm

I ran into an issue i cant seem to fix, just making a simple picture move on command.
Code: Select all
def event_loop(self):
        for event in pg.event.get():
            self.keys = pg.key.get_pressed()
            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
                self.done = True

            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_RETURN and self.state == "START":
                    self.state = "MENU"

            elif self.state == "MENU":
                if event.type == pg.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        if BUTTON_NEW_rect.collidepoint(event.pos):
                            self.state = "NEW"
                   
            elif self.state == "NEW":
                #JASJKDFHJKSDJSDHJKSDHGJSDGHSDJFGHSDGJKSHGKJSHGJKSDJSDGGHSDJGHSDGJKSDGHSDJGHSDJ
                #HOLY FUCK NEED MAP1 AND CHARACTER AHHHHHHHHHHHHHHHHHHHHH
                location = "0,0"
                mapid = "0"
                inv = "0,0"
                self.state = "PLAY1"

            elif self.state == "PLAY":
                pg.key.set_repeat(1,50)
                       
                if event.key == pg.K_w:
                    self.charcoordy -= 3
                   
                elif event.key == pg.K_s:
                    self.charcoordy += 3
                   
                elif event.key == pg.K_a:
                    self.charcoordx -= 3
                       
                elif event.key == pg.K_d:
                    self.charcoordx += 3


This returns Event has not attribute 'key'. HOw do i fix this? Thank you
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: Event has not attribute key?

Postby hansn » Sat Sep 21, 2013 6:08 pm

Always post a full traceback when you need help with an error.

I don't know why, but I think the event.key statements have to be inside of an event.type == keydown statement.
Just like you did in the top of your event loop.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Event has not attribute key?

Postby dboxall123 » Sun Sep 22, 2013 12:33 am

I think hansn is right about needing to use pg.KEYDOWM. Howeve, you could not bother with this and just use
Code: Select all
if self.keys[pg.K_w]:

etc. That might make things a little easier :)
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Event has not attribute key?

Postby DrakeMagi » Sun Sep 22, 2013 1:49 pm

pick a method. 3 different method in code.

code should be in loop. not event loop
Code: Select all
self.keys = pg.key.get_pressed()


only needs to be set once. not every loop or event loop.
only works pygame.KEYDOWN event.
not recommend.
Code: Select all
pg.key.set_repeat(1,50)


using get get_pressed
Code: Select all
# loop
    for event  in pygame.event.get(): # event loop
        if event.type == pygame.QUIT:
            done = True
   
    self.key = pygame.key.get_pressed()
    if self.state == "PLAY!":
        if self.key[pygame.K_w]:
            self.charcoordy -= 3


if you want it time control. use pygame.time.get_ticks.
DrakeMagi
 
Posts: 82
Joined: Sun May 12, 2013 8:36 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests