[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?



On Tue, Feb 28, 2012 at 5:22 PM, Zack Baker <zbaker1398@xxxxxxxxx> wrote:
Thank you all!!!! you are helping me so much. I thought i was pretty proficient in pygame so im very sorry for my 'stupid' questions but i cannot for the life of me figure this one out. I made all the neceesary changes and ive blitted correctly i think but i keep getting... KeyError: 4


import pygame 

pygame.init()
import sys,random,os
from pygame.locals import*
from Classes import Arrow 

clock=pygame.time.Clock()
clock.tick(30)

def main_menu():
    menuclock=pygame.time.Clock()
    clock.tick(30)

    

    menuscreen=pygame.display.set_mode((640, 480))#FULLSCREEN
    pygame.display.set_caption('Dog Fight')
    #pygame.mouse.set_visible(False)

    WHITE=(255,255,255)
    BLACK=(0,0,0)
    GREEN=(0,255,0)
    BLUE=(0,0,255)
    background="" style="color:#2934d5">'data/background.png')
    lasersound=pygame.mixer.Sound('data/lasershot.wav')
    arrow=pygame.image.load('data/arrow.png')
    menuscreen.blit(background,(0,0))
    arrowpos = { 0 : (100,20) , 1:(100,40) , 2:(100,60) , 3 :(100,60) }
    def dostuff():
        print('Hi')

    


    menu='go'
    counter = 0
    menuscreen.blit(arrow,arrowpos[counter])

    while menu == "go":
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                menu = "off"

            if event.key == K_UP:
                if counter > 0:
                    counter -= 1
                    menuscreen.blit(arrow,arrowpos[counter])


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


            if event.key == K_RETURN:
                return counter

        pygame.display.update()



main_menu()







    

    


THANK YOU!!!!!
The issue arises when you tell it to "arrowpos[counter]".  The "KeyError: 4" should tell you something--namely that there's a key, and you gave it 4, and it doesn't like that.  If you look at arrowpos, you'll immediately see why it doesn't like that.  Now, how can you fix it?  Is the data structure wrong, or is the value you're passing it (4) wrong?

Ian