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

Re: [pygame] newbie



Thanks for the 'input' :.P

Ah, yes I see what's happening, the last time I was doing it I was using:
...........KEYDOWN:
if event.key == K_LEFT:
   foreward = True
...........KEYUP:
if event.key == K_LEFT:
   foreward = False
meaning it was staying true, until key up but now it's not keeping the output next time round & waiting for it again. could you elaborate on this bit 'You could make the character move every frame (eg. setting a speed and moving by that speed per frame) until a KEYUP event happens', or is that pretty much what I 'was' doing?

My aim is to keep the input controls as a completely separate (independent & reusable) module, i'm not sure how using True & False is workable.

Could I use "keys_down = pygame.key.get_pressed" then
                     "if keys_down[K_RIGHT] ## move right",

in this way:
class character():
................
   def update(self, d, sp):
       if d == 6:
           self.x += sp
       elif d == 4:
           self.x -= sp
       elif d == 8:
           self.y -= sp
       elif d == 2:
           self.y += sp

main loop:
while True:
   Display.window.fill((0, 0, 0))

   D = Keyboard.controls()
   ball.draw(100, 100)
   ball.update(D, 1)

controls:
def controls():

   output = 0
   keysDown = pygame.key.get_pressed():
   if keysDown[K_RIGHT]:
       output = 6

?
--------------------------------------------------
From: "Kris Schnee" <kschnee@xxxxxxxxxx>
Sent: Thursday, March 25, 2010 9:01 PM
To: <pygame-users@xxxxxxxx>
Subject: Re: [pygame] newbie

On Thu, 25 Mar 2010 19:10:22 -0000, snet-1@xxxxxxx wrote:
Hi guys.

I'm new to python and programming in general so expect noob questions :.)

I've got up to classes & functions & having problems getting round a
little
input problem.

I've put my keyboard controls in a separate module from the pygames main
loop and my 'character' in another module.
When I was putting it all in one file, if I held left my 'character'
carried
on moving left, but now it will only move the specified amount of pixels
then stop.

I think part of the problem is that you're looking at KEYDOWN events. Those
only happen when a key is pressed, meaning there's only one no matter how
long you hold the key. So the code would make your character move for one
frame, then stop.

You could make the character move every frame (eg. setting a speed and
moving by that speed per frame) until a KEYUP event happens. A different
way to handle things is to use pygame.key.get_pressed() (see
http://www.pygame.org/docs/ref/key.html ), which tells you what keys are
down at the moment you ask. You then say, "keys_down =
pygame.key.get_pressed" then "if keys_down[K_RIGHT] ## move right", and so
on.

One example is in my messy code at
http://kschnee.xepher.net/code/squirrelgame/squirrel4.py.txt , under
"Controls".