Blogger Widgets

Search This Blog

Thursday, 10 January 2013

Some Intresting Implimentations using Pygame

                      Pygame is python module designed to writing games. Pygame was written by Pete Shinners and released under open source.It provides excellent GUI so that we can design the games easily rather than text based programmings.

A quick introduction about pygame is available here

Some Interesting Implementations using pygame is explained below
  •         Koch snowflake
  •         Conway's game of life
  •         Sierpinski triangle

Koch snowflake

    Is it possible to think a shape whose area is finite, but the perimeter is infinite?  Yes  ,Koch snow flake is an interesting mathematical curve whose area is converging, But the perimeter is infinite.It is an example of non euclidean geometry. Another property is that the koch curve is continues every where,but not differentiable  any where.

I construct this koch curve using pygame in recursive  fasion. Here I cunstuct a order-1 fractal first, then call that function repeatedly.

My recursive function is 

def koch(screen,order,a):
        if order == 0:
           for angles in [60,-120,60,0]:
           for angles  in [60,-120,60,0]:
               koch(screen, order - 1,a)  

My koch curve is
Koch Snowflake

The complete code is posted in my github


Sierpinski triangle

  Sierpinski triangle is basic examples of self-similar sets, i.e. it is a mathematically generated pattern that can be reproducible at any magnification or reduction.

Its construction starts from an single triangle. And extend as follows

 My algorithm

  1. Create an equilateral triangle. And store its points in save its points in two dimentional list like this  a = [[(300,190), (450,450), (150,450)]]
  2. Calculate the middle points of each side and make the new list as follows. .   a1 = a[i][0]
        a2 = a[i][1]
        a3 = a[i][2]
        md1 = midpoint(a1, a2)
        md2 = midpoint(a2, a3)
        md3 = midpoint(a3, a1)
  3.  Now we get a new two dimensional list and make this in a loop where take the varies the value of i from 0 to 3^6.
  4. Draw Triangles                                                                             .          .     for element in a:
                elem = element

Sierpinski Triangle
 My complete code is posted in my github

Conway's Game of Life

          Conway's game of life cellular automation which is invented by British mathematician John Horton Conway. It is a zero player game and it depends anly on the initials states.

         This game can be easily implemented using pygame module of python.The next states is determined by the rules that are described below

  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.   

Conway's Game of life (From Wikipedia)

My Algorithm is
  1. Create a widow and divide it into squares
  2. Create a two dimensional matrix and initialize it to zero.                        matrix = [[0 for i in xrange(20)] for i in xrange(20)]
  3. Let the element '1' of matrix indicate live cell and '0' indicates dead. Initialize some live cells
  4. Then check the neighboring cells by the rules.                                          def nextstage(m):
        for i in range(1,19):
             for j in range(1,19):
                  count = m[i-1][j-1]+m[i][j-1]+m[i+1][j-1]+m[i+1][j]+m[i+1][j+1]+m[i][j+1]+m[i-1][j+1]+m[i-1][j]
                  if m[i][j] == 1 and count <= 2:
                          m[i][j] = 0
                  elif m[i][j] == 1 and(count == 2 or count == 3):
                          m[i][j] = 1
                  elif m[i][j] == 1 and count > 3:
                          m[i][j] = 0
                  elif m[i][j] == 0 and count == 3:
                          m[i][j] = 1  
  5. Then update and repeat the steps

 my complete code is posted in my github

No comments:

Post a Comment

Blogger Widgets