Python代码编程基础第13课 海底世界(2)

知识要点

  1. 游戏结束画面
  2. onclick()函数
  3. GIF转换器 — Convertio

一、PPT课件

二、知识要点

1.游戏结束画面

  • 把game over图片添加到造型库
  • 用Turtle()方法新建一个海龟
  • 把海龟造型设定为这个图片

2.onclick()函数

  • 把鼠标点击事件函数与海龟关联,实现监听鼠标点击事件

  • 鼠标点击事件函数必须有两个参数,分别对应画布上点的横坐标和纵坐标。

三、代码示例

1.case

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

# -*- coding: utf-8 -*-

import time
import turtle as t
import random

t.bgpic("bg1.png")
t.shape("turtle")
t.shapesize(2, 2)
t.color("dark blue")
t.penup()
t.tracer(20)

# 游戏标题
titleT = t.Turtle()
titleT.penup()
titleT.color("gold")
titleT.hideturtle()
titleT.goto(0, 250)
titleT.write("按左右箭头控制海龟的运动方向", align="center",
font=("Times", 30, "bold"))

# 加载食物图片
food = t.Turtle()
t.addshape('food2.gif')
t.addshape('yu2.gif')
t.addshape('xia2.gif')
shapeList = ['food2.gif', 'yu2.gif', 'xia2.gif']

# 生成障碍和分数的海龟
obstacle = t.Turtle()
score_t = t.Turtle()


# 按键交互事件函数
def turnLeft():
t.left(30)


def turnRight():
t.right(30)


t.listen()
t.onkeypress(turnLeft, "Left")
t.onkeypress(turnRight, "Right")


# 绘制食物图案
def drawfood():
food.hideturtle()
food.pu()
x = random.randint(-600, 600)
y = random.randint(-400, 400)
t.colormode(255)
food.goto(x, y)
food.shape(shapeList[random.randint(0, 2)])
food.showturtle()


# 小球碰撞检测
def isCollided(a, b):
if a.distance(b) < 25:
return True


# 定义障碍
def showobstacle():
obstacle.hideturtle()
t.addshape('fishtrap3.gif')
obstacle.shape('fishtrap3.gif')
obstacle.pensize(500)
obstacle.pu()
obstacle.goto(random.randint(-300, 300), random.randint(-300, 300))
obstacle.showturtle()


# 障碍碰撞检测
def turtleCaught():
caught = isCollided(t, obstacle)
return caught


def obstacleMovement():
obstacle.pu()
obstacle.hideturtle()
obstacle.goto(random.randint(-300, 300), random.randint(-300, 300))
obstacle.showturtle()


# 边缘碰撞检测
def crashTurtle():
x = t.xcor()
y = t.ycor()
if x > 960 or x < - 960 or y > 444 or y < -444:
print(t.xcor(), t.ycor())
return True

# 加分系统


def scoreAdd(s):
score_t.hideturtle()
score_t.pu()
score_t.color("gold")
score_t.goto(-460, 250)
score_t.write('分数:' + str(s), align="left", font=("Times", 30, "bold"))


# 分数速度系统
def speedSystem(score):
if score < 4:
return 2
elif score < 8:
return 3
elif score < 12:
return 4
else:
return 5


t.addshape("gameover3.gif")
shapeT = t.Turtle()
shapeT.shape("gameover3.gif")
shapeT.shapesize(20, 20)
shapeT.hideturtle()


def runGame():
global shapeT
global speed
global score

t.tracer(20)
score_t.clear()
score = 0
speed = 2
scoreAdd(score)
t.goto(random.randint(-300, 300), random.randint(-300, 300))
drawfood()
showobstacle()
shapeT.hideturtle()
obstacle.showturtle()
food.showturtle()
t.showturtle()
t.tracer(1)
while True:
speed = speedSystem(score)
t.forward(speed)
meal = isCollided(t, food)
collide = crashTurtle()
caught = turtleCaught()

if collide or caught:
break

if meal:
t.tracer(20)
score = score + 1
score_t.clear()
scoreAdd(score)
food.clear()
drawfood()
obstacleMovement()
t.tracer(1)

shapeT.showturtle()
obstacle.hideturtle()
food.hideturtle()
t.hideturtle()
score_t.clear()


def restartGame(x, y):
runGame()


shapeT.onclick(restartGame)
runGame()
t.done()

2.homework

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8
# 第13课作业要求:改进镜面绘图板,点击不同的颜色改变画笔颜色,按下空格键改变背景颜色
import turtle as t1

t1.tracer(0)
t1.pu()
t1.goto(-300, 300)
t1.seth(0)
t1.pd()
for i in range(4):
t1.forward(600)
t1.right(90)
t1.tracer(1)

t1.pu()
t1.goto(0, 0)
t1.pd()

t2 = t1.Turtle()
t1.color('black')
t2.color('black')


def drawButton(x, color):
t = t1.Turtle()
t.pu()
t.color(color)
t.shape('circle')
t.goto(x, 330)
return t


t3 = drawButton(-300, 'red')
t4 = drawButton(-270, 'yellow')
t5 = drawButton(-240,'blue')


def colorRed(x, y):
t1.color('red')
t2.color('red')


def colorYellow(x, y):
t1.color('yellow')
t2.color('yellow')


def colorBlue(x, y):
t1.color('blue')
t2.color('blue')


def drawing(x, y):
if (x >= -300 and x <= 300) and (y >= -300 and y <= 300):
t1.goto(x, y)
t2.goto(-x, y)


t3.onclick(colorRed)
t4.onclick(colorYellow)
t5.onclick(colorBlue)
t1.onscreenclick(drawing)


t1.listen()

t1.done()