Picon
Gravatar

Podcast about write a Music Sampler with Midi and Pygame

I recorded a short podcast for hacker public radio talking about the awesome midi functions in pygame. I don't think i did it justice. Someone who really knows what they are talking about should do another one and submit it to the site. :D

http://hackerpublicradio.org/eps.php?id=0991


Mike Lawrence | 21 May 19:54
Picon
Gravatar

Why does pygame.sndarray.make_sound seemingly quadruple the sound duration?

The following code:

    import pygame, numpy
    pygame.mixer.pre_init(frequency=96000,size=-16,channels=1)
    pygame.init()
    a = numpy.random.randn(96000)
    sound = pygame.sndarray.make_sound(a)
    print sound.get_length()

yields a print-out of 4.0, suggesting that the specified duration of
96000 samples at a 96000kHz sampling rate was somehow quadrupled
somewhere along the way. Any idea what I'm missing here? Or is this a
bug?

Nicholas Seward | 14 May 16:12
Picon

pygame.mask.from_surface

One of my students was doing pixel perfect collisions with Sprites and
found what may be a bug.  pygame.mask.from_surface seemed to return an
empty mask when everything on a surface is opaque.  In this situation
rectangle collisions made more sense but I am still curious why
pygame.mask.from_surface is behaving this way.  I quickly dug through
the source and nothing popped out as the cause.

Sam Bull | 14 May 12:25
Favicon

Monospaced fonts are meant to be mono-spaced, right?

Before I go and file a bug against Ubuntu, can somebody confirm I'm not
being stupid.

For my input boxes, the cursor position depends on a monospaced font.
This means I check the length of a rendered letter ("e") using the font,
and then set the cursor position as a multiple of this length.

This worked fine before, but in Ubuntu 12.04, it no longer seems to be
monospaced. Getting the length of the character "e" on my system gives
me 9 pixels, but it seems that some characters, such as "h", are 10
pixels, which starts offsetting my cursor position and messing up my
input box.

The line I use to load the font is:
        pygame.font.SysFont("FreeMono, Monospace", 16)

That should definitely return a monospaced font, right?

And, the line used for getting the width is:
        mono_font.render("e", False, (0,0,0)).get_size()[0]

Is this a bug, or am I doing something stupid?

Winter | 11 May 23:32

pygame.org wiki spam

Hello,

I've been fighting the great fight against spam on the pygame.org wiki, 
but there's at least 2 accounts that seem to be doing automated changes: 
  spainadmon and Ronald Derren.  The Derren account is being 
particularly nasty these past few days.

I've gone from direct editing to reverts, but it's hard to keep up.  I 
could whip up an automated removal tool of my own but that wouldn't 
really fix the problem, and it just seems ungentlemanly to do the same 
as the attackers (even though I have better intentions).

Can prevention tools be added to the site?  Captcha's, IP blocks, 
anything?  I hate to see the site just plowed under by the BadGuys.

I feel for the webmaster(s), this isn't meant as a knock.  The attackers 
always have the advantage and I know it never ends.

Regards,

Winter

(W. Fielder on the pygame wiki recent changes)

Peter Finlayson | 11 May 10:38
Picon
Gravatar

Issue with pygame.mask

Hi,

I am using pygame.mask.from_surface(Surface, color, threshold = (0,0,0,255)) to convert a surface into
a 2D binary mask.

I expected the default threshold (0,0,0) of this function to have it match only that the exact color
specified (ie 
within 0 values of the color). However, it seems that it instead matches NO pixels. To get the behaviour I
expected I 
needed to specify a threshold of (1, 1, 1, 255)

Is this a bug, or are my expectations wrong?

Regards,
Peter Finlayson
Attachment (test_mask_from_threshold.py): text/x-python, 1063 bytes
Peter Finlayson | 10 May 16:13
Picon
Gravatar

Observations on collisions

Hi,

Recently, I did some benchmarking on the various ways to do Rect collision detections using Pygame. I
mentioned some of 
this on the IRC channel a couple weeks ago, and they said that some of you on this list may be interested.

In the past, I have always stored the location of my sprites in a ((x, y), (w, h)) tuple, in a .rect attribute,
because 
I knew Pygame would automatically convert these to Rects as needed. It occured to me that it may be more
efficient to 
instead store them as actual Rects, so I decided to do some testing.

I got some surprising results, so I did more testing, and here's what I found when colliding a single item
against 100 
items (with 100 000 interations):

a rect and a list of tuples                    1.12 seconds,    5.9x
a rect and a list of objects containg tuples   3.19 seconds,    16.9x
a rect and a list of rects                     0.19 seconds,    1.0x
a rect and a list of subclassed rects          1.00 seconds,    5.3x
a rect and a list of objects containing rects  2.19 seconds,    11.6x
a sprite and a group of sprites                1.70 seconds,    9.0x
a sprite and an ordered group of sprites       1.55 seconds,    8.2x

