Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
bellcode
/
lesson8-1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
44df3932
authored
Sep 25, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
98f24605
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
149 additions
and
18 deletions
diy1.py
diy2.py
quiz.py
diy1.py
View file @
44df3932
# 这是悟空为花果山小猴做臂力测试的程序代码
dict_hero
=
{
'猴一'
:
10
,
'猴三'
:
20
,
'猴队长'
:
29
,
'猴七'
:
15
}
name
=
input
(
'你叫啥名啊?'
)
power
=
int
(
input
(
'你臂力多少啊?'
))
list_hero
=
[
'猴三'
,
10
,
'猴一'
,
21
,
'猴五'
,
22
,
'猴队长'
,
29
,
'猴七'
,
30
]
for
i
in
range
(
len
(
list_hero
)):
if
i
%
2
==
1
and
list_hero
[
i
]
>=
power
:
list_hero
.
insert
(
i
-
1
,
name
)
list_hero
.
insert
(
i
,
power
)
break
print
(
list_hero
)
# 请注释掉上面的代码,并在下一行创建一个名为dict_hero的字典
dict_hero
=
print
(
dict_hero
)
print
(
dict_hero
)
#提取猴队长的臂力值
a
=
dict_hero
[
'猴队长'
]
print
(
a
)
#修改猴队长的臂力值为32
dict_hero
[
'猴队长'
]
=
32
a
=
dict_hero
[
'猴队长'
]
print
(
a
)
dict_hero
[
'猴王'
]
=
100
print
(
dict_hero
)
\ No newline at end of file
diy2.py
0 → 100644
View file @
44df3932
import
time
#导入时间模块
import
turtle
#导入时间模块
screen
=
turtle
.
Screen
()
#创建窗口对象
screen
.
bgcolor
(
'bisque'
)
#设置背景颜色
mouse
=
turtle
.
Turtle
()
#创建老鼠画笔对象
mouse
.
shape
(
"turtle"
)
#设置画笔形状为turtle
mouse
.
color
(
"blue"
)
#设置老鼠画笔颜色
cat
=
turtle
.
Turtle
()
#创建猫画笔对象
cat
.
shape
(
"turtle"
)
#设置猫画笔的形状为turtle
cat
.
color
(
'red'
)
#设置猫画笔颜色
mouse
.
up
()
#老鼠画笔抬笔,不会出现移动轨迹
mouse
.
goto
(
100
,
100
)
#老鼠初始位置
boxsize
=
400
#设置追逐范围
caught
=
False
#设置追逐是否成功,默认为False表示没有追逐成功
score
=
0
#初始化分数为0
#创建老鼠画笔前后左右方法
def
up
():
#前进
mouse
.
fd
(
10
)
def
back
():
#后退
mouse
.
bk
(
10
)
def
left
():
#左转
mouse
.
lt
(
5
)
def
right
():
#右转
mouse
.
rt
(
5
)
def
quitTurtle
():
#结束游戏方法
screen
.
bye
()
#检测老鼠是否超出追逐范围
def
checkbound
():
global
boxsize
#将变量设置为全局变量
if
mouse
.
xcor
()
>
boxsize
:
#老鼠x坐标大于追逐范围边长
mouse
.
goto
(
boxsize
,
mouse
.
ycor
())
#将老鼠位置限制在右边缘
if
mouse
.
xcor
()
<
-
boxsize
:
mouse
.
goto
(
-
boxsize
,
mouse
.
ycor
())
#将老鼠位置限制在左边缘
if
mouse
.
ycor
()
>
boxsize
:
mouse
.
goto
(
mouse
.
xcor
(),
boxsize
)
#将老鼠位置限制在上边缘
if
mouse
.
ycor
()
<
-
boxsize
:
mouse
.
goto
(
mouse
.
xcor
(),
-
boxsize
)
#将老鼠位置限制在下边缘
#按键侦测控制老鼠前后左右移动
screen
.
onkeypress
(
up
,
"Up"
)
screen
.
onkeypress
(
back
,
'Down'
)
screen
.
onkeypress
(
left
,
"Left"
)
screen
.
onkeypress
(
right
,
"Right"
)
screen
.
onkeypress
(
quitTurtle
,
"Escape"
)
#设置游戏难度
difficulty
=
screen
.
numinput
(
"难度"
,
"请输入游戏难度值(1~5)"
,
minval
=
1
,
maxval
=
5
)
screen
.
listen
()
#按键监听
#实现小猫画笔追逐
while
not
caught
:
#当没有追逐成功的时候才可以进行重复执行
checkbound
()
cat
.
setheading
(
cat
.
towards
(
mouse
))
#小猫从始至终朝向老鼠的方向
cat
.
forward
(
8
+
difficulty
)
#小猫移动速度随着难度数值的增加而增加
score
+=
1
#加分
if
cat
.
distance
(
mouse
)
<
5
:
#如果小猫距离老鼠的距离小于5
caught
=
True
#将追逐状态修改为真,表示追逐成功
time
.
sleep
(
0.2
-
(
0.01
*
difficulty
))
#游戏运行速度随着难度提升
screen
.
textinput
(
"GameOver"
,
"游戏得分:"
+
str
(
score
*
difficulty
))
#显示最终得分
turtle
.
mainloop
()
\ No newline at end of file
quiz.py
View file @
44df3932
# 直接运行以下代码,说说你的发现:
from
turtle
import
*
#导入turtle模块
list_hero
=
[
'赵一'
,
30
,
'丁二'
,
37
,
'孙五'
,
52
,
'王猛'
,
89
,
'周亮'
,
98
]
import
time
#导入时间模块
dict_hero
=
{
'赵一'
:
30
,
'丁二'
:
37
,
'孙五'
:
52
,
'王猛'
:
89
,
'周亮'
:
98
}
window
=
Screen
()
#创建窗口对象
print
(
len
(
list_hero
))
window
.
bgcolor
(
'wheat'
)
print
(
len
(
dict_hero
))
mouse
=
Turtle
()
#创建老鼠画笔
\ No newline at end of file
mouse
.
color
(
'blue'
)
#设置老鼠画笔颜色为蓝色
cat
=
Turtle
()
#创建猫画笔对象
cat
.
color
(
'red'
)
#设置猫画笔颜色
mouse
.
shape
(
"turtle"
)
#设置画笔形状
cat
.
shape
(
"turtle"
)
mouse
.
up
()
#老鼠画笔抬笔
mouse
.
goto
(
100
,
100
)
boxsize
=
400
#追逐范围边长
caught
=
False
#追逐是否成功,默认为False,表示不成功
score
=
0
#接下来创建老鼠画笔的前后左右方法
def
up
():
#上
mouse
.
fd
(
10
)
def
down
():
#下
mouse
.
bk
(
10
)
def
left
():
#左
mouse
.
lt
(
10
)
def
right
():
#右
mouse
.
rt
(
10
)
def
quit
():
window
.
bye
()
#关闭游戏窗口
#创建侦测是否超出范围的方法
def
checkbound
():
global
boxsize
#转为全局变量
if
mouse
.
xcor
()
>
boxsize
:
mouse
.
goto
(
boxsize
,
mouse
.
ycor
())
if
mouse
.
xcor
()
<
-
boxsize
:
mouse
.
goto
(
-
boxsize
,
mouse
.
ycor
())
if
mouse
.
ycor
()
>
boxsize
:
mouse
.
goto
(
mouse
.
xcor
(),
boxsize
)
if
mouse
.
ycor
()
<
-
boxsize
:
mouse
.
goto
(
mouse
.
xcor
(),
-
boxsize
)
#按键控制老鼠移动
window
.
onkeypress
(
up
,
"Up"
)
window
.
onkeypress
(
down
,
"Down"
)
window
.
onkeypress
(
left
,
"Left"
)
window
.
onkeypress
(
right
,
"Right"
)
window
.
onkeypress
(
quit
,
"Escape"
)
lv
=
window
.
numinput
(
"难度"
,
"请输入难度值(1~5)"
,
minval
=
1
,
maxval
=
5
)
window
.
listen
()
#窗口监听
#小猫画笔的移动
while
not
caught
:
#没有追逐成功的时候才可以进行重复执行
checkbound
()
cat
.
setheading
(
cat
.
towards
(
mouse
))
#让小猫始终朝向老鼠的方向
cat
.
fd
(
2
+
lv
)
# 小猫移速随着难度提升而增加
score
+=
1
if
cat
.
distance
(
mouse
)
<
5
:
#如果猫与老鼠的距离小于5
caught
=
True
time
.
sleep
(
0.2
-
(
0.01
*
lv
))
#控制游戏运行速度,难度越高,速度越快
window
.
textinput
(
"GameOver"
,
"游戏得分:"
+
str
(
score
*
lv
))
mainloop
()
#保持监听(窗口不关闭)A
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment