Tuesday, 20 October 2015

Reflections On IronPython : Still In Production

A recent reddit thread on IronPython brought back some memories so I thought I would record them in a quick blog post.

I really liked Python when I discovered it 10+ years ago and was delighted when I first heard Microsoft had hired talent to bring Python to dotNet. At the time, I was stuck in VB6 (really) and had recently discovered traditional Python. I joined the mailing list, tried out early builds, reported bugs to Codeplex and the team at Microsoft were a great bunch to interactive with. Microsoft are somewhat Open Source today - at the time this was very unusual. I ended up writing a few GUI programs, even an IronPython editor for some time.

Then Microsoft went through changes, IronPython was dropped along with other interesting products like Mesh. The Open Source version is still around but the mailing list is very quiet. I went back to regular Python and messed around with PyGame. I still use IronPython in production (handy for configuration to have .Net scripting with zero-install for installers) and can't help think Microsoft missed a trick here. It is a much more friendly language than Powershell and theres not really a good .Net scripting language even years later. All is not entirely lost though, they still produce Python Tools for Visual Studio and it is very good.

So IronPython for me was interesting, fun and useful. It was great being involved early on in the development, which probably why I got so caught up diving into the relatively new Dart language from Google.

Wednesday, 14 October 2015

New PyGame Windows Installers

Re-awakening this blog with a bit of PyGame news worth sharing...

It's no secret that PyGame is my favourite library for Python and it is great to see x86 (32-bit) Windows installers being made available. These are much needed and helps get machines set up for testing if not full development. Most of my Python development has been Linux based but the significance of Windows as a platform is hard to avoid :-)

Monday, 7 April 2014

Capturing Code Snippets - Part One

Long time no blog!

I've been spending more time with Dart so something had to get out of the way. However I have been revising my Python in time for the PyWeek game jam next month. I thought I would break radio silence on the blog and record a few snippets so 1. I can find them 2. On the off chance they are a handy copy paste for someone else especially beginners! Some of this is from my abandoned (due to illness) previous PyWeek so it never got off my laptop. Care to blog anything yourself? :-)

This first post will cover MISC. Some runnable GFX stuff to come in a follow up post.

Two very simple snippets mainly for moving through colour indexes for graphics primitives but could easily be used for position e.g. a bad guy or obstacle going back and forth.

def keepWithinRange(i,l,u):
    if i<l:
    elif i>u:
    return i

def loopWithinRange(i,l,u):
    if i<l:
    elif i>u:
    return i
Maybe better to rewrite those with Min/Max at some point!

Misc code from various PyGame experiments and Pyweeks!

import random
import pygame
from pygame.locals import *

def CreateBackground(screen):
    bg = pygame.Surface(screen.get_size())
    bg = bg.convert()
    return bg

def DrawText(bg, x, y, text, size=24, color=(255, 255, 255)):
    inst1_font = pygame.font.Font(None, size)
    inst1_surf = inst1_font.render(text, 1, color)
    bg.blit(inst1_surf, [x, y])

def RND(num):
    return random.randint(1,num)
def randomPlusMinus1():
    return random.randint(0,2) -1
def returnTrue():
    return True

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!