Re: [pygame] keep a key pressed and then move a sprite

2010-11-26 Thread Leif Theden
for my project "python fighter framework"
(http://www.pygame.org/project-fighter+framework-1550-2747.html), i
handle keyup and keydown events separately and maintain a dictionary
of pressed keys.  then the game can properly handle multiple key
presses (though subject to the evil keyboard problem).

also, i have a dict that maps keys to specific actions, so that
reconfiguring the keys doesn't require modifying the source code.
consider:

BUTTON_HI_KICK = 1
BUTTON_MED_KICK = 2
BUTTON_LOW_KICK = 4
etc...

# define the default keys.  change the dict at runtime or with a config file.
p1_key_def = {
   BUTTON_HI_KICK: K_e,
   BUTTON_MED_KICK: K_w,
   etc...
}

---
then later

if keys[p1_key_def[BUTTON_HI_KICK]]:
   do stuff

if keys[p1_key_def[BUTTON_MED_KICK]]:
   do stuff


if you have a background that doesn't change often, then i would say
that 99% of the time you are better off with the "dirty rect" method
of drawing.  I use that for my game and it works pretty well.

On Fri, Nov 26, 2010 at 2:18 AM, Nathan BIAGINI  wrote:
> Ok thanks but i saw that psyco do not supports 2.7 release of python so i
> think i ll not try it for the moment.
>
> Ok so thanks everybody be sure to see me again on this mail list, i ll of
> course do my best to read doc to solve my problem the best i can, it s
> always a pretty good exercice for my english haha
>
> 2010/11/24 Kris Schnee 
>>
>> On 2010.11.24 1:53 PM, Nathan BIAGINI wrote:
>>>
>>> Ok thanks to reply so fast :) yeah of course, it seems obvious now but i
>>> didn't know there is a way to manage keyboard state without the common
>>> event loop.
>>> I ll use this created topic to ask something else about the optimization
>>> of a game written with pygame. Restrict frames per second with
>>> clock.tick(xx) and only update dirty rects (in case of static
>>> background) is enough for a quite "simple" 2D game? Or there re more
>>> optimizations to do? I don't try to reach perfect performance, only want
>>> to make my faster as possible and less CPU consuming.
>>
>> One other thing to try is an acceleration system.
>> "Psyco" uses a compiler to speed up a Python program very easily. See
>> http://psyco.sourceforge.net/ . All you need to do after installing it is
>> add the code "import psyco ; psyco.full()".
>>
>> "Boost" ( http://beta.boost.org/doc/libs/1_45_0/libs/python/doc/index.html
>> ) lets you write code in the C language and access that from Python. I've
>> not tried that.
>
>


Re: [pygame] keep a key pressed and then move a sprite

2010-11-25 Thread Nathan BIAGINI
Ok thanks but i saw that psyco do not supports 2.7 release of python so i
think i ll not try it for the moment.

Ok so thanks everybody be sure to see me again on this mail list, i ll of
course do my best to read doc to solve my problem the best i can, it s
always a pretty good exercice for my english haha

2010/11/24 Kris Schnee 

> On 2010.11.24 1:53 PM, Nathan BIAGINI wrote:
>
>> Ok thanks to reply so fast :) yeah of course, it seems obvious now but i
>> didn't know there is a way to manage keyboard state without the common
>> event loop.
>> I ll use this created topic to ask something else about the optimization
>> of a game written with pygame. Restrict frames per second with
>> clock.tick(xx) and only update dirty rects (in case of static
>> background) is enough for a quite "simple" 2D game? Or there re more
>> optimizations to do? I don't try to reach perfect performance, only want
>> to make my faster as possible and less CPU consuming.
>>
>
> One other thing to try is an acceleration system.
> "Psyco" uses a compiler to speed up a Python program very easily. See
> http://psyco.sourceforge.net/ . All you need to do after installing it is
> add the code "import psyco ; psyco.full()".
>
> "Boost" ( http://beta.boost.org/doc/libs/1_45_0/libs/python/doc/index.html) 
> lets you write code in the C language and access that from Python. I've
> not tried that.
>


Re: [pygame] keep a key pressed and then move a sprite

2010-11-24 Thread Greg Ewing

On 25/11/10 05:48, Nathan BIAGINI wrote:

Hi everyone,

i m learning to use Pygame and i wonder how make a sprite moving by
keeping a key pressed.


Set a flag when you receive a KEYDOWN event and clear it
when you receive a KEYUP event.

--
Greg


Re: [pygame] keep a key pressed and then move a sprite

2010-11-24 Thread Bill Coderre

On Nov 24, 2010, at 12:40 PM, Kris Schnee wrote:
> On 2010.11.24 1:53 PM, Nathan BIAGINI wrote:
>> Ok thanks to reply so fast :) yeah of course, it seems obvious now but i
>> didn't know there is a way to manage keyboard state without the common
>> event loop.
>> I ll use this created topic to ask something else about the optimization
>> of a game written with pygame. Restrict frames per second with
>> clock.tick(xx) and only update dirty rects (in case of static
>> background) is enough for a quite "simple" 2D game? Or there re more
>> optimizations to do? I don't try to reach perfect performance, only want
>> to make my faster as possible and less CPU consuming.
> 
> One other thing to try is an acceleration system.
> "Psyco" uses a compiler to speed up a Python program very easily. See 
> http://psyco.sourceforge.net/ . All you need to do after installing it is add 
> the code "import psyco ; psyco.full()".
> 
> "Boost" ( http://beta.boost.org/doc/libs/1_45_0/libs/python/doc/index.html ) 
> lets you write code in the C language and access that from Python. I've not 
> tried that.

There is a saying widely attributed to Don Knuth, "Premature optimization is 
the root of all evil."

The Wikipedia article on Optimization explains well: 
http://en.wikipedia.org/wiki/Program_optimization

The general theory is:
1) Pick an efficient algorithm to start off with. In your case, only drawing 
the changed stuff might be a good choice, but it might depend on your graphics 
card. It might be just as fast to draw everything.

