新闻资讯

一分钟学会Python模拟射箭小游戏

一分钟学会Python模拟射箭小游戏.png

如今,身边有很多的朋友都喜欢玩游戏,今天就给大家带来一个用用Python玩射箭的小游戏,相关代码如下:

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('没有射中箭靶')

效果图:

Python模拟射箭小游戏.PNG

上述就是关于用Python玩射箭的相关代码了,大家可以试着操作一下这个小游戏。

文章部分内容源于网络,联系侵删*


客户经理
787116447 15252125410