Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
POKEMAN SAM
Jul 8, 2004

deimos posted:

defaultdicts

Man, and I didn't even know I was missing these!

Defaultdicts are my new favorite coding-toy-of-the-week, now.

Adbot
ADBOT LOVES YOU

POKEMAN SAM
Jul 8, 2004
So, I'm working on a game for a class, in PyGame, and I had a question about Python:

I have several objects each with a common property (HitRectangle.) I have a collision detection algorithm working fine, but I'd like to have it so it calls a different collision method based on the types of the two collided objects. Since this will be called several times per frame, I'd like it to be as fast as possible, but really I don't know where I want to start.

The only real idea I had was to create a member variable called Type or something for each object, then compare two objects' Type strings and then determine which function to call, but it seems like there should be an easier way.

POKEMAN SAM
Jul 8, 2004

deimos posted:

You can't use type(obj)?

Not sure how fast that is, and additionally I may have some derived classes that I'll want to "inherit" the collision method of their parent.

Hmm, actually, what if I make a dictionary of tuples, (type1, type2) with the value being the method to call, then to know what method to call I just call dict[(type1, type2)] for however I determine to figure out the type.

POKEMAN SAM
Jul 8, 2004

No Safe Word posted:

Really you want to use isinstance (sometimes in conjunction with type), because that handles inheritance better and is just in general more robust.

Thanks, I think I've got a reasonable prototype working.

POKEMAN SAM
Jul 8, 2004

duck monster posted:

Note thats off the top of my head. Probably typos in there

Thanks for the help, but it's not quite that applicable for my problem, because I'm dealing with collisions between two different objects (of different types.)

POKEMAN SAM
Jul 8, 2004

Alhireth-Hotep posted:

So. Thoughts? Plausible? Waste of time?

I just got done with a project in PyGame, and this sounds really, really easy. Saving the data and dumping to CSV will be super-trivial in Python, and PyGame makes throwing things on the screen easy. Sounds are pretty easy, too. I don't know how high of a resolution timer Python has, though, if you want to be super awesomely precise (as much as the processor/OS/whatever can handle) in your timing measurements.

POKEMAN SAM
Jul 8, 2004

Bonus posted:

I'm getting into pygame right now and I think it's pretty cool, although does anyone else think that the documentation is lacking? It just seems that the function descriptions aren't as clear as they could be and there's never any example of usage.
For instance, there are key constans listen in the documentation for pygame.key but it doesn't say anywhere in exactly which namespace they are. I looked everywhere through the pygame.key namespace only to see that they're defined in the main module namespace.

pygame.locals has all of the constants, I think.

POKEMAN SAM
Jul 8, 2004

LuckySevens posted:

Hey guys, I've started learning Python as my start to beginning to learn programming, hopefully I'll have the staying power to learn a thing or two. Is it alright if I post really newbie questions here?

Sure. I'd be glad to answer them if no one else does.

POKEMAN SAM
Jul 8, 2004

colonp posted:

It'd be great if someone could explain what exactly I'm supposed to do.

You'll want to call find on the string searching for the character. If it finds none (find returns -1) then it found zero, so return the counter which is initialized to zero at the beginning. If it found one, increment the counter and do another find() except start right after where it found the last one.

For example, if you have find 'a' in "aaba" you first call find on the string for a starting at position 0. Then, it finds one at position zero and returns 0 (the index of where it found it.) So, you increment your counter and call find with the starting index (third parameter) being 1 (0+1, where 0 is where it found the last instance.) This time it will return 1, so search starting at 2, which finds one at 3. It will return -1 after the last one is found, so use that -1 to signal when to return the counter to the calling procedure.

POKEMAN SAM
Jul 8, 2004
Yeah, you're essentially doing the same problem as before but a different way (recursion) and the third optional parameter means that you don't need a helper function to do it, since you implicitly start at index=0 when you don't explicitly state what index you want to begin at.

Here's the code I came up with, though untested it should work:

code:
[spoiler]
def count_letters(word, ch, start=0):
    foundindex = find(word, ch, start)
    if foundindex == -1:
        return 0
    else:
        return 1 + count_letters(word, ch, foundindex + 1)
[/spoiler]

POKEMAN SAM
Jul 8, 2004

dorkanoid posted:

py2exe

py2exe is a bitch for pygame, though, by the way (there is a like pygame2exe or something, but it's hard to configure.)

POKEMAN SAM
Jul 8, 2004

duck monster posted:

How? Every time I've used it with pygame stuff, it just sort of magically works for me. There doesn't seem to be any extra configuation required.

I was never able to get it work well/right and all I've read online is problems people have had with it. Maybe I was just retarded.

Adbot
ADBOT LOVES YOU

POKEMAN SAM
Jul 8, 2004
Edit: I'm on Windows

I'm trying to follow this: http://eli.thegreenplace.net/2011/07/03/parsing-c-in-python-with-clang/ but I'm running into a problem with loading the DLL via ctypes. I have a mingw32 compiled DLL, but whenever I call a function that ends up calling a C function in the DLL and passing in an object, I get this error:

ValueError: Procedure called with not enough arguments (4 bytes missing) or wrong calling convention

Trivial functions work, as long as they don't require passing in an object. Changing it to windll.LoadLibrary made it all explode, which makes sense because it's supposed to be a cdll, so using cdll.LoadLibrary should be right.

Any ideas? I've tried Python 2.6 and 2.7 (both 32-bit of course) to no avail...

Edit: It's actually not functions that take in non-boring typed arguments, but ones that return non-boring types that are breaking.

Edit 2: Aha, I have figured it out. Apparently the msys32 clang.dll wasn't cutting it, so I built (using VS2010) my own libclang.dll and everything works peachy keen :)

POKEMAN SAM fucked around with this message at 00:26 on Aug 25, 2011

  • Locked thread