1

I'm watching a tutorial on youtube by thenewboston http://www.youtube.com/watch?v=9YWzFcHMz78&feature=share&list=EC8E21BDD0981FDF66 and I'm copying his code yet mine isn't working. Whenever I press the down arrow key, the whole program freezes and won't let me quit. I'm using python 3.3. Please help. Here's my code.

grey="grey.jpg"
circle="circle.png"

import pygame, sys
from pygame.locals import*

pygame.init()
screen=pygame.display.set_mode((1278,990),0,32)

background=pygame.image.load(grey).convert()
pic=pygame.image.load(circle).convert()

x,y=0,0
movex,movey=0,0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_LEFT:
                movex=-1
            elif event.key==K_RIGHT:
                movex=+1
            elif event.key==K_UP:
                movey=-1
            elif event.key==K_DOWN:
                movey=+1
        if event.type==KEYUP:
            if event.key==K_LEFT:
                movex=0
            elif event.key==K_RIGHT:
                movex=0
            elif event.key==K_UP:
                movey=0
            elif event.key==K_Down:
                movey=0
    x+=movex
    y+=movey

    screen.blit(background, (0,0))
    screen.blit(pic,(x,y))

    pygame.display.update()
1
  • Line 37 in your code, it should be K_DOWN not K_down. Commented Feb 2, 2014 at 23:43

1 Answer 1

4

Quick run of the code:

$ python test1.py 
Traceback (most recent call last):
  File "test1.py", line 37, in <module>
    elif event.key==K_Down:
NameError: name 'K_Down' is not defined

The error is displayed when the program crashes on a down arrow push.

The crash is due to a typo, you have "K_Down" when it needs to be "K_DOWN".

Corrected code:

grey="grey.jpg"
circle="circle.png"

import pygame, sys
from pygame.locals import*

pygame.init()
screen=pygame.display.set_mode((1278,990),0,32)

background=pygame.image.load(grey).convert()
pic=pygame.image.load(circle).convert()

x,y=0,0
movex,movey=0,0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type==KEYDOWN:
            if event.key==K_LEFT:
                movex=-1
            elif event.key==K_RIGHT:
                movex=+1
            elif event.key==K_UP:
                movey=-1
            elif event.key==K_DOWN:
                movey=+1
        if event.type==KEYUP:
            if event.key==K_LEFT:
                movex=0
            elif event.key==K_RIGHT:
                movex=0
            elif event.key==K_UP:
                movey=0
            elif event.key==K_DOWN:
                movey=0
    x+=movex
    y+=movey

    screen.blit(background, (0,0))
    screen.blit(pic,(x,y))

    pygame.display.update()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.