viernes, 9 de noviembre de 2007

Python - Juego de la Vida (Automatas Celulares)

Me intereso bastante cuando vi el efecto que producia unas simples leyes dentro de una matriz "Juego de la vida" este codigo lo encontre en un manual de python, el cual lo modifique para usar con pygame aqui se los dejo y modifiquen a su gusto xD.......(Hay partes del codigo que no se usan por que? bueno tambien estoy creando mi universo jajaja en fin ^^)

#! /usr/bin/env python
import pygame, serial, sys, MySQLdb, time
from pygame.locals import *
from random import random
from random import randint

pygame.init()
pygame.font.init()

def MostrarMundo():
for y in range(filas):
for x in range(columnas):
if Mundo[y][x]== 1:
pygame.draw.rect(screen,rojo,(x*10,y*10,10,10))
pygame.display.flip()
elif Mundo[y][x] == 2:
pygame.draw.rect(screen,verde,(x*10,y*10,10,10))
pygame.display.flip()
elif Mundo[y][x] == 3:
pygame.draw.rect(screen,azul,(x*10,y*10,10,10))
pygame.display.flip()
elif Mundo[y][x] == 4:
pygame.draw.rect(screen,blanco,(x*10,y*10,10,10))
pygame.display.flip()
elif Mundo[y][x] == 0:
pygame.draw.rect(screen,negro,(x*10,y*10,10,10))

pygame.time.wait(500)

def LaGranExplosion():
for i in range(randint(1,200)):
a = randint(0,21)
b = randint(0,21)
Mundo[a][b]=4

def Accion():

for y in range(filas):
for x in range(columnas):
n = 0
if y > 0 and x > 0 and Mundo[y-1][x-1]:
n+=1
if x > 0 and Mundo[y][x-1]:
n+=1
if y <> 0 and Mundo[y-1][x]:
n+=1
if y <>0 and Mundo[y+1][x]:
n+=1
if y>0 and x < columnas-1 and Mundo[y-1][x+1]:
n+=1
if x < columnas -1 and Mundo[y][x+1]:
n+=1
if y < filas-1 and x < columnas-1 and Mundo[y+1][x+1]:
n+=1

if Mundo[y][x] and (n==2 or n==3): #Nacimiento
Mundo[y][x]=1
elif not Mundo[y][x] and n==3: #Permanencia
Mundo[y][x]=1
else:
Mundo[y][x]=0 #Expiracion

MostrarMundo()

#---------------------------------------------------------------------
screen = pygame.display.set_mode((240, 240))
pygame.display.set_caption('JMQ')

running = 1

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0,0,0,0,0,0,0,0,0,0,0,0,0,0],\
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

LaGranExplosion()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#pygame.draw.rect(screen, Color, (x, y, x_size, y_size))
Accion()
screen.fill(negro)
pygame.display.flip()


Tengan en cuenta la identacion......... xD

1 comentario:

Jota Perez dijo...

hola chispo me gusta la idea de probar esto vamos a ver si puedo instalar pygames y largarlo a interpretar

IRC

#freenode->#usljujuy

Seguidores

Eventos n_n

Tira Ecol Nano,Bilo y Luca

Archivo del blog