Created
November 5, 2016 20:12
-
-
Save 648trindade/317d2dd084ad92fdcde5ac7f562d3685 to your computer and use it in GitHub Desktop.
PygGame Of Life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
from pygame import gfxdraw | |
pygame.init() | |
size = (800,600) | |
display = pygame.display.set_mode(size) | |
surface = [None, None] | |
surface[0] = pygame.Surface((size[0]/10, size[1]/10)) | |
surface[1] = pygame.Surface((size[0]/10, size[1]/10)) | |
active = False | |
surface[active].fill((255,255,255)) | |
drawing = True | |
def count_neighboors(x,y): | |
count = 0 | |
for i in range (x-1,x+2): | |
for j in range(y-1,y+2): | |
if (i-x or j-y) and (i in range(size[0]//10)) and (j in range(size[1]//10)): | |
count += tuple(surface[active].get_at((i,j))) == (0,0,0,255) | |
return count | |
def draw(): | |
_surface = pygame.transform.scale(surface[active],size) | |
display.blit(_surface,(0,0)) | |
pygame.display.flip() | |
pygame.time.Clock().tick(60) | |
draw() | |
while True: | |
event = pygame.event.wait() | |
if event.type is pygame.MOUSEBUTTONDOWN: | |
break | |
elif event.type is pygame.QUIT: | |
pygame.quit() | |
quit() | |
pygame.time.Clock().tick(60) | |
while True: | |
#receive input | |
print ("drawing") | |
while drawing: | |
pos = None | |
for event in pygame.event.get(): | |
if event.type is pygame.MOUSEBUTTONUP: | |
drawing = False | |
elif event.type is pygame.MOUSEMOTION: | |
pos = event.pos | |
elif event.type is pygame.QUIT: | |
pygame.quit() | |
quit() | |
if pos is not None: | |
gfxdraw.pixel(surface[active], pos[0]//10, pos[1]//10, (0,0,0)) | |
draw() | |
#game of life | |
print("gaming") | |
gaming = True | |
while gaming: | |
for event in pygame.event.get(): | |
if event.type is pygame.MOUSEBUTTONDOWN: | |
drawing = True | |
gaming = False | |
elif event.type is pygame.QUIT: | |
pygame.quit() | |
quit() | |
surface[not active].fill((255,255,255)) | |
for i in range(size[0]//10): | |
for j in range(size[1]//10): | |
neighboors = count_neighboors(i,j) | |
alive = tuple(surface[active].get_at((i,j))) == (0,0,0,255) | |
if alive and (neighboors in (2,3)): | |
surface[not active].set_at((i,j),(0,0,0,255)) | |
elif (not alive) and (neighboors == 3): | |
surface[not active].set_at((i,j),(0,0,0,255)) | |
active = not active | |
draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment