Sunday, 23 December 2012

Bouncy Text and Christmas Trees

First a quick video of the demo!

Code is available here.

The code runs much smoother than on YouTube :-) Hopefully each post in future will come with a video. The defaults are for the demo to run at 320 by 240 but you can adjust these values via the WinWidth and WinHeight Variables.

So how do we make the text bounce like this? Well, do you remember the Sin wave from trigonometry? Watch the 'M' in the video climb up to the top, back to the middle and then the bottom of the window and back up. We use a little bit of math to store the coordinates of this path in a list and each time we draw the window we move onto the next set of coordinates thus moving the text along the path. When it's gone through the list it goes back to the start so our animation loops.

The background Christmas trees dimensions are worked out and stored in treewidth and treeheight. We then have a couple of loops so we have 4 columns of trees over 3 rows. To liven things up the c3B data item adjust the shade of green. Can you figure out how to add more (smaller) trees to the scene?

The rest of the program covers the usual plumbing. An update function moves the text to the next set of coordinates in the list and it is called on a schedule. There is the on_draw handler for the window that actually takes care of the drawing.

One last point to mention is that we want the text to appear in front of the trees. Pyglet has the concept of groups. Have a look at the objects foreground and background. By setting up our objects in the right group we can ensure Pyglet draws them in the desired order.

Hope to return with something 3D next time around. Happy Python/Pyglet-ing!!

Thursday, 20 December 2012

Simple Starfield in PyGlet

How about more stars?

Whilst stuck in bed unwell, I stumbled upon a fun video tutorial on how to write an asteroid game in pyglet. Pyglet is available for Python 2 and 3 (in alpha form) and provides a pure Python wrapper to OpenGL. Whilst not been as extensive a library as say PyGame, it does offer some interesting capabilities such as sprite rotation and scaling plus a route into the OpenGL world. The example code was very clean and readable. I do miss the lack of built in primitives but you can get round that.

I thought it would be fun to revisit the star field program and convert it to pyglet. At first I was disappointed with the speed. Cutting down the number of stars worked but what made a real difference was drawing each layer as a single batch/OpenGL command. That put things back at warp speed with the regular numbers!

I highly recommend the asteroid tutorial video. My Starfield demo code is available here.

Thursday, 18 October 2012

PyWeek 15 PostMortem

Now that some time has passed, I thought it would be good to look back at PyWeek 15 to review how it went. First a screenshot!

I quite liked the look of this game with the bright colours and use of shades. I liked the generate landscape. Disappointed that there is not much to do except zap without a goal. Some systems seem to be unable to cope with the game and get next to no keyboard response making it unplayable :-( Definitely felt unfinished but I had a horrible cold that week so I simply ran out of energy some nights.

So lessons learned...

  • It was fun to do new stuff!
  • Even a simple sound effect can add to a game!
  • Test on a range of systems regularly - watch that CPU usage.
  • Allow time for review and correction.
  • Have a clear idea - this one suffered from 'just keeping busy'.
  • Players like smooth movement.
  • Players like to be in the center of the screen.
  • A few warm up projects certainly help.

Here's to next time!

Wednesday, 29 August 2012

Make Cool Game Menus with SGC

SGC (Simple Game Code) is a new project that describes itself as 'A GUI toolkit for PyGame, to ease game development for Pygame developers'. The idea being that it takes care of menus, checkboxes, inputs etc so you can get on with the game itself. I have been following this project on the PyGame mailing list and had the idea to create a skeleton 'game' to try out the menus. They look great and even have nice transitions! So we have a simple functional menu with an options screen with one option. Start the game with the option on/off for a different number of bad guys (boxes in this case). Press q to quit the game screen. Will definitely be using this for PyWeek!
Code is available from the DaftPython Google Code Project. The menu is loaded from the dmenu file so you need that as well as the .py python file.


Monday, 20 August 2012

Returning To The Stars

My previous post with a simple starfield has proved to be one of the most popular posts on this blog so far. I thought it was high time I gave the 'solution' to the challenge of a 'layered' version.

Having several starfields on top of each other running at different speeds in order to give the idea of depth. This required no changes to the starfield code (though I did tweak it not to use tuples anymore - more to avoid confusion). A screenshot doesn't do it justice - run the demo to see the movement!



All examples from this blog will appear on the new DaftPython Google Code site. The latest star code is here.

Friday, 10 August 2012

Screenshot Viewer For Minecraft - Now Released

The screenshot viewer is now available! Only tested on Windows for now so Linux/Mac users (who make a good proportion of visitors to this site) be warned. I have created a Google code project for all the code on this blog at http://code.google.com/p/daftpython/ 

You can get the latest Screenshot viewer here. It is a single .PY file tested on Python 2.6.6 and wxPython 2.8. We have been trying out a CXFreeze version with a Windows Installer and it is looking good. I'd like to look at maintaining aspect ratio for the thumbnails and display.

If you run it on a non-Windows platform I would appreciate a screenshot. Any other thoughts, please leave a comment!


Tuesday, 7 August 2012

Minecraft Screenshot Viewer in wxPython - A Preview

There's a lot of Minecraft going on in this house! The boys all love it and come up with some creative designs for houses, statues, machines and roller-coasters. Minecraft has a built in feature for taking screenshots 'F2' and I encourage them to do this if they have made something. Eventually we will make a slideshow video of them.

But there is no easy way to view the screenshots in the game and it is a bit 'hidden' under .minecraft so what a great excuse for a wxPython program. Not quite ready for the prime-time (needs Linux tested at least) but my eldest was impressed by the demo of it and is keen to get it installed on the main family PC.

Here's a screenshot until I get some SVN sorted out:


Ideas for features welcome - leave a comment!