[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re: [pygame] Re: [pygame] [pygame] Re: [pygame] [pygame] Moving a ….sprite…item?





 Sumarizing:


On Wed, Feb 29, 2012 at 1:22 AM, Zack Baker <zbaker1398@xxxxxxxxx> wrote:
 
blitted correctly i think but i keep getting... KeyError: 4

 KeyError: 4

 You're accessing a dictionary with key = 4 and there is no such key in the dict. See:

 
    arrowpos = { 0 : (100,20) , 1:(100,40) , 2:(100,60) , 3 :(100,60) }

 arrowpos[0] exists. And also arropos[1], [2] and [3]. But not arrowpos[4].

 So you should never access arrowpos[4], that would raise a KeyError: 4 error.

 And why are you accesing arrowpos[4]? :

 Due to this:

1.- Here you access arrowpos, notice that is via the "counter" variable:
 
    menuscreen.blit(arrow,arrowpos[counter])

 So, "counter", somewhere can reach the value "4", and it shouldn't.

 Where? Here:
 
            if event.key == K_DOWN:
                if counter < 4:
                    counter += 1
                    menuscreen.blit(arrow,arrowpos[counter])


if counter < 3:
   counter += 1

When counter is 3, and you press K_DOWN, you increment it from 3 to 4 via counter+=1.

The right code should be:

   if counter < 3:


 instead of

 if counter < 4:


 This way you only will increase "counter" if it's < 3,

counter = 0 -> counter = 1
counter = 1 -> counter = 2
counter = 2 -> counter = 3
counter = 3 -> IF FAILS -> counter never reaches 4


 I hope the explanation it's clear.

 See you!

--
Santiago Romero
Ubuntu GNU/Linux
http://www.sromero.org