This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-06-09 18:52:10

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Python/Pygame Question

So I don't know if it's okay to ask the question here, but I figure someone has to know the answer and this is really the only forums I'm on.

Anyway, I've started learning Python and Pygame programming.

I'm trying to make velocity in Pygame, but for some reason I can't figure out how to use it when a key is held down.  the velocity only works when it's pressed and doesn't work until the key is pressed again.  here's my script so far:

import pygame, sys
from pygame.locals import *

pygame.init()

screen=pygame.display.set_mode((480,360),0,32)
background=pygame.image.load(bif).convert()
mouse_c=pygame.image.load(mif).convert_alpha()

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=movex-.05
            elif event.key==K_RIGHT:
                movex=movex+.05
            elif event.key==K_UP:
                movey=movey-.05
            elif event.key==K_DOWN:
                movey=movey+.05

    x=x+movex
    if x>400 or x<0:
        movex=movex*-1
    y=y+movey
    if y>280 or y<0:
        movey=movey*-1
       
    movex=movex*.999
    movey=movey*.999
    screen.blit(background, (0,0) )
    screen.blit(mouse_c, (x,y) )

    pygame.display.update()

So my question is:  How do I get Pygame to recognize a key being held down?

Thanks in advance,
AtomicBawm3

Last edited by AtomicBawm3 (2011-06-09 18:52:38)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#2 2011-06-10 13:05:29

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Python/Pygame Question

Bump.  Please help if you can.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#3 2011-06-10 13:10:24

meew0
Scratcher
Registered: 2010-02-22
Posts: 1000+

Re: Python/Pygame Question

I am familiar with Python, but not with Pygame.  sad  Sorry...


http://i.imgur.com/mJV3j.pnghttp://i.imgur.com/HwWAX.pnghttp://i.imgur.com/sZ7Ui.pnghttp://i.imgur.com/0y6yh.pnghttp://i.imgur.com/nOC4l.png

Offline

 

#4 2011-06-10 15:49:10

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Python/Pygame Question

Bump.  Please, even  if you just had to wager a guess.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

Board footer