stupidpythonprojects/02_gameOfLife/Main.py

149 lines
3.8 KiB
Python
Raw Normal View History

2016-09-12 15:19:48 +02:00
#!/usr/bin/python
import pygame
import random
from OpenGL.GL import *
2017-05-21 11:11:24 +02:00
from OpenGL.GLU import *
2016-09-12 15:19:48 +02:00
from pygame.locals import *
2017-05-21 11:11:24 +02:00
video_flags = OPENGL | HWSURFACE | DOUBLEBUF | FULLSCREEN
2016-09-12 15:19:48 +02:00
pygame.init()
2017-05-21 11:11:24 +02:00
dinfo = pygame.display.Info();
2016-09-12 15:19:48 +02:00
2017-05-21 11:11:24 +02:00
livingSpaceWidth = 48
livingSpaceHeight = 27
2016-09-12 15:19:48 +02:00
2017-05-21 11:11:24 +02:00
creatureW = dinfo.current_w/(livingSpaceWidth)
creatureH = dinfo.current_h/(livingSpaceHeight)
2016-09-12 15:19:48 +02:00
2017-05-21 11:11:24 +02:00
FPS = 40
2016-09-12 15:19:48 +02:00
livingSpace = []
2017-05-07 18:46:44 +02:00
def resize(shape):
width, height = shape
2016-09-12 15:19:48 +02:00
if height == 0:
height = 1
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, livingSpaceWidth * creatureW, livingSpaceHeight * creatureH, 0.0, -6.0, 0.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def init():
glClearColor(0.0, 0.0, 0.0, 0.0)
def initLivingSpace():
for x in range(livingSpaceWidth):
livingSpace.append([])
for y in range(livingSpaceHeight):
if random.randint(0,1) ==1:
livingSpace[x].append(1000)
else:
livingSpace[x].append(0)
def isAlive(x,y):
return livingSpace[x][y] == 1000
def draw():
2017-05-21 11:11:24 +02:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
2016-09-12 15:19:48 +02:00
glLoadIdentity()
glTranslatef(0.0,0.0,3.0)
glBegin(GL_QUADS)
2017-05-21 11:11:24 +02:00
for column in range(livingSpaceWidth):
for row in range(livingSpaceHeight):
if livingSpace[column][row] != 0:
health = float(float(livingSpace[column][row])/1000.0)
2016-09-12 15:19:48 +02:00
2017-05-21 11:11:24 +02:00
glColor4f(health*(float(column)/float(livingSpaceWidth)),
health*(float(livingSpaceWidth-column)/float(livingSpaceWidth)),
health*(float(row)/float(livingSpaceHeight)),1.0)
x = column * creatureW
y = row * creatureH
2016-09-12 15:19:48 +02:00
2017-05-21 11:11:24 +02:00
glVertex3f(x,y,0.0)
glVertex3f(x + creatureW-1.0,y,0.0)
glVertex3f(x+creatureW-1,y+creatureH-1,0.0)
glVertex3f(x,y+creatureH-1,0.0)
2016-09-12 15:19:48 +02:00
glEnd()
def getNeighborCount(x,y):
count = 0
xpn = (x + 1) % livingSpaceWidth
ypn = (y + 1) % livingSpaceHeight
# nach unten hin ist keine ausnahmebehandlung noetig,
# da python bei negativen indizes automatisch die liste von hinten
# durchlaeuft
# Vorsicht: boolscher Wert wird hier als Int genutzt!
count += isAlive(x,ypn)
count += isAlive(xpn,ypn)
count += isAlive(xpn,y)
count += isAlive(xpn,y-1)
count += isAlive(x,y-1)
count += isAlive(x-1,y-1)
count += isAlive(x-1,y)
count += isAlive(x-1,ypn)
return count
def calculateNextGeneration():
neighborCount = []
for column in range(livingSpaceWidth):
neighborCount.append([])
for row in range(livingSpaceHeight):
neighborCount[column].append(getNeighborCount(column,row))
for column in range(livingSpaceWidth):
for row in range(livingSpaceHeight):
if 2 <= neighborCount[column][row] <= 3:
if neighborCount[column][row] == 3:
livingSpace[column][row] = 1000
if not isAlive(column,row):
livingSpace[column][row] = float(livingSpace[column][row])/1.2
else:
livingSpace[column][row] = float(livingSpace[column][row])/1.2
if livingSpace[column][row] < 20:
livingSpace[column][row] = 0
def main():
2017-05-21 11:11:24 +02:00
pygame.display.set_mode((dinfo.current_w,dinfo.current_h),video_flags)
2016-09-12 15:19:48 +02:00
initLivingSpace()
2017-05-21 11:11:24 +02:00
resize((dinfo.current_w,dinfo.current_h))
2016-09-12 15:19:48 +02:00
init()
clock = pygame.time.Clock()
frames = 0
#Hauptschleife:
while True:
ticktime = clock.tick(FPS)
#print ticktime
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
2017-05-15 09:21:56 +02:00
2017-05-21 11:11:24 +02:00
draw()
2017-05-15 09:21:56 +02:00
2016-09-12 15:19:48 +02:00
pygame.display.flip()
2017-05-21 11:11:24 +02:00
calculateNextGeneration()
2016-09-12 15:19:48 +02:00
if __name__ == '__main__':
main()