儿童编程趣Python我爱编程

趣玩Python之《射箭》篇

2018-11-20  本文已影响52人  一石匠人

今天我们用Python来模拟射箭。箭射出后,系统判定中了几环,并显示在屏幕上。
程序分为三个部分:
(1)箭靶设置
(2)射箭过程
(3)成绩判断
效果图如下:


《射箭》效果图

完成代码如下:

from turtle import *
import random
import math
#箭靶设置
speed(0)
hideturtle()
penup()
goto(0,-10)
pendown()
circle(10)
penup()
goto(0,-20)
pendown()
circle(20)
penup()
goto(0,-40)
pendown()
circle(40)
penup()
goto(0,-70)
pendown()
circle(70)
penup()
goto(0,-110)
pendown()
circle(110)
penup()
goto(0,-160)
pendown()
circle(160)

#射箭过程
penup()
goto(-600,-600)
showturtle()
speed(5)
pendown()
myx=random.randint(-160,160)
myy=random.randint(-160,160)
goto(myx,myy)
dis=math.sqrt(myx**2+myy**2)
#print(myx,myy,dis)

#成绩判断
if dis<10:
    write('10环')
elif dis>10 and dis<=20:
    write('9环')
elif dis>20 and dis<=40:
    write('8环')
elif dis>40 and dis<=70:
    write('7环')
elif dis>70 and dis<=110:
    write('6环')
elif dis>110 and dis<=160:
    write('5环')
else:
    write('没有射中箭靶')
上一篇 下一篇

猜你喜欢

热点阅读