Skip to content

Commit 9aadf95

Browse files
committed
weather & game
1 parent e7dd7e5 commit 9aadf95

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
2.82 KB
Loading
65.1 KB
Loading

automatic_weather/天气查询.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
__author__ = 'luyi'
2+
import os
3+
import urllib.request
4+
import urllib.parse
5+
import json
6+
class weather(object):
7+
# 获取城市代码的uri
8+
code_uri = "http://apistore.baidu.com/microservice/cityinfo?cityname="
9+
# 获取天气信息的uri
10+
weather_uri = "http://apistore.baidu.com/microservice/weather?cityid="
11+
# 主处理逻辑
12+
def mainHandle(self):
13+
city_name = input("输入你要查询的天气:")
14+
uri = self.code_uri + urllib.parse.quote(city_name)
15+
#获取该城市天气情况的网址
16+
print("查询中请等待")
17+
ret = json.loads(urllib.request.urlopen(uri).read().decode("utf8"))
18+
if ret['errNum'] != 0:
19+
print(ret['retMsg'])
20+
return False
21+
#查询失败
22+
else:
23+
#查询成功使用json解析
24+
#需要更详细信息,请看百度api的说明文档。使用方法类似。
25+
weather_uri = self.weather_uri + ret['retData']['cityCode']
26+
data = json.loads(urllib.request.urlopen(weather_uri).read().decode("utf8"))
27+
if data['errNum'] == 0:
28+
ret_data = data['retData']
29+
output = "城市名:" + city_name + "\r\n"
30+
output += "更新时间:" + ret_data["date"] + " " + ret_data["time"] + "\r\n"
31+
output += "天气:" + ret_data["weather"] + " [" + ret_data["WD"] + ret_data["WS"] + "]\r\n"
32+
output += "当前温度:" + ret_data["temp"] + " (" + ret_data["h_tmp"] + " ---> " + ret_data["l_tmp"] + ")\r\n"
33+
print(output)
34+
return True
35+
else:
36+
print(data['errMsg'])
37+
return False
38+
if __name__ == "__main__":
39+
weather = weather()
40+
weather.mainHandle()
41+
a = input("按任意键退出")
120 KB
Loading
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#coding:utf-8
2+
import turtle
3+
import time
4+
5+
boxsize = 200
6+
caught = False
7+
score = 0
8+
#盒子大小,游戏终止条件,成绩
9+
def up():
10+
mouse.forward(10)
11+
checkbound()
12+
13+
def left():
14+
mouse.left(45)
15+
16+
def right():
17+
mouse.right(45)
18+
19+
def back():
20+
mouse.backward(10)
21+
checkbound()
22+
23+
def quitTurtles():
24+
window.bye()
25+
#以上是触发 方向,以及游戏结束的函数,其中中庸前进后退有移动。
26+
def checkbound():
27+
global boxsize
28+
if mouse.xcor() > boxsize:
29+
mouse.goto(boxsize,mouse.ycor())
30+
if mouse.xcor() < -boxsize:
31+
mouse.goto(-boxsize,mouse.ycor())
32+
if mouse.ycor() > boxsize:
33+
mouse.goto(mouse.xcor(),boxsize)
34+
if mouse.ycor() < -boxsize:
35+
mouse.goto(mouse.xcor(),-boxsize)
36+
#以上是防止老鼠跑到外面去的。所以一旦接触边缘就修改
37+
window = turtle.Screen()
38+
#设定窗口
39+
mouse = turtle.Turtle()
40+
cat = turtle.Turtle()
41+
mouse.penup()
42+
mouse.penup()
43+
mouse.goto(100,100)
44+
#将老鼠放在100.100那里
45+
window.onkeypress(up,"Up")
46+
window.onkeypress(left,"Left")
47+
window.onkeypress(right,"Right")
48+
window.onkeypress(back,"Down")
49+
window.onkeypress(quitTurtles,"Escape")
50+
#设定按键触发的函数
51+
difficulty = window.numinput("Difficulty",
52+
"enter a difficulty from easy(1) ,for hard(5)",minval = 1,maxval = 5)
53+
#初始设定难度值。
54+
window.listen()
55+
#开始监听
56+
while not caught:
57+
cat.setheading(cat.towards(mouse))
58+
cat.forward(8+difficulty)
59+
#前进速度
60+
score = score + 1
61+
if cat.distance(mouse) < 5:
62+
caught = Ture
63+
#如果两者距离小与五个像素,则停止循环
64+
time.sleep(0.2 - (0.01*difficulty))
65+
#这个代表每次追逐的时间间隔,,越短则猫越快。。
66+
window.textinput("game over", "well done, you scored:"+str(score*difficulty))
67+
window.bye()
68+
#最后输出成绩,不过似乎有点闪退。。
69+
70+
71+

0 commit comments

Comments
 (0)