User avatar
Mahan_Micro
Posts: 44
Joined: Tue Aug 04, 2015 8:52 pm

my problem with pygame EVENT !

Thu Sep 03, 2015 2:33 am

Hi
I'm learning python by Mag pi :)
in mag pi 33 , there is a code for keyboard contrrol (keyboard.py)
when i compile it, program runs correctly but when I push a key of keyboard following error apear and program stop:

if event.key == pygame.K_UP:
'module' object has no attribute 'K_UP' :!: :!: :!:

why????? in first I import :
import pygame, sys
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS

what's your idea ?? :?:
If your Dreams Don't Scare You , They Aren't Big Enough

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: my problem with pygame EVENT !

Thu Sep 03, 2015 7:04 am

Have you called your script pygame.py (or do you have a pygame.py file in your script folder)? If so, rename it and delete the pygame.pyc file.

Post your full code too. It's hard to tell if this is the problem without this and the full error message.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Mahan_Micro
Posts: 44
Joined: Tue Aug 04, 2015 8:52 pm

Re: my problem with pygame EVENT !

Thu Sep 03, 2015 8:37 am

elParaguayo wrote:
Post your full code too. It's hard to tell if this is the problem without this and the full error message.
my code is in Mag pi 33 , page: 33
befor this I use pygame without problem for drawing different object like circle and...
and now when I push UP KEY of keyboard my program stop and error apear in python shell

thanks for your help
If your Dreams Don't Scare You , They Aren't Big Enough

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: my problem with pygame EVENT !

Thu Sep 03, 2015 10:45 am

Ok, post all your code and the full error message.

The point of my earlier answer was that if you do have a file called pygame in your script folder then your script will import that rather than the pygame module you want. This is a very common cause for the error message you're getting. It may not be the right answer here but we need more information to confirm this.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Mahan_Micro
Posts: 44
Joined: Tue Aug 04, 2015 8:52 pm

Re: my problem with pygame EVENT !

Fri Sep 04, 2015 5:55 am

Code: Select all

import pygame ,sys 
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS

width = 600
height = 600

pygame.init()
surface = pygame.display.set_mode((width,height))
pygame.display.set_caption(' Mohsen ')
#======================================================
playersize = 20
playerX = (width/2)-(height/2)
playerY = height - playersize
playerVX = 1.0
playerVY = 0.0
jumpheight = 25.0
movespeed = 1.0
maxspeed = 10.0
gravity = 1.0

leftdown = False
rightdown = False
havejumped = False
#======================================================
def move():
    global playerX,playerY,playerVX,playerVY,havejumped,gravity
    #move left
    if leftdown :
        if playerVX < 0.0:
            playerVX = movespeed
            playerVX = -playerVX
        if playerX >0:
            playerX += playerVX

    if rightdown :
        if playerVX <0.0:
            playerVX = movespeed    
    if playerX+playersize < width:
        playerX += playerVX

    if playerVY >1.0 :
        playerVY = playerVY*0.9
    else:
        playerVY = 0.0
        havejamped = False

    if playerY <height - playersize :
        playerY += gravity
        gravity = gravity * 1.1
    else :
        playerY = height - playersize
        gravity = 1.0

    playerY -= playerVY

    if (playerVX>0.0 and playerVX < maxspeed ) or (playerVX <0.0 and playerVX > -maxspeed) :
    
        if not havejumped and (leftdown or rightdown):
            playerVX = playerVX * 1.1
#====================================================================
def quitgame():
    pygame.quit()
    sys.exit()
#====================================================================
while True :
     surface.fill((0,0,0))
     pygame.draw.rect (surface,(255,0,0),(playerX,playerY,playersize,playersize))
     for event in GAME_EVENTS.get():
        if event.type==pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                leftdown = True
            if event.key == pygame.K_RIGHT:
                rightdown = True
            if event.key == pygame.K_up:
                if not havejumped:
                    havejumped = True
                    playerVY += jumpheight
            if event.key == pygame.K_SCAPE:
                quitgame()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                leftdown = False
                playerVX = movespeed              
            if event.key == pygame.K_RIGHT:
                rightdown = False
                playerVX = movespeed
#========================================================================
       # if event.type == GAME_GLOBALS.QUIT():
        #    quitgame()
     move()

     pygame.display.update()
            
                    
                    
            
        
        
        
            




This is my code (and I run this by raspberry,no my laptop)
If your Dreams Don't Scare You , They Aren't Big Enough

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: my problem with pygame EVENT !

Fri Sep 04, 2015 7:47 am

Ok, the error message you posted in the first post was incorrect (it's better to copy and paste it, rather than retype it).

Your code has

Code: Select all

if event.key == pygame.K_up:
this should be

Code: Select all

if event.key == pygame.K_UP:
Python is case sensitive so this is an important difference.

Also, check out your next lines:

Code: Select all

if event.key == pygame.K_SCAPE:
should be

Code: Select all

if event.key == pygame.K_ESCAPE:
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Mahan_Micro
Posts: 44
Joined: Tue Aug 04, 2015 8:52 pm

Re: my problem with pygame EVENT !

Fri Sep 04, 2015 6:32 pm

This is my error:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/game2.py", line 75, in <module>
    if event.key == pygame.K_Up:
AttributeError: 'module' object has no attribute 'K_Up'
I see this error when any key i press (any key :!: )
If your Dreams Don't Scare You , They Aren't Big Enough

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: my problem with pygame EVENT !

Fri Sep 04, 2015 6:37 pm

You need to take far more care over your typing. Look at what I wrote:

Code: Select all

if event.key == pygame.K_UP:
That is not the same as you've got written.

You'll see the error when you press any key because your script needs to check if any of your if statements are satisfied.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
Mahan_Micro
Posts: 44
Joined: Tue Aug 04, 2015 8:52 pm

Re: my problem with pygame EVENT !

Sat Sep 05, 2015 4:22 pm

elParaguayo wrote:You need to take far more care over your typing. Look at what I wrote:

Code: Select all

if event.key == pygame.K_UP:
That is not the same as you've got written.

You'll see the error when you press any key because your script needs to check if any of your if statements are satisfied.
Thanks a lot :) :) :)
If your Dreams Don't Scare You , They Aren't Big Enough

Return to “Python”