Home > Games, Python > Python Tuesdays – The kickoff!

Python Tuesdays – The kickoff!

Hello all!

Today is the first of the Python Tuesdays!  Today, I’m going to talk about a missile aiming system using python and pygame. I’ll assume that you have already installed both.

So, first, let’s get a basic codebase down for a simple game.

import pygame, time, basics

pygame.display.set_mode((640, 480))
img1, rect1 = basics.load_image("heli.png", -1, 320, 240, "missilefighter")
img2, rect2 = basics.load_image("invader.png", -1, 100, 100, "missilefighter")
end = False

def main(screen, bearing):
    while not end:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESC):
                end = True
if __name__ == "__main__":
    main(screen, -3pi/4)
    pygame.quit()

This code gets some basic images loaded using my basics library. The file hierarchy should look like this:

Containing folder
  |-basics.py
  |-missileaiming.py (main code file)
  |-data
    |-missilefighter
      |-heli.png
      |-invader.png

For heli.png and invader.png, I’ve used some files from the very useful SpriteLib.  You can use whatever you want.

Now, add some code to get the following:

import pygame, time, basics
from math import *

screen = pygame.display.set_mode((640, 480))
img1, rect1 = basics.load_image("heli.png", -1, 320, 240, "missilefighter")
img2, rect2 = basics.load_image("invader.png", -1, 100, 100, "missilefighter")

class Missile(pygame.sprite.Sprite):

    def __init__(self, pos, bearing, *groups):
        pygame.sprite.Sprite.__init__(self, groups)

        self.rect = pygame.Rect(pos[0], pos[1], 2, 2)
        self.img = pygame.Surface((2, 2))
        self.img.fill([255, 0, 0])
        self.bearing = bearing
        self.actpos = [self.rect.x + 0.0, self.rect.y + 0.0]

    def update(self):
        vx = 0.5 * cos(self.bearing)
        vy = 0.5 * sin(self.bearing)
        self.actpos[0] += vx
        self.actpos[1] += vy
        self.rect.x = int(self.actpos[0])
        self.rect.y = int(self.actpos[1])

def main(screen, bearing):
    missl = Missile([320, 240], bearing)
    end = False
    while not end:
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESC):
                end = True

        screen.fill([0,0,0])
        screen.blit(img1, rect1)
        screen.blit(img2, rect2)
        missl.update()
        screen.blit(missl.img, missl.rect)
        pygame.display.flip()

if __name__ == "__main__":
    main(screen, -3*pi/4)
    pygame.quit()

You may notice several things: the missile misses the invader (oh no!), and there is no collision detection yet.  Don’t worry; we’ll get to that later.

Notice the update() definition in the Missile class.  You can see that we perform a strange math task to get our velocities, and then apply them.

The math task performed is something like:

vx = 0.5 * cos(self.bearing)

for the X velocity, and the same with sin substituted for cos for Y velocity.

What we are doing here is a basic translation of polar coördinates to rectangular coördinates.  Learn more about that on Wikipedia and my other polar coördinates post.  You may notice that we use an angle from a different quadrant; this is because the Y axis is flipped in pygame.

For my next Python Tuesday, I’ll finish up the code for this.

See ya soon!

About these ads
  1. November 29, 2011 at 8:39 PM

    ‘fraid I last you on the first codebox. I guess I could just copy it, but how does it work?

    • November 29, 2011 at 8:43 PM

      It works using python and pygame. Learn those, and you’ll be all set ;)

  1. No trackbacks yet.

Your comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: