Einzelnen Beitrag anzeigen
Alt 15.07.10, 19:26   #15 (permalink)
badagent
 
Registriert seit: 13.07.10
badagent Leistung: Facit NTK
Likes: 0
Post

Hallo,

das ist ja mal wirklich ne super Idee für ne Programmieraufgabe :-)

Hab da mal versucht nen anderen Ansatz zu wählen, indem ich alle aktiven Zellen
in einer Liste verwalte und entsprechend immer nur die aktiven Zellen und deren Nachbarn
abklappere. Leider wird das ab einer gewissen Anzahl von Zellen aber zu langsam, so dass ich im Moment auch schon wieder an anderen Lösungen bastel.

Dafür hab ich aber immerhin ne einfach Zoom Funktion reingebastelt...

Beim Start kann man einfach die Zellen per Maus setzen und dann das Spiel mit der Leertaste starten/stoppen

Ach ja, Code in Python + Pygame (und mit Sicherheit verbesserungsbedürftig )

Code:
#!/usr/bin/python

#notwendigen Kram importieren

import pygame
from sys import exit

#einige Variablen vordefinieren
#die seltsamen Werte bei width & height liegen daran dass das für mein Netbook
#die optimale Auflösung ist ;-)
width=800
height=400
cellColor = [0,0,255]
bgcolor = [255,255,255]
#Starte mit einem Zoom Faktor (entspricht Breite und Höhe einer Zelle in Pixel) von 10
zoomFactor = 10
cycle = 0
#Das Spiel startet erst nach Drücken der Leertaste!
running = False
#die Liste, welche später zur Verwaltung der aktiven Zellen genutzt wird
cellList = []


def initGame(width=800, height=400):
    size = width, height
    global screen
    global cellList
    screen = pygame.display.set_mode(size)
    screen.convert()

def toggleCell(pos):
    global cellList
    if checkField(pos):
        cellList.remove(pos)
    else:
        cellList.append(pos)

def checkField(pos):
    try:
        cellList.index(pos)
    except ValueError:
        return 0
    return 1     

def getNeighbors(pos):
    posx, posy = pos
    pattern = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
    pattern = [[(x+posx),(y+posy)] for x,y in pattern]
    return pattern

def checkNeighbors(pos):
    neighborhood = getNeighbors(pos)
    tmp = neighborhood[:]
    counter = 0
    for npos in tmp:
        if checkField(npos):
            counter = counter + 1
            neighborhood.remove(npos)
    return counter, neighborhood

def checkNeighborsEmpty(pos):
    neighborhood = getNeighbors(pos)
    counter = 0
    for npos in neighborhood:
        counter = counter + checkField(npos)
    return counter

def checkRules():
    global cycle
    cycle=cycle+1
    global cellList
    print len(cellList)
    tmpCellList = cellList[:]

    for cell in cellList:
        neighbors, tmpEmpty = checkNeighbors(cell)
        if (neighbors < 2 or neighbors > 3):
            tmpCellList.remove(cell)
        for Emptycell in tmpEmpty:
            try:
                tmpCellList.index(Emptycell)
            except ValueError:
                if (checkNeighborsEmpty(Emptycell) == 3): tmpCellList.append(Emptycell)
    cellList = tmpCellList

def zoomOut():
    global zoomFactor
    zoomFactor = zoomFactor -1
    if(zoomFactor < 1):
        zoomFactor = 1
    

def drawCell(pos,color = cellColor):
    global screen
    x,y = pos
    if (x < 0 or y < 0):
        return

    if ((x*zoomFactor >= (screen.get_width()-zoomFactor)) or (y*zoomFactor >= (screen.get_height()-zoomFactor))):
        zoomOut()
    x = x*zoomFactor
    y = y*zoomFactor
    cell_rect = pygame.Rect(([x,y]),([zoomFactor,zoomFactor]))
    screen.fill(color,cell_rect)


def drawAllCells():
    for pos in cellList:
        drawCell(pos)

def redrawScreen():
    screen.fill(bgcolor)
    drawAllCells()
    pygame.display.flip()

def gameLoop():
    while 1:
        redrawScreen()
        for event in pygame.event.get():
            if (event.type == pygame.QUIT):
                print "Quit in Cycle: ", cycle
                sys.exit()
            if (event.type == pygame.KEYDOWN):
                if(event.key==32):
                    global running
                    if(running == False): running = True
                    else: running = False
            if (event.type == pygame.MOUSEBUTTONDOWN):
                x,y = pygame.mouse.get_pos()
                x = x/zoomFactor
                y = y/zoomFactor
                toggleCell([x,y])

        if (running):
            checkRules()

initGame()

gameLoop()
Gruß BadAgent
badagent ist offline   Mit Zitat antworten
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61