Reply
Thread Tools
Posts: 1,086 | Thanked: 2,964 times | Joined on Jan 2010
#1
Pygame is a set of modules, built on Python, that you can use to build games. It's free and it's pretty easy, it's a great alternative to make apps for the N900.

This is another beginner tutorial in association with the N900 coding competition , running til July 21st. Please get involved

Part1: Building a hello world style app with a spaceship that moves to keypresses



Part2: Building an accelerometer app which makes a hammer sound when the N900 is held and moved downwards in a hammering motion



This short tutorial covers:
  • Getting started with PyGame
  • Responding to key press events
  • Using the N900's accelerometer
  • Playing sound files with PyGame
  • Badly drawn programmer art

Pre-requisites
• Python should be installed correctly on your PC and n900. If not, please read over the start of MikeC's excellent tutorial to get your environment set up. Python for the N900 is available in the 'extras' repository.
• You should have WinSCP on your PC and OpenSSH client-server on your n900 (or equivalents) for copying the files over.

Caveats
I am also a beginner, this tutorial is put out there in the hope it helps but should not be seen as well written or particularly ‘good’

Get Python and PyGame
PyGame for you development machine can be downloaded from pygame.org. The 'python-pygame' module is available to install on your N900 via the extras repository. You can also install it from xterm, using the following command:
Code:
apt-get install python-pygame
Part1 - Hello Spaceship!
It's basically Hello World, but with an added moveable space ship. I created two images for this example, star_background.jpg and starship.jpg. Feel free to use the files attached or substitute your own.



Next we build the code. The first step is to import and intialize PyGame. Open up a new file in your IDE or text editor and type in:
Code:
import pygame
pygame.init()
and set the screen size for our app, in this case 800*420
Code:
screen = pygame.display.set_mode([800,420])
Next we load in our two images, and "blit" (which means draw them at a particular position) to the screen. Ive also created a font to write the "Hello Spaceship!" message. (btw '#' means a comment)
Code:
#load the background image
starfield= pygame.image.load("star_background.jpg").convert() 
#load the spaceship image
starship = pygame.image.load("starship.jpg").convert() 
#create a new font to use for writing to the display
myFont = pygame.font.SysFont("arial", 30) 
#fill the screen with a black colour to start 
screen.fill([0,0,0]) 
#draw the background at top left corner
screen.blit(starfield, (0,0)) 
#starting x and y positions of the spaceship image
ship_x, ship_y = 350, 300 
#draw the star ship at starting x and y
screen.blit(starship, (ship_x, ship_y))
PyGame makes use of a game loop, within which we put all the commands we want to be carried out as the application runs. In this example, we will want to check if an arrow key has been pressed on the N900's keypad, and if so to update the position of the space ship image. We will then redraw the screen to show any changes. In this example, we will be using 30 fps (frames per second) which means the loop runs approx 30 times a second.

We need to create a clock object to manage the loop timing, a fps variable to assign the speed, and a boolean 'mainloop' to set to false when we want the loop to stop running (e.g. quitting the app)
Code:
mainloop =  True
fps = 30
clock = pygame.time.Clock()
And here is the main loop, showing the key press handling, font writing, and image redrawing. It also writes the app name and current fps value to the top of the window. Key presses are handled as keydown events here, for each arrow key press an event is generated and picked up within the loop to add or subtract 10 pixels to the spaceship x or y co-ordinates. K_LEFT refers to the left arrow key, similarly for the others.
Code:
while mainloop:
    tick_time = clock.tick(fps) # milliseconds since last frame
    pygame.display.set_caption("maeStarShip. FPS: %.2f" % (clock.get_fps()))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False
            elif event.key == pygame.K_LEFT:
                ship_x = ship_x-10
            elif event.key == pygame.K_RIGHT:
                ship_x = ship_x+10
            elif event.key == pygame.K_UP:
                ship_y = ship_y-10
            elif event.key == pygame.K_DOWN:
                ship_y = ship_y+10

    screen.fill([0,0,0])
    screen.blit(starfield, (0,0))
    screen.blit(starship, (ship_x, ship_y))
    screen.blit(myFont.render("Hello Spaceship!", 0, (255,255,255)), (20,20))

    pygame.display.update()
Here's the full code, copy it into your favorite Python IDE (e.g. Idle) or a text editor will work as well. Save it as maeStarShip.py. You can test it on your development machine by launching the file (double click/run in IDE).

Code:
import pygame
pygame.init()

screen = pygame.display.set_mode([800,420])
mainloop =  True
fps = 30
clock = pygame.time.Clock() 
starfield= pygame.image.load("star_background.jpg").convert()
starship = pygame.image.load("starship.jpg").convert()
myFont = pygame.font.SysFont("arial", 30)
ship_x, ship_y = 350, 300
screen.fill([0,0,0])
screen.blit(starfield, (0,0))
screen.blit(starship, (ship_x, ship_y))

while mainloop:
    tick_time = clock.tick(fps)
    pygame.display.set_caption("maeStarShip. FPS: %.2f" % (clock.get_fps()))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False
            elif event.key == pygame.K_LEFT:
                ship_x = ship_x-10
            elif event.key == pygame.K_RIGHT:
                ship_x = ship_x+10
            elif event.key == pygame.K_UP:
                ship_y = ship_y-10
            elif event.key == pygame.K_DOWN:
                ship_y = ship_y+10

    screen.fill([0,0,0])
    screen.blit(starfield, (0,0))
    screen.blit(starship, (ship_x, ship_y))
    screen.blit(myFont.render("Hello Spaceship!", 0, (255,255,255)), (20,20))

    pygame.display.update()
Use WinSCP and OpenSSH to transfer the files into your N900’s Opt directory. To run, open Xterm and cd into the opt directory and run the maeStarShip.py file.

Code:
cd /opt
python maeStarShip.py
The application will open in a new window and that's it - your first PyGame app for the n900!


Part2 - adding sound and accelerometer with maeHammer

Start off with a hammer image. You can use the one I've drawn or substitute it for one of your own. You also need a sound file of a hammer noise (or any noise), you'll find hammeronce.wav in my attached files or again feel free to substitute one of your own.


Here is the full code, a lot of it should be familiar to the previous example. Copy and paste it into your ide/text editor, and save as maeHammer.py. To test, grip the n900 at the right side (handle of the hammer image) like you're holding a hammer and do a hammer type motion. A sound should play when the left side of the n900 goes down past the horizontal orientation. Please do not hammer anything other than air with your n900!

Code:
import pygame
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode([800,420])
mainloop = True
fps = 30
clock = pygame.time.Clock()
hammerImage= pygame.image.load("hammer.jpg").convert()
screen.fill([255,255,255])
screen.blit(hammerImage, (0,0))
last_pos_ccrd = 100
hammerTime=False

def play_hammerSound(): # plays the hammer sound file
    sound = pygame.mixer.Sound('hammeronce.wav')
    sound.play()

def get_rotation(): # returns accelerometer data
   f = open("/sys/class/i2c-adapter/i2c-3/3-001d/coord", 'r' )
   coords = [int(w) for w in f.readline().split()]
   f.close()
   return coords

while mainloop:
    tick_time = clock.tick(fps) # milliseconds since last frame
    a = get_rotation()
    pygame.display.set_caption("maeHammer, a[0]: %.2f" % (a[0]))
    if (a[0]>0):
        if (hammerTime==False) and (a[0]>last_pos_ccrd+50):
            hammerTime = True
            play_hammerSound()
    else:
        hammerTime = False
    last_pos_ccrd = a[0]
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            mainloop = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                mainloop = False

    pygame.display.update()
The play_hammerSound() method, when called from the main loop, plays the 'hammeronce.wav' sound file. If your sound file is in a different folder to the maeHammer.py file you may need to add the path to the name.
Code:
def play_hammerSound(): # plays the hammer sound file
    sound = pygame.mixer.Sound('hammeronce.wav')
    sound.play()
the get_rotation() method is stolen from the accelerometer wiki here. It returns 3 values (x, y, and z) which corresponds approx to the position of the N900.
Code:
def get_rotation(): # returns accelerometer data
   f = open("/sys/class/i2c-adapter/i2c-3/3-001d/coord", 'r' )
   coords = [int(w) for w in f.readline().split()]
   f.close()
   return coords
