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.
Rules:
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:
- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- 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.
Setup colors:
Here we use three colors
GREEN = (0, 100, 0)
ORENGE = (255, 102, 0)
GREY = (120, 120,120)
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.
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)
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
Drawing green cells: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))
for i in range(0,480,22):
pygame.draw.rect(screen,(ORENGE),pygame.Rect(j,i,20,20))
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: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
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
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.
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()
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type== KEYDOWN and event.key==K_ESCAPE:
pygame.quit()
sys.exit()
pygame.display.update()
No comments:
Post a Comment