网站全屏代码,写wordpress导航栏,哪有做机械设计的网站,国外域名注册服务商到上期结束#xff0c;我们已经学习了Python语言的基本数据结构#xff0c;除了数值型没有介绍#xff0c;数值型用的非常广#xff0c;但也是最容易理解的#xff0c;将在未来的学习中带大家直接接触和学习掌握。后续我们会开始学习这门语言的一些基础语法和编程技巧我们已经学习了Python语言的基本数据结构除了数值型没有介绍数值型用的非常广但也是最容易理解的将在未来的学习中带大家直接接触和学习掌握。后续我们会开始学习这门语言的一些基础语法和编程技巧在这之前我们休闲一下写一个小游戏娱乐一下。
小戏用到了Python内置的基础图形库turtle用到了random来生成随机数下面来看一下小游戏的效果比较简陋。
首先运行程序弹出游戏窗口并提示选择哪个颜色的乌龟会赢。 我们输入red鼠标点击OK 屏幕上开始初始乌龟一共出现6只不同颜色的乌龟当它们站在起跑线上后开始起跑。 小乌龟奋力向前跑去这次你很幸运押对了恭喜你你的红色小乌龟赢了比赛。 我们再来运行一次这次我们还是选择red红色小乌龟这次运气不太好我们失败了。 下面分享一下小游戏程序的完整代码
import turtle
from turtle import Turtle, Screen
import random
screen Screen()
screen.setup(width500, height400)user_bet screen.textinput(titlePlace your bet, promptWhich turtle will win the race? Enter a color: )
colors [red, orange, yellow, green, blue, purple]
y_positions [100, 60, 20, -20, -60, -100]
all_turtles []
for turtle_index in range(0,6):new_turtle Turtle(shapeturtle)new_turtle.color(colors[turtle_index])new_turtle.penup()new_turtle.goto(x-230, y y_positions[turtle_index])all_turtles.append(new_turtle)is_race_on True
while is_race_on:for turtle in all_turtles:if turtle.xcor() 200:is_race_on Falsewinning_color turtle.pencolor()if winning_color user_bet:print(fYouve won!, The {winning_color} turtle is the winner.)else:print(fYouve lost!, The {winning_color} turtle is the winner.)breakrand_distance random.randint(0, 10)turtle.forward(rand_distance)
好了本期到此结束期待下期我们再来快乐的学习