Re: [Tutor] invalid literal for int error in Game loop

2013-01-05 Thread Ciaran Mooney


On 4 Jan 2013, at 19:20, Francesco Loffredo f...@libero.it wrote:

 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


Thanks Francesco,
Ill give it a go and come back to u.

Ciaran
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2013-01-04 Thread Francesco Loffredo

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Ciaran Mooney
HI All,

Was hoping someone could help me with a problem I am having programming a game 
using pygame.

I am trying to return a variable with a integer value defined by user input 
from a function.

Basically the idea is to allow the player to select the level of difficulty by 
selecting from a choice of keys, each key corresponding too  the number of 
frames per second. The function code is as follows:

def difficultyLevel():
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

The function is then called and FPS converted too a  integer value in the main 
game loop as follows:

topScore = 0
while True:
#set up the game and all intial key and mouse movement values as false

baddies = [] #baddies as a empy list
score = 0
etc

FPS = int(difficultyLevel())

FPS is then used within the game loop  to allow for number of frames per second 
in:

mainClock.tick(FPS)

However I keep getting the following error:

ValueError: invalid literal for int() with base 10: ''


Please note I am a absolute beoingeer in programming and have had no formal 
education to date so please excuse anything I may of left out !!


Thanks
Ciaran

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Steven D'Aprano

On 21/12/12 07:40, Ciaran Mooney wrote:


def difficultyLevel():
 FPS = ''


Here you set the variable FPS to the empty string.


 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


These three options set FPS to an integer value. But notice that if the user
does not press one of the b m or h keys, FPS never gets changed so it still
has the initial value of the empty string.


 return FPS

The function is then called and FPS converted too a  integer value in the main 
game loop as follows:



 FPS = int(difficultyLevel())



However I keep getting the following error:

ValueError: invalid literal for int() with base 10: ''


There are three possible values that difficultyLevel() may return:

* if the user hits the 'h' key, it returns the integer value 120;
* if the user hits the 'm' key, it returns the integer value 70;
* if the user hits the 'b' key, it returns the integer value 30;
* otherwise, it returns the empty string.

Three of the four values are already ints and don't need to be converted;
the fourth cannot be converted because it is not a numeric string.

To fix this, change the line FPS = '' to a default integer value,
say, FPS = 30.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Dave Angel
On 12/20/2012 03:51 PM, Steven D'Aprano wrote:
 On 21/12/12 07:40, Ciaran Mooney wrote:

 def difficultyLevel():
  FPS = ''

 Here you set the variable FPS to the empty string.

  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

 These three options set FPS to an integer value. But notice that if
 the user
 does not press one of the b m or h keys, FPS never gets changed so it
 still
 has the initial value of the empty string.

  return FPS

 The function is then called and FPS converted too a  integer value in
 the main game loop as follows:

  FPS = int(difficultyLevel())

 However I keep getting the following error:

 ValueError: invalid literal for int() with base 10: ''

 There are three possible values that difficultyLevel() may return:

 * if the user hits the 'h' key, it returns the integer value 120;
 * if the user hits the 'm' key, it returns the integer value 70;
 * if the user hits the 'b' key, it returns the integer value 30;
 * otherwise, it returns the empty string.

 Three of the four values are already ints and don't need to be converted;
 the fourth cannot be converted because it is not a numeric string.

 To fix this, change the line FPS = '' to a default integer value,
 say, FPS = 30.




Or even better, remove that default value and add an else clause to the
if/elif/elif section.  That way it's all in one place, and it's more
obvious that the return value will always be an int.

And of course you can remove the int() call on the line
FPS = int( difficultyLevel() )
becomes
FPS = difficultyLevel()

BTW, when quoting an error, please include the whole traceback, not just
one line of it.  Here it was obvious, but many times other parts will be
very useful, or even essential.



-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Ciaran Mooney
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
On 20 Dec 2012, at 21:07, Dave Angel d...@davea.name wrote:

 On 12/20/2012 03:51 PM, Steven D'Aprano wrote:
 On 21/12/12 07:40, Ciaran Mooney wrote:
 
 def difficultyLevel():
 FPS = ''
 
 Here you set the variable FPS to the empty string.
 
 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
 
 These three options set FPS to an integer value. But notice that if
 the user
 does not press one of the b m or h keys, FPS never gets changed so it
 still
 has the initial value of the empty string.
 
 return FPS
 
 The function is then called and FPS converted too a  integer value in
 the main game loop as follows:
 
 FPS = int(difficultyLevel())
 
 However I keep getting the following error:
 
 ValueError: invalid literal for int() with base 10: ''
 
 There are three possible values that difficultyLevel() may return:
 
 * if the user hits the 'h' key, it returns the integer value 120;
 * if the user hits the 'm' key, it returns the integer value 70;
 * if the user hits the 'b' key, it returns the integer value 30;
 * otherwise, it returns the empty string.
 
 Three of the four values are already ints and don't need to be converted;
 the fourth cannot be converted because it is not a numeric string.
 
 To fix this, change the line FPS = '' to a default integer value,
 say, FPS = 30.
 
 
 
 
 Or even better, remove that default value and add an else clause to the
 if/elif/elif section.  That way it's all in one place, and it's more
 obvious that the return value will always be an int.
 
 And of course you can remove the int() call on the line
FPS = int( difficultyLevel() )
 becomes
FPS = difficultyLevel()
 
 BTW, when quoting an error, please include the whole traceback, not just
 one line of it.  Here it was obvious, but many times other parts will be
 very useful, or even essential.
 
 
 
 -- 
 
 DaveA
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Dave Angel
On 12/20/2012 04:34 PM, 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
 
Please don't top-post.  Your message is now out of order with whatever
you're quoting below.

You don't show your new version of code, but clearly, your else is NOT
in the right place.  If it were, then there would be no way that FPS
would be undefined.

And this is exactly why it's a good thing to use else rather than
defining FPS at the top.  The error message shows you your error, or at
lest makes you think more about the problem.

You have a for-loop here.  Is it possible that it's not executing at
all?  If so, what value do you want for FPS?  Do you just want to wait
till the user causes SOME event?  or what?

And what about if you go around the loop multiple times?  Do you want
the last value of FPS, or the first?

Assuming you want to take the first matching event that's waiting, you
could do this.  The breaks get you out of the loop

for event in pygame.event.get():
if event.type == QUIT:
terminate()

if event.type == KEYDOWN:
if event.key == ord('b'):
FPS = 30
break
elif event.key == ord('m'):
FPS = 70
break
elif event.key == ord('h'):
FPS = 120
break
else:
FPS = 30#default value

return FPS

This doesn't address the problem of the user not being fast enough at
hitting that key.  Perhaps you want to wait till he hits something, or
till a timeout has passed, or something?

Without a clear spec in your head, the fact that we could help you get
your code to not crash is not the same as getting it right.

-- 

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] invalid literal for int error in Game loop

2012-12-20 Thread Prasad, Ramit
Ciaran Mooney wrote:
 Sent: Thursday, December 20, 2012 3:35 PM
 To: d...@davea.name
 Cc: tutor@python.org
 Subject: Re: [Tutor] invalid literal for int error in Game loop
 
 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

If you read Dave's comment a little closer you will see that
he suggests adding an *else* block not another elif. 

(pseudocode)
if key == b:
FPS = 30
elif key == m:
FPS = 60 
elif key == h:
FPS = 120
else:
FPS = 15 # or whatever default value you want

 
 
 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

The else clause prevents FPS from being anything other than a number.
You can choose to predefine FPS or just let the if/else tree take
care of defining it.

 
 Cheers
 Ciaran
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor