Wednesday, January 9, 2013

Pygame: Game Of Life

Pygame:

    Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. It is built over the Simple DirectMedia Layer (SDL) library. This is based on the assumption that the most expensive functions inside games (mainly the graphics part) can be abstracted from the game logic, making it possible to use a high-level programming language, such as Python, to structure the game.
    Pygame was originally written by Pete Shinners and is released under the open source free software GNU Lesser General Public License.

Game of life:

    The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and obseving how it evolves.

Rules:
    The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.
         I find the best way to understand the Pygame library is to jump straight into an example. In the early days of Pygame, I created The "Conway's game of life". Let's take a look at the code of Conway's game of life. This tutorial will go through the code block by block. Explaining how the code works.
Import modules:
This is the code that imports all the needed modules into your program. It also checks for the availability of some of the optional pygame modules.
import pygame,sys
from pygame.locals import *
There is a special pygame module named "locals". This module contains a subset of pygame. The members of this module are commonly used constants and functions that have proven useful to put into your program's global namespace. This locals module includes functions like "Rect" to create a rectangle object, and many constants like "QUIT, HWSURFACE" that are used to interact with the rest of pygame.
Setup colors:
Here we use three colors

GREEN = (0, 100, 0)
ORENGE = (255, 102, 0)
GREY = (120, 120,120)
Initialize Everything:
Before we can do much with pygame, we need to make sure its modules are initialized. In this case we will also open a simple graphics window.

pygame.init() # initialize pygame
screen=pygame.display.set_mode((635,480)) #setup the window
pygame.display.set_caption("Conway's Game of Life")
screen.fill(GREY)
The first line to initialize pygame takes care of a bit of work for us.Next we set up the display graphics mode.Last we set the window title and fill the screen with color.
Grid Drawing:
Next, we need drawing grids in the screen

for j in range(0,640,22):
    for i in range(0,480,22):
        pygame.draw.rect(screen,(ORENGE),pygame.Rect(j,i,20,20))
Drawing green cells:
For the game we will need to alive cells
pixObj=pygame.PixelArray(screen)
pixObj[132:152,88:108]=GREEN
pixObj[154:174,88:108]=GREEN
pixObj[176:196,88:108]=GREEN
pixObj[110:130,110:130]=GREEN
pixObj[132:152,110:130]=GREEN
pixObj[154:174,110:130]=GREEN

m=44
for m in range(44, 90, 22):
    for n in range(44,90,22):
        pixObj[m:m+20,n:n+20]=GREEN
Main loop:
Nothing much here, just an infinite loop.

 while 1:
Next, checking the neighboring cells and check the rules of game.

for c in range(10,400,22):
   for r in range(10,400,22):
   count=0
   if pixObj[c,r-22]==screen.map_rgb(GREEN):
      count +=1
   if pixObj[c,r+22]==screen.map_rgb(GREEN):
      count +=1
   if pixObj[c-22,r-22]==screen.map_rgb(GREEN):
      count +=1
   if pixObj[c-22,r]==screen.map_rgb(GREEN):
      count+=1
   if pixObj[c-22,r+22]==screen.map_rgb(GREEN):
      count+=1
   if pixObj[c+22,r-22]==screen.map_rgb(GREEN):
      count+=1
   if pixObj[c+22,r]==screen.map_rgb(GREEN):
      count+=1
   if pixObj[c+22,r+22]==screen.map_rgb(GREEN):
      count+=1
   #conditions
   if pixObj[c,r]==screen.map_rgb(GREEN):
       if count<2 or count>3:
          pixObj[c-10:c+10,r-10:r+10]=(ORENGE)
       else:
          pixObj[c-10:c+10,r-10:r+10]=(GREEN)
   if pixObj[c,r]==screen.map_rgb(ORENGE):
       if count==3:
           pixObj[c-10:c+10,r-10:r+10]=(GREEN)
   pygame.display.update()#update display
Handle all input Events:
This is an extremely simple case of working the event queue.
for event in pygame.event.get():
    if event.type==QUIT:
        pygame.quit()
        sys.exit()
    elif event.type== KEYDOWN and event.key==K_ESCAPE:
        pygame.quit()
        sys.exit()
    pygame.display.update()

First we get all the available Events from pygame and loop through each of them. The first two tests see if the user has quit our game, or pressed the escape key. In these cases we just return from the main() function and the program cleanly ends.

complete code is Here


No comments:

Post a Comment