|
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.
|
# ¿ Nov 5, 2007 16:42 |
|
|
# ¿ May 4, 2024 13:58 |
|
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.
|
# ¿ Nov 6, 2007 18:06 |
|
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.
|
# ¿ Nov 6, 2007 18:12 |
|
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.
|
# ¿ Nov 6, 2007 20:46 |
|
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.)
|
# ¿ Nov 8, 2007 07:38 |
|
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.
|
# ¿ Nov 20, 2007 18:53 |
|
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. pygame.locals has all of the constants, I think.
|
# ¿ Nov 30, 2007 06:13 |
|
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.
|
# ¿ Jan 16, 2008 10:13 |
|
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.
|
# ¿ Jan 16, 2008 10:17 |
|
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:
|
# ¿ Jan 17, 2008 09:24 |
|
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.)
|
# ¿ Jan 25, 2008 08:56 |
|
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.
|
# ¿ Jan 30, 2008 20:48 |
|
|
# ¿ May 4, 2024 13:58 |
|
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 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 |
# ¿ Aug 24, 2011 21:05 |