今天我们用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('没有射中箭靶')
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有