When the N900 is horizontal, the x value is 0. Left edge down, the x value raises in + numbers up to 1000. Left edge up (and right edge down) the x value does into - numbers down to -1000. In my hammer example, the n900 is gripped like a hammer with the left edge up to begin with. As the left edge is lowered down past horizontal the x value passes '0' and that's when I trigger the sound file. The new code in the main loop checks for this passing through 0, by calling the get_rotation() method. a[0] contains the x value, a[1] and a[2] would contain the y and z values respectively, but are not used in this app.

Code:
while mainloop:
    tick_time = clock.tick(fps) # milliseconds since last frame
    a = get_rotation()
    pygame.display.set_caption("maeHammer, a[0]: %.2f" % (a[0]))
    if (a[0]>0):
        if (hammerTime==False) and (a[0]>last_pos_ccrd+50):
            hammerTime = True
            play_hammerSound()
    else:
        hammerTime = False
    last_pos_ccrd = a[0]
The main loop also contains code to check that the left edge is moving down by comparing the previous value to the new one - this stops the noise triggering when you move the left edge back up past horizontal again (ie bringing the hammer back to starting position). I add 50 to the previous number as I found the accelerometer value to be a bit jumpy, depending how steady you hold it. You may need to increase this value if you find the noise repeats at the horizontal. Ive outputted the a[o] value to the top of the window so you can see how jumpy it is for you.

Anyway, you cant run this locally as the accelerometer file wont exist on your computer im sure So use WinSCP and OpenSSH to transfer the files into your N900’s Opt directory. To run, open Xterm and cd into the opt directory and run the main.py file.

Code:
cd /opt
python maeHammer.py
The application will open in a new window...


That's it from me. Make sure you check out the much better tutorials on pygame.org - http://www.pygame.org/wiki/tutorials. There are also lots of examples and ideas there, it's a great resource
Attached Files
File Type: zip maeStarShip.zip (6.0 KB, 392 views)
File Type: zip maeHammer.zip (18.4 KB, 316 views)
__________________
Follow me on my neglected twitter @kojacker

Cybot950 - Control a robot with your N9/N950
SMSPetFeeder - Build a Bluetooth/SMS dog feeder with Qt, N950, and arduino
Nerf950 - Use your N9/N950 to fire a Nerf gun

Last edited by kojacker; 2010-06-13 at 00:23.
 

The Following 28 Users Say Thank You to kojacker For This Useful Post:
Posts: 13 | Thanked: 20 times | Joined on Jun 2010
#2
Here's some guides that I've found useful, or, potentially useful when it comes to pygame:

This guide seems to be pretty good for getting started in pygame. I keep meaning to look through it more, but haven't had a chance to.

Invent with Python also covers game design as well as learning to program in Python. It's primarily aimed at kids (supposedly), however it covers a lot. It starts out with some text-based games, but then moves on to using pygame.

This tutorial isn't too bad, and mostly works. He's got exercises to go with the lessons, with IMO is quite helpful.

Hope someone finds them useful!
 

The Following 4 Users Say Thank You to timconradinc For This Useful Post:
Posts: 44 | Thanked: 28 times | Joined on Mar 2010
#3
I rebuild the hammer app into a vuvuzela playing one

details found here:
http://talk.maemo.org/showpost.php?p...8&postcount=26

thank kojakker for you detailed posts!
 

The Following User Says Thank You to ny-hardcore For This Useful Post:
Posts: 372 | Thanked: 61 times | Joined on Jan 2012
#4
is it possible to crete a game clone of lane splitter or temple run. just simple. may be a box moving with obstacles. there are many simple games in android which have no esp graphics at all. like impossible jump and speedes 3D or falling ball. or smthng like stick games. those games do not have any esp graphics but are damn addictive. is it possible to make them?
 
jedi's Avatar
Posts: 1,411 | Thanked: 1,330 times | Joined on Jan 2010 @ Tatooine
#5
Originally Posted by Mohammed Muid View Post
is it possible to crete a game clone of lane splitter or temple run. just simple. may be a box moving with obstacles. there are many simple games in android which have no esp graphics at all. like impossible jump and speedes 3D or falling ball. or smthng like stick games. those games do not have any esp graphics but are damn addictive. is it possible to make them?
Yes - I believe that's the point of this thread? They'd all be great projects for starting to learn to code.

Or are you requesting that someone else do the work for you?
__________________
May the source be with you.
 
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 23:50.