[Tutor] invalid literal for int error in Game loop

Francesco Loffredo fal at libero.it
Fri Jan 4 20:20:53 CET 2013


Ciaran Mooney wrote:
> Thanks for the feedback.
>
> Steve, If I set the FPS to a default of say 30, the game seems to run at this default FPS=30 regardless of the key pressed in the 
> function.
>
> Dave, If I remove the the default value at the start of the function and add it to elif in the loop I get the following error:
>
>
> Traceback (most recent call last):
>   File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 139, in <module>
>     FPS = difficultyLevel()
>   File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 50, in difficultyLevel
>     return FPS
> UnboundLocalError: local variable 'FPS' referenced before assignment
>
>
> I wondered if i could set FPS to nether a string or integer and just declare it by setting FPS=None but I get the following error:
>
> Traceback (most recent call last):
>   File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 301, in <module>
>     mainClock.tick(FPS)
> TypeError: a float is required
>
> Cheers
> Ciaran
As far as I understood your problem, it seems that you need the difficulty level to be changed at the player's will during the game. 
In this case, the function gets called many times, maybe at every game loop. If this is true, you need a global FPS value to be 
changed by the player's choice, not one that you create inside the function.

Try this:

# put the line below out of the function definition and out of the game loop, usually together with other default program settings
FPS = 30  # you want the game to be beginner-friendly, don't you?

def difficultyLevel():
    global FPS
     windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.FULLSCREEN)
     windowSurface.fill(BACKGROUNDCOLOUR)
     drawText('LEVEL OF PLAY', font3, windowSurface, (WINDOWWIDTH/6), (WINDOWHEIGHT/6))
     drawText('B: Your an absoulute Begineer', font3, windowSurface, (WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+100)
     drawText('M: Not Too Snazy', font3, windowSurface, (WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+150)
     drawText('H: Deathwish' ,font3,windowSurface, (WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+200)
     pygame.display.update()

     for event in pygame.event.get():
         if event.type == QUIT:
             terminate()
         if event.type == KEYDOWN:
             if event.key == ord('b'):
                 FPS = 30
             elif event.key == ord('m'):
                 FPS = 70
             elif event.key == ord('h'):
                 FPS = 120
     return FPS

... then let us know.
Hope that  helps

Francesco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130104/020ee954/attachment.html>


More information about the Tutor mailing list