Search This Blog

Tuesday, July 12, 2011

Learn Pygame the hard way

Pygame is a python library for game development with openGL capabilities.

Here you can find the basic concept for game development in python.
Lets start the basic.
First you need to get pygame.You can download it from pygame.org.
Then download a book called beginning-game-development-with-python-and-pygame-from-novice-to-professional
This book is really great.
The link to download this book is
http://www.scribd.com/doc/59912539/Beginning-Game-Development-With-Python-and-Pygame-From-Novice-to-Professional-9781590598726-29808

To start pygame you must have python basics.
The best book to learn python is core python programming.
The link to download this book link is


The solution of the programs given at the end of the chapter can be found here.

Ok lets begin python gaming..............

Basic Program for Pygame

#!/usr/bin/env python
#program to demonstrate the basic image and mouse handling
background_image_filename='jython.jpg'
mouse_image_filename='circle.png'

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption("hello world!")

background = pygame.image.load(background_image_filename).convert()
mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

while True:

for event in pygame.event.get():
if event.type==QUIT:
exit()

screen.blit(background,(0,0))

x,y = pygame.mouse.get_pos()
x-=mouse_cursor.get_width()/2
y-=mouse_cursor.get_height()/2
screen.blit(mouse_cursor,(x,y))

pygame.display.update()

Second basic program for event mouse handling with MATRIX like display


#! /usr/bin/env python

import pygame
from pygame.locals import*
from sys import exit

pygame.init()
SCREEN_SIZE =(800,600)
screen=pygame.display.set_mode(SCREEN_SIZE,0,32)

font = pygame.font.SysFont("arial",16);
font_height = font.get_linesize()
event_text=[]

while True:

event=pygame.event.wait()
event_text.append(str(event))
event_text=event_text[-SCREEN_SIZE[1]/font_height:]

if event.type == QUIT:
exit()

screen.fill((0,0,0))

y=SCREEN_SIZE[1]-font_height
for text in reversed(event_text):
screen.blit(font.render(text,True,(0,255,0)),(0,y))
y-=font_height

pygame.display.update()





1 comment: