Showing posts with label tutorials. Show all posts
Showing posts with label tutorials. Show all posts

Friday, 25 June 2010

...And the post frequency meter was striking thirteen!

Yes, I know - two posts in as many days! Utter madness! The post frequency meter is indeed frazzled. But this is merely a swift news-deposit before I head off to undertake the elaborate job of implementing the new keyframing system in PyIgnition for the Alpha 4 ('controlled eruption') release. Incidentally, on that topic, I've just this morning sorted out completely how I'm going to implement the new system. It's all scribbled down on assorted scraps of paper (many of which lie within my own head), and just needs to be coded now. However, as mentioned a few sentences ago, it'll be a long and difficult process. Updates will follow when it's underway!

So, to return to the planned purpose of this present posting, I've done a bit of dabbling in wxPython today for the first time in ages and I must say I'm addicted all over again. It's simply brilliant. And, I'm pleased to announce, fully compatible with Pygame after a little bit of fiddling around. Here for your viewing is a screenshot of 'Wind.py' running on a wxPanel, completely under the command of wxPython:


The window is even resizeable, and the particle effect display scales with it. Incidentally, 'Obsidian' is a name that we were originally considering for what is now PyIgnition, and at the moment is probably going to be the name of the particle effects editor. More incidentally, the more astute amongst you may have noticed that the window looks a bit different to those seen in screenshots I've posted in the past. Why, you ask? Well, I've finally upgraded from the old XP machine (which is probably getting near its sixth birthday now), and with the new one came a new OS: Windows 7. It's rather good actually; I fully agree with those saying it's a vast improvement over Vista. But I digress.

Using pygame in wxPython applications is actually not too complicated a process, although it was incredibly difficult to find anything about it on the Internet when I looked so I'll post my findings here in the hope that the next person who tries it will find them through a Google search. There's only really one source I could find that explains it, and that's the one at http://wiki.wxpython.org/IntegratingPyGame. However, it's a bit iffy in its explanation. In case you don't notice immediately, they miss the part where they actually give the wxFrame a child to use as a pygame display - you'll need to remember to create this yourself. I just used a simple wxPanel.

The code to look at is line 10 - this makes SDL see your widget (or wxWindow, to be terminologically accurate) as the display. From there you can simply use pygame.display.init() as shown, and get a handle to the screen with self.screen = pygame.display.set_mode(). Normally in Pygame one would supply screen dimensions to that function, but here it's probably a good idea to leave them out. However I haven't tried supplying them so I don't know whether it would work or not, so feel free to experiment and see what happens. Now, although the link says you shouldn't import pygame until after setting the SDL_WINDOWID parameter, this is actually a pain because you then don't have access to pygame's functions anywhere else. I actually found that it works perfectly well if you do import pygame at the top of the file, so there's probably nothing wrong with doing so any more. That said, I'm just going with what seems to work so feel free to correct me if I'm wrong and there are hidden issues with doing so.

With that done, you're free to do things like self.screen.fill((0, 0, 0)) and pygame.draw.aaline(screen, [blah...]) to draw to the pygame surface, so all is well from there on. But is it? As a matter of fact, you'll face problems when you try to exit - neither pygame nor wxPython wants to close first, and you'll get errors either way. However, after a bit of messing around I've managed to find a solution to this. Simply bind the following function to WX_EVT_CLOSE:

____def Kill(self, event):
________self.Unbind(event = wx.EVT_PAINT, handler = [your drawing function])
________pygame.quit()
________self.Destroy()

See, the problem that arose in making Pygame quit first was that wxPython would for some reason try to draw again before it exited. This naturally causes problems when your drawing function contains Pygame draw functions, as they will be operating on a surface which has been disabled on Pygame's exit. Unbinding the draw function avoids this issue, and lets pygame exit first with wxPython smoothly following suit afterwards.

And that's pretty much it. So far I haven't found any more issues with the two working together, but I'll update if I do. I hope this proved somewhat helpful. So, back to keyframes!
Goodbye!

Monday, 10 December 2007

The Beginner's Guide to Pygame, part one

It's been a week now since I first began learning PyGame, and in that time I've gone through several tutorials. Some have been great; others less so. As there are so many bobbing about on the interweb I thought I'd write a guide to which ones are best. Essentially a guide to beginning with PyGame really.
First off, the main source I've been using has been the PyGame documentation. This has links to several tutorials and is a great place to look if you're stuck. Next, you should check this tutorial for a good introduction that launches straight into a working example to get a feel for what writing PyGame programs is like.
At this point, you should get acquainted with the blit function. "How do I move an image?" provides an excellent introduction, starting by showing what the blit function does using basic Python and then moving on to show you how that's applied in PyGame. If this "blitting" is confusing you then that tutorial will almost definitely clear things up a bit.
Now that you have a decent understanding of how drawing things to the screen works, you should get more practice by using PyGame's built-in draw methods to make some funky line patterns, as taught by this tutorial. It also goes into colouring and animation as well, which could help later on.
Now, obviously a game's not a game if you can't control it in any way, so you have to have some way of getting events from the user. First, the mouse. This one also takes some of what you learned in the last tutorial by challenging you to make it draw a '+' on the screen where you click it. Then, once you've mastered that, this tutorial helps elaborate on what you've learned about events.
At the heart of every 2D game today is a system of sprites; that is, objects with images, properties and methods that you can use as characters, platforms, clouds or even blades of grass if you like. By following this article you too can learn to use them effectively in your games. And if you want to use collision detection as well, you'll want to look at this page.
Finally, you might be wondering how to bring all this knowledge together to make a full game. Well, this epic tutorial can help you there. It starts with a bit of revision of all the things you've learned about, then goes into taking you through the creation of a Pong remake. There's even a short maths lesson in there.
That's about as far as I've gotten so far; I will continue this guide later on. Either way, having done all the tutorials above you should have a basic knowledge and understanding of PyGame and should probably be able to start work on some simple games. I hope this article has been helpful in some way to those just starting with PyGame; good luck and happy coding.