The Game of Life is a cellular automation concept by the mathematician John Horton Conway. The system is simple and interesting. It is not like a conventional game, that is there is no players, and no winnings or losing. The game is a zero- player game, meaning that its evolution is determined by its initial state, requiring no further input. Once the "pieces" are placed in the starting position, the rules determine everything that happens later. In this game it is impossible to look at the starting pattern of the game. The pattern randomly changing as per the rules. the only way to find what will happen in the future is the follow the rules of the game
Rules of the Game of Life
Game of Life is played on an infinite two-dimensional grid of square cells like a chess board. Each of this cell is in one of two possible state live or dead. A Live cell is shone by a square filled with black colour, a Dead cell is shone by a square filled with white colour. every cell interact with eight niborous cells, which are the cells that are horizontal, vertical and diagonally adjacent.
The rules determines the next state of each cell according to the current state of the cell and number neighboring live cells. the rules are given below
1) A dead cell with exactly three live neighbors become a live cell(Birth)
2) A live cell with two or three live neighbors stays alive(survival)
3) Any live cell with fewer than two live neighbors dies, as if caused by under population
4) Any live cell with more than three live neighbors dies,as if by overcrowding
The initial pattern constitutes the seed of the system. The first generation iscreated 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 purefunction of the preceding one). The rules continue to be applied repeatedly to create further generations.
To develop a pygame program for life is not a hard task. its become more easy by follow some procedures. First we make sure that the system have pygame, if it is not the first step is to install pygame. It is simple task, just type the command " sudo apt-get install python.pygame" on terminal, and follow the installation procedure. After pygame installation open a file ands tarts to write a program.
Import and Initialise
First we must import the pygame package. Most game will import all of pygane like this.
import pygame
from pygame.locals import *
Before you can do much with pygame, you will need to initialize it. The
most common
way to do this is just make one call.
- pygame.init()
Initialise display
when you use a display in pygame program. first we need to initialise this display. the initialisation means to set the size and background colour of display. you can also set some name to display these instructions are in the part of display initializing section.
DIS = pygame.display.set_mode((440, 440))
pygame.display.set_caption('game of life')
pygame.display.set_caption('game of life')
The instructions mentioned above is used initialize name and size of the display. the first line set size of the display,(440,440) is the width and hight of the display respectively. Second line used to set the name of the display, in that instruction
the text enclosed in brazes is used as name.
before fill the background with colore you need to set the colour values. There are three primary colors of light: red, green and blue. By combining different amounts of these three colors you can form any other color. In Pygame, we represent colors with tuples of three integers. The first value in the tuple is how much red is in the color. An integer value of 0 means there is no red in this color, and a value of 255 means there is the maximum amount of red in the color. The second value is for green and the third value is for blue. These tuples of three integers used to represent a color are often called RGB values.
The above instruction used to set the colour , it will set the background as white colour . instead of (255,255,255) use (0,0,0) to set display background to black.
drawing on display
pygame.draw is the basic instruction used to draw something on the display. we can draw lines, rectangle circle, polygon etc using this instruction.
- pygame.draw.line(surface, color, start_point, end_point, width) – This function draws line between the start_point and end_point parameters.
- pygame.draw.rect(surface, color, rectangle_tuple, width) – This function draws a rectangle. The rectangle_tuple is either a tuple of four integers (for the XY coordinates of the top left corner, and the width and height) or a pygame.Rect object can be passed instead. If the rectangle_tuple has the same size for the width and height, a square will be drawn.
- pygame.draw.circle(surface, color, center_point, radius, width) – This function draws a circle. The center of the circle is at the center_point parameter. The integer passed for the radius parameter sets the size of the circle. The radius of a circle is the distance from the center to the edge. (The radius of a circle is always half of the diameter.) Passing 20 for the radius parameter will draw a circle that has a radius of 20 pixels.
Unfortunately, there isn’t a single function you can call that will set a single pixel to a color(unless you call pygame.draw.line() with the same start and end point). Instead, you should create a pygame.PixelArray object (we’ll call them PixelArray objects for short) of a Surface object and then set individual pixels.The PixelArray object that is returned from pygame.PixelArray() can have individual pixels set by accessing them with two indexes. For example, line 28’s pixObj[480][380] = BLACK will set the pixel at X coordinate 480 and Y coordinate 380 to be black (remember that the BLACK variable stores the color tuple (0, 0, 0)).
for finding the live and dead cell you can check the color of pixel. if the colour is black it is live cell, if the colour is white the cell is dead cell. To check the colour of a pixel a specific instruction used in python that is
if pixObj[x,y]==Display.map_rgb(black):
This instruction check the pixel of index(x,y) is black or not. this will lead yo find how many live cells situate in a display
Game loop
Game loop is the main loop of the program. it done all the main functions of the program. Always it is a infinite while loop. The only way the program execution will ever exit the loop is if a break statement is executed (which moves execution to the first line after the loop) or sys.exit() (which terminates the program). If a loop like this was inside a function, a return statement will also move execution out of the loop (as well as the function too).
A game loop (also called a main loop) is a loop where the code does three things:
2. Updates the game state.
3. Draws the game state to the screen.
Since the game state is usually updated in response to events (such as mouse clicks or keyboard presses) or the passage of time, the game loop is constantly checking and re-checking many times a second for any new events that have happened. Inside the main loop is code that looks at which events have been created (with Pygame, this is done by calling the pygame.event.get() function). The main loop also has code that updates the game state based on which events have been created. This is usually called event handling.
Draw image on the pygame screen using function "python.draw" and using variable pixObj. all these are mentioned above. after youare done calling the drawing functions to make the display surface object look the way you want, you must call pygame.display.update () to make the display surface actually appear on the user's monitor.The one thing that you must remember is that pygame.display.update() will only make the display Surface (that is, the Surface object that was returned from the call to pygame.display.set_mode()) appear on the screen.
PROGRAM
#importing pygame
import pygame, sys
from pygame.locals import *
#initialise display
pygame.init()
DIS = pygame.display.set_mode((440, 440))
pygame.display.set_caption('game of life')
clock=pygame.time.Clock()
W=(255,255,255)
B=(0,0,0)
G=(190,180,170)
DIS.fill(W)
#drawing drid
x=20
while x<440:
pygame.draw.line(DIS,G,(x,0),(x,440),2)
x+=22
y=20
while y<440:
pygame.draw.line(DIS,G,(0,y),(440,y),2)
y+=22
pygame.display.update()
#drawing initial black cell
# Block(16 cell)
pixObj=pygame.PixelArray(DIS)
p=176
while p<250:
q=176
while q<250:
pixObj[p:p+20,q:q+20]=(0,0,0)
q=q+22
p=p+22
# Toad (period 2)
pixObj[132:152,88:108]=(0,0,0)
pixObj[154:174,88:108]=(0,0,0)
pixObj[176:196,88:108]=(0,0,0)
pixObj[110:130,110:130]=(0,0,0)
pixObj[154:174,110:130]=(0,0,0)
pixObj[132:152,110:130]=(0,0,0)
# Block (4 cell)
pixObj[330:350,330:350]=(0,0,0)
pixObj[352:372,330:350]=(0,0,0)
pixObj[330:350,352:372]=(0,0,0)
pixObj[352:372,352:372]=(0,0,0)
# Blinker Block (period 2)
pixObj[154:174,308:328]=(0,0,0)
pixObj[176:196,308:328]=(0,0,0)
pixObj[198:218,308:328]=(0,0,0)
pygame.display.update()
clock.tick(1)
#check neighbours cells of black cell
def black(m,n):
c1=0
k=m+25
l=n+25
pixObj=pygame.PixelArray(DIS)
x=m-20
while x<k:
y=n-20
while y<l:
if pixObj[x,y]==DIS.map_rgb(B):
c1+=1
y+=22
x+=22
#Applying conditions
if c1<3 or c1>4:
return 1
return 0
#check neighbours cells of white cell
def white(m,n):
c1=0
k=m+25
l=n+25
pixObj=pygame.PixelArray(DIS)
x=m-20
while x<k:
y=n-20
while y<l:
if pixObj[x,y]==DIS.map_rgb(0,0,0):
c1+=1
y+=22
x+=22
#Applying conditions
if c1==3:
return 1
return 0
# main loop
while True:
#checking colour of each block
l1 =[]
l2 =[]
m=22
while m<415:
n=22
while n<415:
if pixObj[m,n]==DIS.map_rgb(0,0,0):
#function to check neighbours cells of black cell
if black(m,n)==1:
a=[m,n]
l1=l1+[a]
if pixObj[m,n]==DIS.map_rgb(255,255,255):
#function to check neighbours cells of white cell
if white(m,n)==1:
b=[m,n]
l2=l2+[b]
n+=22
m+=22
#update cell
i=0
j=0
d=len(l1)
e=len(l2)
while i<d:
pixObj=pygame.PixelArray(DIS)
m=l1[i][0]
n=l1[i][1]
pixObj[m:m+20,n:n+20]=(255,255,255)
i+=1
while j<e:
m=l2[j][0]
n=l2[j][1]
pixObj[m:m+20,n:n+20]=(0,0,0)
j+=1
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
clock.tick(5)
pygame.display.update()
No comments:
Post a Comment