The fastest way to do collisions was to do a Rect.collidelistall(). The surprising thing to me was how much
slower my 
chosen method was (Storing tuple in the .rect attributes of my classes). Up to twenty times slower on some tests.

Other things I found interesting:

Using the collisions offered by the Sprite class is convenient, but quite slow.
Subclassing the Rect and using that for your own sprites causes quite a loss of speed (5x slower).
The extra lookup to extract the .rect attribute from an object is quite expensive in Python.
Creating a (pos, size) tuple is marginally slower than creating a Rect, but the advantages of the Rect far
outweigh that 
cost.

Conclusions:

Well, testing shows I was doing things The Wrong Way (tm) :) A lot of this is probably familiar to those of you
who are 
familiar with Pygame internals, but the scale of the difference was a shock to me. The relative advantage of
the list of 
Rects improves even more as you add more objects to collide with. At 200 objects, it was 20x times faster the
my 
original method.

There is also the issue of /relevance/. Collision probably aren't the biggest slice of your game's time,
but if you are 
aiming for 60 FPS, my method was eating up 3.2 milliseconds each frame. That's a significant proportion of
the 16ms budget.

I have attached the code that produced these results.

Regards,
Peter Finlayson
Attachment (collision-test.py): text/x-python, 6209 bytes
Silver | 9 May 05:30
Picon

help with clock

I wrote a small clock program that I might use if it turns out well, but
I am having a small issue where the top of the arcs that grow over time
shifts when I would like it to stay still. Does anyone know how to
achieve this?
Attachment (clock.py): text/x-python, 1175 bytes
Witton, Caroline | 4 May 11:13
Picon
Picon
Favicon

sndarray with numpy array in Windows

Hello,

I'm a researcher using pygame to measure sensory processing in children.  I want to use sndarray to play out a
numpy array I have created, containing a sound stimulus. But I can't seem to get any sound at all.  I'm not an
expert programmer so I would welcome any advice.

I'm using Windows 7, with python 2.7, and the array will play out fine (as float) if I use pyaudiere.  A
simplified version of my code is below - interestingly I also get no sound when I run the example which
someone has kindly posted as a comment on the sndarray doc page. I am baffled!

Many thanks
Caroline

from numpy import *
import pygame

SRATE=44100 # sample rate in Hz
DURATION=1 # duration in sec
# an array of floating-point random numbers
noise= array(random.randn(SRATE*DURATION))
pygame.mixer.pre_init(SRATE, 16, 1,4096)
pygame.init()
# make it int16, scale it for 16 bit
mysound=int16(noise.copy()*2**15)
snd=pygame.sndarray.make_sound(mysound)
snd.play()
pygame.time.wait(DURATION*1000)

Jake b | 30 Apr 21:15
Picon

pyweek voting is starting

Contest starts next week. Don't forget. :)

--
Jake
Picon

Re : [pygame] raycasting engine performances (numpy array)

In fact it s not for professional stuff or whatever but i am à huuuuge fan of python and of old school techniques such as raycasting. I have already wrote a wolfenstein clone in c using le libx. Textures and some.effects were implemented without any lag ( i can give the code if anyone is interested). So now i'd like to write à more advanced wolf/doom like using an OO approach. So i though Python of course cause it's soon sexy blablabla but maybe not adapted.
So now my question is, can i write the game logic entirely in python and write the rendering part in C with OpenGL calls?
Thank you in advance!

----- Reply message -----
De : "Weeble" <clockworksaint <at> gmail.com>
Pour : <pygame-users <at> seul.org>
Objet : [pygame] raycasting engine performances (numpy array)
Date : ven., avr. 27, 2012 08:36


On Thu, Apr 26, 2012 at 1:49 PM, Nathan Biagini <nathan.open <at> gmail.com> wrote:
> i dunno if i can do more "optimized" as a drawing line function...

I see a moderate speed-up when replacing:

   asf[x][y] = 255

with:

   asf[x,y] = 255

Which avoids doing two layers of Python indexing for each pixel.
Still, it doesn't make it fast enough to be smooth. I see a much
bigger speed-up if I replace that loop with:

   asf[x,start:end] = 255

This shifts the whole loop into C code with no Python interpreter to
slow it down. The only problem is figuring out how to express the
texturing as something numpy can do do efficiently. I think it may
well be possible, but I'm not enough of a numpy expert to know off the
top of my head.

I totally agree with Greg: only do this if it's for your own education
and amusement. Raycasting on the CPU is always going to be way slower
than a GPU can do, even if you can get the Python interpreter out of
all the slow bits (whether that's by way of numpy, PyPy or just plain
writing some C).


Gmane