[Python-de] Das Modul pygame
Fritz Cizmarov
fritz at sol.at
Fri Jan 31 01:00:22 EST 2003
Hallo Gregor,
in Zeile 78 fand sich noch ein kleiner Fehler und ich hab das Programm
so verändert, daß es auch mit Pythonversionen vor 2.2 läuft, unter z.B.
Debian Linus ist noch 2.1 standart bei Python
Gruß
Dookie
P.S.: Echt nettes Beispiel
### Python für Kids - Kapitel B4 ###
# Autor: Gregor Lingl
# Datum: 29. 9. 2002
# hanoig.py
from sys import version_info
if version_info <= (2,2): # check for version < 2.2
from UserList import UserList as list
from Tkinter import Tk, Canvas
from Canvas import Rectangle
# from scheibe import Scheibe
usingIDLE = 0
class Scheibe(Rectangle):
"Bewegliches Rechteck auf einem Tkinter-Canvas"
def __init__(self,cv,pos,laenge,hoehe):
x0, y0 = pos
x1, x2 = x0-laenge/2.0, x0+laenge/2.0
y1, y2 = y0-hoehe, y0
Rectangle.__init__(self,cv,x1,y1,x2,y2,
fill = "red")
def bewege_nach(self, x, y):
from math import sqrt
x1,y1,x2,y2 = self.coords()
x0, y0 = (x1 + x2)/2, y2
dx, dy = x-x0, y-y0
d = sqrt(dx**2+dy**2)
schritte = int(d/10) + 1
dx, dy = dx/schritte, dy/schritte
for i in range(schritte):
self.move(dx,dy)
self.canvas.update()
self.canvas.after(20)
class Turm(list):
def __init__(self, x, y, h):
if version_info <= (2,2): self.data = []
self.x = x
self.y = y
self.h = h
def top(self):
return self.x, self.y - len(self)*self.h
def hanoi(n,von,nach,hilf):
if n==1:
move(von,nach)
else:
hanoi(n-1,von,hilf,nach)
hanoi(1,von,nach,hilf)
hanoi(n-1,hilf,nach,von)
def move(von_turm, nach_turm):
scheibe = von_turm.pop()
x1, y1 = von_turm.top()
x2, y2 = nach_turm.top()
scheibe.bewege_nach(x1,20)
scheibe.bewege_nach(x2,20)
scheibe.bewege_nach(x2,y2)
nach_turm.append(scheibe)
def hanoispiel(n):
"""Führt ein Hanoi-Spiel mit n Scheiben aus:"""
root = Tk() # ein Fenster
root.title("Towers of Hanoi") # mit Titel,
cv = Canvas(root,width=440,height=210) # eine Leinwand -
cv.pack() # - aufspannen
pflock1 = Rectangle(cv, 75, 40, 85,190,fill='blue')
pflock2 = Rectangle(cv,215, 40,225,190,fill='blue')
pflock3 = Rectangle(cv,355, 40,365,190,fill='blue')
boden = Rectangle(cv, 5,190,435,200,fill='black')
turm_a = Turm( 80, 190, 15)
turm_b = Turm(220, 190, 15)
turm_c = Turm(360, 190, 15)
for i in range(n): # turm_a aufbauen
laengen_differenz = 100 / n
laenge = 120 - i * laengen_differenz
s = Scheibe( cv, turm_a.top(), laenge, 13)
turm_a.append(s)
hanoi(n, turm_a, turm_b, turm_c)
if not usingIDLE:
root.mainloop()
if __name__ == '__main__':
hanoispiel(7)
More information about the Python-de
mailing list