Hallo Leute,
sorry, wenn ich mich hier einmische.
Ich verfolge diesen Thread schon seit 3 Tagen.
Gestern Nacht juckte es mich derart in den Fingern, dass
ich nicht umhin kam, endlich auch so ein Hexfeld Programm
zu schreiben.
Inspiriert von wuf's code bin ich
(vorlaeufig) auf Folgendes gekommen...
Code: Alles auswählen
#!/usr/bin/env python
####
import Tkinter as tk
from math import sin, cos, pi
import random as rand
####
def unit_poly(n):
delta = 2 * pi / n
return([(cos(i * delta), sin(i * delta)) for i in xrange(n)])
####
def scale_poly(x_scale, y_scale, poly):
return [(x_scale * x, y_scale * y) for (x, y) in poly]
####
def trans_poly(x_tr, y_tr, poly):
return [(x_tr + x, y_tr + y) for (x, y) in poly]
####
def int_poly(poly):
return [(int(x + 0.5), int(y + 0.5)) for (x, y) in poly]
####
def draw_hexes(cv, x_offset, y_offset, x_delta, x_max, y_max,
colors, distrib):
poly = unit_poly(6)
x_delta3 = x_delta / 3.0
x_delta2 = x_delta / 2.0
y_delta = sin(pi / 3.0) * x_delta3
poly = scale_poly(x_delta3, x_delta3, poly)
x_max1 = x_max - 1
even_odd = 1
for iy in xrange(y_max):
even_odd ^= 1
y_pos = y_offset + iy * y_delta
for ix in xrange((x_max, x_max1)[even_odd]):
x_pos = x_offset + ix * x_delta + (0, x_delta2)[even_odd]
col = colors[rand.choice(distrib)]
draw_poly(cv, int_poly(trans_poly(x_pos, y_pos, poly)), col)
####
def draw_poly(cv, poly, col, bordcol='#000000'):
cv.create_polygon(poly, fill=col, outline=bordcol)
####
def distribute(weights):
'''generate a list for weighted color indices'''
return reduce(lambda x, y: x + y,
[[i] * w for i, w in zip(xrange(len(weights)), weights)])
####
def main_hex(**conf):
root = tk.Tk()
hexfield = tk.Canvas(root, width=conf['width'], height=conf['height'],
bg=conf['backcol'])
hexfield.pack()
# make the hexfield reproducible
startseed = conf['startseed']
if startseed:
rand.seed(startseed)
draw_hexes(hexfield, conf['x_offset'], conf['y_offset'],
conf['delta'], conf['x_max'], conf['y_max'],
conf['colors'], distribute(conf['weights']))
root.mainloop()
####
the_conf = {'width': 800,
'height': 600,
'backcol': '#000000',
'x_offset': 15,
'y_offset': 15,
'delta': 22,
'x_max': 36,
'y_max': 91,
'colors': ['#00ff00', '#ffff00', '#00ffff'],
'weights': [8, 3, 2],
'startseed': 1}
####
if __name__ == '__main__':
main_hex(**the_conf)
####
@ derkai:
Ich weiss, das Obiges ein bisschen arg komprimiert fuer Dich ist.
Falls Du jedoch weiter machst wie bisher,
hast Du spaetestens nach 600 Zeilen
den Ueberblick verloren.
(Ist wirklich nicht boese gemeint
)
Daher wuerde ich Dir raten, noch etwas mehr Zeit in die Strukturierung Deines Programmes zu investieren,
und genau zu ueberlegen, welche Datenstrukturen,
Funktionen, usw. du brauchst und wie diese interagieren sollen.
...ach so, der 1. Entwurf ist meistens fuer die Tonne,
ist auch bei mir so...
Bitte nimms mir nicht uebel...
LG yipyip