mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
fix demo/doc eol
This commit is contained in:
parent
69150c77af
commit
852f3cc0d1
2 changed files with 312 additions and 317 deletions
|
@ -1,227 +1,226 @@
|
||||||
""" turtle-example-suite:
|
""" turtle-example-suite:
|
||||||
|
|
||||||
tdemo_nim.py
|
tdemo_nim.py
|
||||||
|
|
||||||
Play nim against the computer. The player
|
Play nim against the computer. The player
|
||||||
who takes the last stick is the winner.
|
who takes the last stick is the winner.
|
||||||
|
|
||||||
Implements the model-view-controller
|
Implements the model-view-controller
|
||||||
design pattern.
|
design pattern.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import turtle
|
import turtle
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
SCREENWIDTH = 640
|
SCREENWIDTH = 640
|
||||||
SCREENHEIGHT = 480
|
SCREENHEIGHT = 480
|
||||||
|
|
||||||
MINSTICKS = 7
|
MINSTICKS = 7
|
||||||
MAXSTICKS = 31
|
MAXSTICKS = 31
|
||||||
|
|
||||||
HUNIT = SCREENHEIGHT // 12
|
HUNIT = SCREENHEIGHT // 12
|
||||||
WUNIT = SCREENWIDTH // ((MAXSTICKS // 5) * 11 + (MAXSTICKS % 5) * 2)
|
WUNIT = SCREENWIDTH // ((MAXSTICKS // 5) * 11 + (MAXSTICKS % 5) * 2)
|
||||||
|
|
||||||
SCOLOR = (63, 63, 31)
|
SCOLOR = (63, 63, 31)
|
||||||
HCOLOR = (255, 204, 204)
|
HCOLOR = (255, 204, 204)
|
||||||
COLOR = (204, 204, 255)
|
COLOR = (204, 204, 255)
|
||||||
|
|
||||||
def randomrow():
|
def randomrow():
|
||||||
return random.randint(MINSTICKS, MAXSTICKS)
|
return random.randint(MINSTICKS, MAXSTICKS)
|
||||||
|
|
||||||
def computerzug(state):
|
def computerzug(state):
|
||||||
xored = state[0] ^ state[1] ^ state[2]
|
xored = state[0] ^ state[1] ^ state[2]
|
||||||
if xored == 0:
|
if xored == 0:
|
||||||
return randommove(state)
|
return randommove(state)
|
||||||
for z in range(3):
|
for z in range(3):
|
||||||
s = state[z] ^ xored
|
s = state[z] ^ xored
|
||||||
if s <= state[z]:
|
if s <= state[z]:
|
||||||
move = (z, s)
|
move = (z, s)
|
||||||
return move
|
return move
|
||||||
|
|
||||||
def randommove(state):
|
def randommove(state):
|
||||||
m = max(state)
|
m = max(state)
|
||||||
while True:
|
while True:
|
||||||
z = random.randint(0,2)
|
z = random.randint(0,2)
|
||||||
if state[z] > (m > 1):
|
if state[z] > (m > 1):
|
||||||
break
|
break
|
||||||
rand = random.randint(m > 1, state[z]-1)
|
rand = random.randint(m > 1, state[z]-1)
|
||||||
return z, rand
|
return z, rand
|
||||||
|
|
||||||
|
|
||||||
class NimModel(object):
|
class NimModel(object):
|
||||||
def __init__(self, game):
|
def __init__(self, game):
|
||||||
self.game = game
|
self.game = game
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
if self.game.state not in [Nim.CREATED, Nim.OVER]:
|
if self.game.state not in [Nim.CREATED, Nim.OVER]:
|
||||||
return
|
return
|
||||||
self.sticks = [randomrow(), randomrow(), randomrow()]
|
self.sticks = [randomrow(), randomrow(), randomrow()]
|
||||||
self.player = 0
|
self.player = 0
|
||||||
self.winner = None
|
self.winner = None
|
||||||
self.game.view.setup()
|
self.game.view.setup()
|
||||||
self.game.state = Nim.RUNNING
|
self.game.state = Nim.RUNNING
|
||||||
|
|
||||||
def move(self, row, col):
|
def move(self, row, col):
|
||||||
maxspalte = self.sticks[row]
|
maxspalte = self.sticks[row]
|
||||||
self.sticks[row] = col
|
self.sticks[row] = col
|
||||||
self.game.view.notify_move(row, col, maxspalte, self.player)
|
self.game.view.notify_move(row, col, maxspalte, self.player)
|
||||||
if self.game_over():
|
if self.game_over():
|
||||||
self.game.state = Nim.OVER
|
self.game.state = Nim.OVER
|
||||||
self.winner = self.player
|
self.winner = self.player
|
||||||
self.game.view.notify_over()
|
self.game.view.notify_over()
|
||||||
elif self.player == 0:
|
elif self.player == 0:
|
||||||
self.player = 1
|
self.player = 1
|
||||||
row, col = computerzug(self.sticks)
|
row, col = computerzug(self.sticks)
|
||||||
self.move(row, col)
|
self.move(row, col)
|
||||||
self.player = 0
|
self.player = 0
|
||||||
|
|
||||||
def game_over(self):
|
def game_over(self):
|
||||||
return self.sticks == [0, 0, 0]
|
return self.sticks == [0, 0, 0]
|
||||||
|
|
||||||
def notify_move(self, row, col):
|
def notify_move(self, row, col):
|
||||||
if self.sticks[row] <= col:
|
if self.sticks[row] <= col:
|
||||||
return
|
return
|
||||||
self.move(row, col)
|
self.move(row, col)
|
||||||
|
|
||||||
|
|
||||||
class Stick(turtle.Turtle):
|
class Stick(turtle.Turtle):
|
||||||
def __init__(self, row, col, game):
|
def __init__(self, row, col, game):
|
||||||
turtle.Turtle.__init__(self, visible=False)
|
turtle.Turtle.__init__(self, visible=False)
|
||||||
self.row = row
|
self.row = row
|
||||||
self.col = col
|
self.col = col
|
||||||
self.game = game
|
self.game = game
|
||||||
x, y = self.coords(row, col)
|
x, y = self.coords(row, col)
|
||||||
self.shape("square")
|
self.shape("square")
|
||||||
self.shapesize(HUNIT/10.0, WUNIT/20.0)
|
self.shapesize(HUNIT/10.0, WUNIT/20.0)
|
||||||
self.speed(0)
|
self.speed(0)
|
||||||
self.pu()
|
self.pu()
|
||||||
self.goto(x,y)
|
self.goto(x,y)
|
||||||
self.color("white")
|
self.color("white")
|
||||||
self.showturtle()
|
self.showturtle()
|
||||||
|
|
||||||
def coords(self, row, col):
|
def coords(self, row, col):
|
||||||
packet, remainder = divmod(col, 5)
|
packet, remainder = divmod(col, 5)
|
||||||
x = (3 + 11 * packet + 2 * remainder) * WUNIT
|
x = (3 + 11 * packet + 2 * remainder) * WUNIT
|
||||||
y = (2 + 3 * row) * HUNIT
|
y = (2 + 3 * row) * HUNIT
|
||||||
return x - SCREENWIDTH // 2 + WUNIT // 2, SCREENHEIGHT // 2 - y - HUNIT // 2
|
return x - SCREENWIDTH // 2 + WUNIT // 2, SCREENHEIGHT // 2 - y - HUNIT // 2
|
||||||
|
|
||||||
def makemove(self, x, y):
|
def makemove(self, x, y):
|
||||||
if self.game.state != Nim.RUNNING:
|
if self.game.state != Nim.RUNNING:
|
||||||
return
|
return
|
||||||
self.game.controller.notify_move(self.row, self.col)
|
self.game.controller.notify_move(self.row, self.col)
|
||||||
|
|
||||||
|
|
||||||
class NimView(object):
|
class NimView(object):
|
||||||
def __init__(self, game):
|
def __init__(self, game):
|
||||||
self.game = game
|
self.game = game
|
||||||
self.screen = game.screen
|
self.screen = game.screen
|
||||||
self.model = game.model
|
self.model = game.model
|
||||||
self.screen.colormode(255)
|
self.screen.colormode(255)
|
||||||
self.screen.tracer(False)
|
self.screen.tracer(False)
|
||||||
self.screen.bgcolor((240, 240, 255))
|
self.screen.bgcolor((240, 240, 255))
|
||||||
self.writer = turtle.Turtle(visible=False)
|
self.writer = turtle.Turtle(visible=False)
|
||||||
self.writer.pu()
|
self.writer.pu()
|
||||||
self.writer.speed(0)
|
self.writer.speed(0)
|
||||||
self.sticks = {}
|
self.sticks = {}
|
||||||
for row in range(3):
|
for row in range(3):
|
||||||
for col in range(MAXSTICKS):
|
for col in range(MAXSTICKS):
|
||||||
self.sticks[(row, col)] = Stick(row, col, game)
|
self.sticks[(row, col)] = Stick(row, col, game)
|
||||||
self.display("... a moment please ...")
|
self.display("... a moment please ...")
|
||||||
self.screen.tracer(True)
|
self.screen.tracer(True)
|
||||||
|
|
||||||
def display(self, msg1, msg2=None):
|
def display(self, msg1, msg2=None):
|
||||||
self.screen.tracer(False)
|
self.screen.tracer(False)
|
||||||
self.writer.clear()
|
self.writer.clear()
|
||||||
if msg2 is not None:
|
if msg2 is not None:
|
||||||
self.writer.goto(0, - SCREENHEIGHT // 2 + 48)
|
self.writer.goto(0, - SCREENHEIGHT // 2 + 48)
|
||||||
self.writer.pencolor("red")
|
self.writer.pencolor("red")
|
||||||
self.writer.write(msg2, align="center", font=("Courier",18,"bold"))
|
self.writer.write(msg2, align="center", font=("Courier",18,"bold"))
|
||||||
self.writer.goto(0, - SCREENHEIGHT // 2 + 20)
|
self.writer.goto(0, - SCREENHEIGHT // 2 + 20)
|
||||||
self.writer.pencolor("black")
|
self.writer.pencolor("black")
|
||||||
self.writer.write(msg1, align="center", font=("Courier",14,"bold"))
|
self.writer.write(msg1, align="center", font=("Courier",14,"bold"))
|
||||||
self.screen.tracer(True)
|
self.screen.tracer(True)
|
||||||
|
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
self.screen.tracer(False)
|
self.screen.tracer(False)
|
||||||
for row in range(3):
|
for row in range(3):
|
||||||
for col in range(self.model.sticks[row]):
|
for col in range(self.model.sticks[row]):
|
||||||
self.sticks[(row, col)].color(SCOLOR)
|
self.sticks[(row, col)].color(SCOLOR)
|
||||||
for row in range(3):
|
for row in range(3):
|
||||||
for col in range(self.model.sticks[row], MAXSTICKS):
|
for col in range(self.model.sticks[row], MAXSTICKS):
|
||||||
self.sticks[(row, col)].color("white")
|
self.sticks[(row, col)].color("white")
|
||||||
self.display("Your turn! Click leftmost stick to remove.")
|
self.display("Your turn! Click leftmost stick to remove.")
|
||||||
self.screen.tracer(True)
|
self.screen.tracer(True)
|
||||||
|
|
||||||
def notify_move(self, row, col, maxspalte, player):
|
def notify_move(self, row, col, maxspalte, player):
|
||||||
if player == 0:
|
if player == 0:
|
||||||
farbe = HCOLOR
|
farbe = HCOLOR
|
||||||
for s in range(col, maxspalte):
|
for s in range(col, maxspalte):
|
||||||
self.sticks[(row, s)].color(farbe)
|
self.sticks[(row, s)].color(farbe)
|
||||||
else:
|
else:
|
||||||
self.display(" ... thinking ... ")
|
self.display(" ... thinking ... ")
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
self.display(" ... thinking ... aaah ...")
|
self.display(" ... thinking ... aaah ...")
|
||||||
farbe = COLOR
|
farbe = COLOR
|
||||||
for s in range(maxspalte-1, col-1, -1):
|
for s in range(maxspalte-1, col-1, -1):
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
self.sticks[(row, s)].color(farbe)
|
self.sticks[(row, s)].color(farbe)
|
||||||
self.display("Your turn! Click leftmost stick to remove.")
|
self.display("Your turn! Click leftmost stick to remove.")
|
||||||
|
|
||||||
def notify_over(self):
|
def notify_over(self):
|
||||||
if self.game.model.winner == 0:
|
if self.game.model.winner == 0:
|
||||||
msg2 = "Congrats. You're the winner!!!"
|
msg2 = "Congrats. You're the winner!!!"
|
||||||
else:
|
else:
|
||||||
msg2 = "Sorry, the computer is the winner."
|
msg2 = "Sorry, the computer is the winner."
|
||||||
self.display("To play again press space bar. To leave press ESC.", msg2)
|
self.display("To play again press space bar. To leave press ESC.", msg2)
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
if self.game.state == Nim.OVER:
|
if self.game.state == Nim.OVER:
|
||||||
self.screen.clear()
|
self.screen.clear()
|
||||||
|
|
||||||
class NimController(object):
|
class NimController(object):
|
||||||
|
|
||||||
def __init__(self, game):
|
def __init__(self, game):
|
||||||
self.game = game
|
self.game = game
|
||||||
self.sticks = game.view.sticks
|
self.sticks = game.view.sticks
|
||||||
self.BUSY = False
|
self.BUSY = False
|
||||||
for stick in self.sticks.values():
|
for stick in self.sticks.values():
|
||||||
stick.onclick(stick.makemove)
|
stick.onclick(stick.makemove)
|
||||||
self.game.screen.onkey(self.game.model.setup, "space")
|
self.game.screen.onkey(self.game.model.setup, "space")
|
||||||
self.game.screen.onkey(self.game.view.clear, "Escape")
|
self.game.screen.onkey(self.game.view.clear, "Escape")
|
||||||
self.game.view.display("Press space bar to start game")
|
self.game.view.display("Press space bar to start game")
|
||||||
self.game.screen.listen()
|
self.game.screen.listen()
|
||||||
|
|
||||||
def notify_move(self, row, col):
|
def notify_move(self, row, col):
|
||||||
if self.BUSY:
|
if self.BUSY:
|
||||||
return
|
return
|
||||||
self.BUSY = True
|
self.BUSY = True
|
||||||
self.game.model.notify_move(row, col)
|
self.game.model.notify_move(row, col)
|
||||||
self.BUSY = False
|
self.BUSY = False
|
||||||
|
|
||||||
class Nim(object):
|
class Nim(object):
|
||||||
CREATED = 0
|
CREATED = 0
|
||||||
RUNNING = 1
|
RUNNING = 1
|
||||||
OVER = 2
|
OVER = 2
|
||||||
def __init__(self, screen):
|
def __init__(self, screen):
|
||||||
self.state = Nim.CREATED
|
self.state = Nim.CREATED
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.model = NimModel(self)
|
self.model = NimModel(self)
|
||||||
self.view = NimView(self)
|
self.view = NimView(self)
|
||||||
self.controller = NimController(self)
|
self.controller = NimController(self)
|
||||||
|
|
||||||
|
|
||||||
mainscreen = turtle.Screen()
|
mainscreen = turtle.Screen()
|
||||||
mainscreen.mode("standard")
|
mainscreen.mode("standard")
|
||||||
mainscreen.setup(SCREENWIDTH, SCREENHEIGHT)
|
mainscreen.setup(SCREENWIDTH, SCREENHEIGHT)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
nim = Nim(mainscreen)
|
nim = Nim(mainscreen)
|
||||||
return "EVENTLOOP!"
|
return "EVENTLOOP!"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
turtle.mainloop()
|
turtle.mainloop()
|
||||||
|
|
||||||
|
|
|
@ -1,90 +1,86 @@
|
||||||
""" turtle-example-suite:
|
""" turtle-example-suite:
|
||||||
|
|
||||||
tdemo_round_dance.py
|
tdemo_round_dance.py
|
||||||
|
|
||||||
(Needs version 1.1 of the turtle module that
|
(Needs version 1.1 of the turtle module that
|
||||||
comes with Python 3.1)
|
comes with Python 3.1)
|
||||||
|
|
||||||
Dancing turtles have a compound shape
|
Dancing turtles have a compound shape
|
||||||
consisting of a series of triangles of
|
consisting of a series of triangles of
|
||||||
decreasing size.
|
decreasing size.
|
||||||
|
|
||||||
Turtles march along a circle while rotating
|
Turtles march along a circle while rotating
|
||||||
pairwise in opposite direction, with one
|
pairwise in opposite direction, with one
|
||||||
exception. Does that breaking of symmetry
|
exception. Does that breaking of symmetry
|
||||||
enhance the attractiveness of the example?
|
enhance the attractiveness of the example?
|
||||||
|
|
||||||
Press any key to stop the animation.
|
Press any key to stop the animation.
|
||||||
|
|
||||||
Technically: demonstrates use of compound
|
Technically: demonstrates use of compound
|
||||||
shapes, transformation of shapes as well as
|
shapes, transformation of shapes as well as
|
||||||
cloning turtles. The animation is
|
cloning turtles. The animation is
|
||||||
controlled through update().
|
controlled through update().
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from turtle import *
|
from turtle import *
|
||||||
|
|
||||||
def stop():
|
def stop():
|
||||||
global running
|
global running
|
||||||
running = False
|
running = False
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global running
|
global running
|
||||||
clearscreen()
|
clearscreen()
|
||||||
bgcolor("gray10")
|
bgcolor("gray10")
|
||||||
tracer(False)
|
tracer(False)
|
||||||
shape("triangle")
|
shape("triangle")
|
||||||
f = 0.793402
|
f = 0.793402
|
||||||
phi = 9.064678
|
phi = 9.064678
|
||||||
s = 5
|
s = 5
|
||||||
c = 1
|
c = 1
|
||||||
# create compound shape
|
# create compound shape
|
||||||
sh = Shape("compound")
|
sh = Shape("compound")
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
shapesize(s)
|
shapesize(s)
|
||||||
p =get_shapepoly()
|
p =get_shapepoly()
|
||||||
s *= f
|
s *= f
|
||||||
c *= f
|
c *= f
|
||||||
tilt(-phi)
|
tilt(-phi)
|
||||||
sh.addcomponent(p, (c, 0.25, 1-c), "black")
|
sh.addcomponent(p, (c, 0.25, 1-c), "black")
|
||||||
register_shape("multitri", sh)
|
register_shape("multitri", sh)
|
||||||
# create dancers
|
# create dancers
|
||||||
shapesize(1)
|
shapesize(1)
|
||||||
shape("multitri")
|
shape("multitri")
|
||||||
pu()
|
pu()
|
||||||
setpos(0, -200)
|
setpos(0, -200)
|
||||||
dancers = []
|
dancers = []
|
||||||
for i in range(180):
|
for i in range(180):
|
||||||
fd(7)
|
fd(7)
|
||||||
tilt(-4)
|
tilt(-4)
|
||||||
lt(2)
|
lt(2)
|
||||||
update()
|
update()
|
||||||
if i % 12 == 0:
|
if i % 12 == 0:
|
||||||
dancers.append(clone())
|
dancers.append(clone())
|
||||||
home()
|
home()
|
||||||
# dance
|
# dance
|
||||||
running = True
|
running = True
|
||||||
onkeypress(stop)
|
onkeypress(stop)
|
||||||
listen()
|
listen()
|
||||||
cs = 1
|
cs = 1
|
||||||
while running:
|
while running:
|
||||||
ta = -4
|
ta = -4
|
||||||
for dancer in dancers:
|
for dancer in dancers:
|
||||||
dancer.fd(7)
|
dancer.fd(7)
|
||||||
dancer.lt(2)
|
dancer.lt(2)
|
||||||
dancer.tilt(ta)
|
dancer.tilt(ta)
|
||||||
ta = -4 if ta > 0 else 2
|
ta = -4 if ta > 0 else 2
|
||||||
if cs < 180:
|
if cs < 180:
|
||||||
right(4)
|
right(4)
|
||||||
shapesize(cs)
|
shapesize(cs)
|
||||||
cs *= 1.005
|
cs *= 1.005
|
||||||
update()
|
update()
|
||||||
return "DONE!"
|
return "DONE!"
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
print(main())
|
print(main())
|
||||||
mainloop()
|
mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue