import turtle as t def filledSemicircle(radius, color): """ draws a semicircle above the x axis, centred on the origin, of radius radius. """ t.penup() t.goto((radius, 0)) t.fillcolor(color) t.begin_fill() t.pendown() t.setheading(90) t.circle(radius, 180) t.end_fill() t.penup() def rainbow(): """ draws a semicircular rainbow with 7 colors. The center of the rainbow is white. The rainbow is centered on the origin and os of width 400 units. """ t.hideturtle() filledSemicircle(200, 'darkviolet') filledSemicircle(190, 'indigo') filledSemicircle(180, 'blue') filledSemicircle(170, 'green') filledSemicircle(160, 'yellow') filledSemicircle(150, 'orange') filledSemicircle(140, 'red') filledSemicircle(130, 'white') def target(targetRadius): """draw an archery target centered on the origin, with overall radius targetRadius""" t.speed(10) ringWidth = targetRadius/10 edgedCircle(10*ringWidth, "white") edgedCircle(9*ringWidth, "white") edgedCircle(8*ringWidth, "black") edgedCircle(7*ringWidth, "black") edgedCircle(6*ringWidth, "lightblue") edgedCircle(5*ringWidth, "lightblue") edgedCircle(4*ringWidth, "red") edgedCircle(3*ringWidth, "red") edgedCircle(2*ringWidth, "gold") edgedCircle(1*ringWidth, "gold") def edgedCircle(size, color): """draw a circle with a contrasting edge, filled with color, of radius size, centered on the origin """ t.penup() t.hideturtle() if (color == "black"): t.pencolor("white") else: t.pencolor("black") t.fillcolor(color) t.goto((size,0)) t.setheading(90) t.pendown() t.begin_fill() t.circle(size) t.end_fill() def shoot(targetRadius): """Fire a pseudo-random arrow on or near the target with radius targetRadius. Draw a hole in the target (or near the target) where the “arrow†lands. Return the distance from the origin at which the arrow lands. """ import random import math r = random.randrange(0, math.ceil(targetRadius*1.1)) theta = random.randrange(0,259) t.penup() t.hideturtle() t.goto((0,0)) t.setheading(theta) t.forward(r) t.dot(10, "darkgreen") return r def score(distance, targetRadius): """return the archery score corresponding to an arrow that lands distance from a target of radius targetRadius. Examples: >>> score(0,200) 10 >>> score(1,200) 10 >>> score(19,200) 10 >>> score(20,200) 9 >>> score(29,200) 9 >>> score(39,200) 9 >>> score(40,200) 8 >>> score(199,200) 1 >>> score(200,200) 0 >>> score(440,200) 0 """ import math ringNumber = math.floor(10*distance/targetRadius) score = 10-ringNumber return max(score, 0) def displayScore(s, targetRadius): """Displays the score s centered under the target of radius targetRadius""" t.penup() t.goto((0, - (targetRadius + 40))) t.write( "Your score is " + str(s), align="center", font=("Arial", 20, "normal")) def singleShot(size): """Play single-shot archery with a target of radius size""" target(size) distance = shoot(size) myScore = score(distance, size) displayScore(myScore, size)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question