2) Do not "optimize" in the traditional sense of, for instance, rewriting 
clear, obvious code as "faster" but obscure stuff. This is for three reasons:
2a) You might optimize the wrong piece of code -- Kernighan and Plaugher's 
"Elements of Programming Style" explains a case like this.

2b) Your "optimization" might not actually help, or might even make things 
worse, if the compiler is smart. "Do not write C code in Python." 
2c) Irrespective of 2a and 2b, you WILL end up with messy, hard to read code 
that is more likely to have bugs. Python's main advantage over C is that it can 
be easier to read and understand. Don't throw that away to get a tiny speedup! 
(But see step 4, below.)

3) Run the code, and see if it is actually slow. If not, you're done. THIS IS 
THE MOST IMPORTANT STEP.

4) If it is slow, do measurements to figure out WHERE it is slow. Then look at 
that part, and figure out if there's a faster algorithm to use in that place. 
For instance, you might find that you're using a lot of objects in a loop, 
creating a lot of garbage to be collected. Switching your approach to one that 
re-uses objects could fix that -- but it might also make the code hard to read, 
so you don't want to do that unless you absolutely have to. In any case, save 
copies so you can switch back.

5) If everything is slow, you might get "for free" speedup using psyco. But 
that won't help, for instance, if your code mostly runs fine, but occasionally 
slows down due to garbage collection.


All that being said, I know it's easy to put a frame rate counter on your code, 
and then get excited about making the number bigger. If you're just learning to 
code, BY ALL MEANS hack away! Just try to learn from your hacks, especially 
based on the advice above. And definitely spend a little time learning how to 
use Subversion, Git, or Mercurial, so you can revert your changes quickly.




Re: [pygame] keep a key pressed and then move a sprite

2010-11-24 Thread Kris Schnee

On 2010.11.24 1:53 PM, Nathan BIAGINI wrote:

Ok thanks to reply so fast :) yeah of course, it seems obvious now but i
didn't know there is a way to manage keyboard state without the common
event loop.
I ll use this created topic to ask something else about the optimization
of a game written with pygame. Restrict frames per second with
clock.tick(xx) and only update dirty rects (in case of static
background) is enough for a quite "simple" 2D game? Or there re more
optimizations to do? I don't try to reach perfect performance, only want
to make my faster as possible and less CPU consuming.


One other thing to try is an acceleration system.
"Psyco" uses a compiler to speed up a Python program very easily. See 
http://psyco.sourceforge.net/ . All you need to do after installing it 
is add the code "import psyco ; psyco.full()".


"Boost" ( 
http://beta.boost.org/doc/libs/1_45_0/libs/python/doc/index.html ) lets 
you write code in the C language and access that from Python. I've not 
tried that.


Re: [pygame] keep a key pressed and then move a sprite

2010-11-24 Thread Saul Spatz
It should be enough for simple games.  If you go to this link
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470068221,descCd-DOWNLOAD.html
you
can download all the code for the book, and you'll see that the simple games
just use these techniques.  (I'm not recommending the book, just pointing
out a source of code for simple 2D games.)

P.S. Your English is not perfect, but quite understandable; keep up the good
work.

Saul

On Wed, Nov 24, 2010 at 12:53 PM, Nathan BIAGINI wrote:

> Ok thanks to reply so fast :) yeah of course, it seems obvious now but i
> didn't know there is a way to manage keyboard state without the common event
> loop.
> I ll use this created topic to ask something else about the optimization of
> a game written with pygame. Restrict frames per second with clock.tick(xx)
> and only update dirty rects (in case of static background) is enough for a
> quite "simple" 2D game? Or there re more optimizations to do? I don't try to
> reach perfect performance, only want to make my faster as possible and less
> CPU consuming.
>
> PS : i'm sorry but i do my best to try to write an understandable english,
> it s not my native langage at all and i m pretty young but i was forced to
> learn it to enjoy python/pygame ressources avaiable on the Internet (there
> is no many french ressources on the Internet...)
>
> 2010/11/24 Ian Mallett 
>
>> Hi,
>>
>> Try this:
>>
>>
>> while True:
>>clock.tick(60) #set on 60 frames per second
>>
>>for event in pygame.event.get():
>>if event.type == QUIT:
>> sys.exit()
>>
>>key = pygame.key.get_pressed()
>>
>>if key[K_LEFT]: heroes.move_left()
>>
>>
>> pygame.display.update(heroes_group.draw(screen))
>>
>> The PyGame event manager fires events *only when a key's state changes *(that
>> is, the moment it is pressed or the moment it was released, a *single *event
>> is fired).
>>
>> What you want to do is to check the *current state *of the keyboard.  "
>> pygame.key.get_pressed()" does this.  The state "key" that it returns can
>> then be checked for the required key.
>>
>> Ian
>>
>
>


Re: [pygame] keep a key pressed and then move a sprite

2010-11-24 Thread Nathan BIAGINI
Ok thanks to reply so fast :) yeah of course, it seems obvious now but i
didn't know there is a way to manage keyboard state without the common event
loop.
I ll use this created topic to ask something else about the optimization of
a game written with pygame. Restrict frames per second with clock.tick(xx)
and only update dirty rects (in case of static background) is enough for a
quite "simple" 2D game? Or there re more optimizations to do? I don't try to
reach perfect performance, only want to make my faster as possible and less
CPU consuming.

PS : i'm sorry but i do my best to try to write an understandable english,
it s not my native langage at all and i m pretty young but i was forced to
learn it to enjoy python/pygame ressources avaiable on the Internet (there
is no many french ressources on the Internet...)

2010/11/24 Ian Mallett 

> Hi,
>
> Try this:
>
>
> while True:
>clock.tick(60) #set on 60 frames per second
>
>for event in pygame.event.get():
>if event.type == QUIT:
> sys.exit()
>
>key = pygame.key.get_pressed()
>
>if key[K_LEFT]: heroes.move_left()
>
>
> pygame.display.update(heroes_group.draw(screen))
>
> The PyGame event manager fires events *only when a key's state changes *(that
> is, the moment it is pressed or the moment it was released, a *single *event
> is fired).
>
> What you want to do is to check the *current state *of the keyboard.  "
> pygame.key.get_pressed()" does this.  The state "key" that it returns can
> then be checked for the required key.
>
> Ian
>


Re: [pygame] keep a key pressed and then move a sprite

2010-11-24 Thread Kris Schnee

On 2010.11.24 12:27 PM, Ian Mallett wrote:

Hi,

Try this:

The PyGame event manager fires events /only when a key's state changes
/(that is, the moment it is pressed or the moment it was released, a
/single /event is fired).

What you want to do is to check the /current state /of the keyboard.
"pygame.key.get_pressed()" does this.  The state "key" that it returns
can then be checked for the required key.


That's all correct. One other option (though get_pressed is probably 
better) is to watch for both the KEYDOWN and KEYUP events. You could 
start the characters moving on a KEYDOWN event and keep moving them 
until there's a KEYUP event.


Constants like K_LEFT are defined in pygame.locals, so you can either 
reference them like "pygame.locals.K_LEFT" or (easier) say "from 
pygame.locals import *" so you can just say "K_LEFT".


Also, consider separating the code for what happens to the 
character/sprite from the bit that reacts to keyboard input. I try to do 
something like:

"if keys[K_RIGHT]: player.speed_x = player.speed
if keys[K_UP]: player.speed_y = -player.speed
player.coords[x] += player.speed_x; player.coords[y] += player.speed_y"
That kind of thing lets you separate the movement logic a bit from the 
way it's inputted.


Note that if you have trouble with certain combinations of keys not 
registering when pressed together, that's a known problem inherent to 
keyboards. Google "keyboards are evil" for an explanation.


Re: [pygame] keep a key pressed and then move a sprite

2010-11-24 Thread Ian Mallett
Hi,

Try this:

while True:
   clock.tick(60) #set on 60 frames per second

   for event in pygame.event.get():
   if event.type == QUIT:
   sys.exit()

   key = pygame.key.get_pressed()

   if key[K_LEFT]: heroes.move_left()

   pygame.display.update(heroes_group.draw(screen))

The PyGame event manager fires events *only when a key's state changes *(that
is, the moment it is pressed or the moment it was released, a *single *event
is fired).

What you want to do is to check the *current state *of the keyboard.  "
pygame.key.get_pressed()" does this.  The state "key" that it returns can
then be checked for the required key.

Ian