Attribute Error in Pygame?
-
So here's the code: bif="bg.jpg" mif="lady.png" import pygame, sys from pygame.locals import * pygame.init() screen=pygame.display.set_mode((640,42... background=pygame.image.load(bif).conv... mouse_c=pygame.image.load(mif).convert... x,y=0,0 movex, movey=0,0 while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type==KEYDOWN: if event.key==K_LEFT: movex=-1 elif event.key==K_RIGHT: movex=+1 elif event.key==K_UP: movey=-1 elif event.key==K_DOWN: movey=+1 if event.type==KEYUP: if event.key==K_LEFT: movex=0 elif event.key==K_RIGHT: movex=0 elif event.key==K_UP: movey=0 elif event.key==K_DOWN: movey=0 x+=movex y+=movey screen.blit(background,(0,0)) screen.blit(mouse_c,(x,y)) pygame.display.update() And whenever I run, it, it always displays: Traceback (most recent call last): File "C:/Users/*****/Desktop/maria", line 25, in <module> elif event.key==K_RIGHT: AttributeError: 'Event' object has no attribute 'key' I don't know what's wrong. I tried rewriting it but it shows the same error. Any help? I'm really frustrated about it.
-
Answer:
Could it be that your indentation is off, that the elif clause on line 25 is not actually a part of the if block on line 22? As far as I can tell, that would be the only way for the Event object not to have a key attribute at that point. Edit: Yep, that image confirms it. The indentation of your elif clauses is incorrect. It must be deeper than the if that checks the event type, otherwise it will not "belong" to that clause, but be executed if it fails. (Which automatically means that it doesn't have a "key" attribute.)
N at Yahoo! Answers Visit the source
Other answers
Could it be that your indentation is off, that the elif clause on line 25 is not actually a part of the if block on line 22? As far as I can tell, that would be the only way for the Event object not to have a key attribute at that point. Edit: Yep, that image confirms it. The indentation of your elif clauses is incorrect. It must be deeper than the if that checks the event type, otherwise it will not "belong" to that clause, but be executed if it fails. (Which automatically means that it doesn't have a "key" attribute.)
Atli
Related Q & A:
- How to serialize an object to XML with dynamic element's attribute?Best solution by Stack Overflow
- How to solve the “Your search cannot be completed because of a service error” error in Search Center?Best solution by SharePoint
- Which MIME type to compress? and what If I omit the `type` attribute from the HTML?Best solution by Webmasters
- How to get 'name' attribute of item of string-array?Best solution by Stack Overflow
- How do I change the value of an attribute?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.