# 课程E-函数1-一个超人的简单函数

• 年龄 9+
• 65+ 分钟
• MakeCode Editor

## 介绍

``````
let KilometersRan = 0
input.onButtonPressed(Button.A, () => {
Run_FasterThan_A_Speeding_Bullet()
Run_FasterThan_A_Speeding_Bullet()
Run_FasterThan_A_Speeding_Bullet()
Run_FasterThan_A_Speeding_Bullet()
Run_FasterThan_A_Speeding_Bullet()
})
input.onButtonPressed(Button.B, () => {
KilometersRan = 5
basic.showNumber(KilometersRan)
basic.clearScreen()
basic.pause(100)
KilometersRan = 5
basic.showNumber(KilometersRan)
basic.clearScreen()
basic.pause(100)
KilometersRan = 5
basic.showNumber(KilometersRan)
basic.clearScreen()
basic.pause(100)
KilometersRan = 5
basic.showNumber(KilometersRan)
basic.clearScreen()
basic.pause(100)
KilometersRan = 5
basic.showNumber(KilometersRan)
basic.clearScreen()
basic.pause(100)
})
function Run_FasterThan_A_Speeding_Bullet()  {
KilometersRan = 5
basic.showNumber(KilometersRan)
basic.clearScreen()
basic.pause(100)
}``````

1. 首先要注重演示。学生们大多数都在看和听。（初始代码解释/挑战1）
2. 老师在学生们的帮助下继续完成大部分课程的讲解。老师对于这些帮助给出快速的反馈。（挑战2-独立练习）

### 背景知识

• 提前体验起点
• 课程E. CS基础。 课程简要地回顾了一下之前在课程C和D之中讲授的概念。学生们将会用算法、循环、条件、事件和函数来进行编程练习。

## 课堂活动

• 1块micro:bit
• 1根USB线
• 1个AAA电池组（可选）
• 2节AAA电池（可选）

### 编写文本

``````
function doSomething()  {
basic.showString("Hello!")
}

doSomething()
``````

### 函数

``basic.showIcon(IconNames.Heart)``

### showIcon

``````
input.onGesture(Gesture.Shake, () => {

})
``````

### onGesture

``````
input.onButtonPressed(Button.A, () => {

})
``````

4个步骤

## 指导练习

``````let KeepRunning = false
let KilometersRan = 0
let XRunningPosition = 0
let YRunningPosition = 0
input.onButtonPressed(Button.A, () => {
Run_FasterThan_A_Speeding_Bullet()
})
function Run_FasterThan_A_Speeding_Bullet()  {
KilometersRan = 0
KeepRunning = true
XRunningPosition = 0
YRunningPosition = 0
for (let YRunningPosition = 0; YRunningPosition <= 5; YRunningPosition++) {
for (let XRunningPosition = 0; XRunningPosition <= 5; XRunningPosition++) {
led.plot(XRunningPosition, YRunningPosition)
led.unplot(XRunningPosition - 1, YRunningPosition - 1)
basic.pause(70)
}
}
}
input.onButtonPressed(Button.B, () => {
MagicUsedToConjure()
})
function Object_Your_Superhero_Can_Conjure()  {

}
function MagicUsedToConjure()  {
for (let index = 0; index <= 4; index++) {
Object_Your_Superhero_Can_Conjure()
basic.clearScreen()
basic.pause(150)
}
}``````

## 简单函数的挑战

### 挑战1

``````let YRunningPosition = 0
let XRunningPosition = 0
let KeepRunning = false
let KilometersRan = 0
input.onButtonPressed(Button.B, () => {
MagicUsedToConjure()
})
function MagicUsedToConjure()  {
for (let index = 0; index <= 4; index++) {
Object_Your_Superhero_Can_Conjure()
basic.clearScreen()
basic.pause(150)
}
}
function Object_Your_Superhero_Can_Conjure()  {
basic.showIcon(IconNames.Ghost)
}
input.onButtonPressed(Button.A, () => {
Run_FasterThan_A_Speeding_Bullet()
})
function Run_FasterThan_A_Speeding_Bullet()  {
KilometersRan = 0
KeepRunning = true
XRunningPosition = 0
YRunningPosition = 0
for (let YRunningPosition2 = 0; YRunningPosition2 <= 5; YRunningPosition2++) {
for (let XRunningPosition2 = 0; XRunningPosition2 <= 5; XRunningPosition2++) {
led.plot(XRunningPosition2, YRunningPosition2)
led.unplot(XRunningPosition2 - 1, YRunningPosition2 - 1)
basic.pause(70)
}
}
}
input.onGesture(Gesture.Shake, () => {

})``````

### 挑战2

``````let YRunningPosition = 0
let XRunningPosition = 0
let KeepRunning = false
let KilometersRan = 0
input.onButtonPressed(Button.B, () => {
MagicUsedToConjure()
})
function MagicUsedToConjure()  {
for (let index = 0; index <= 4; index++) {
Object_Your_Superhero_Can_Conjure()
basic.clearScreen()
basic.pause(150)
}
}
function Object_Your_Superhero_Can_Conjure()  {
basic.showIcon(IconNames.Ghost)
}
input.onButtonPressed(Button.A, () => {
Run_FasterThan_A_Speeding_Bullet()
})
function Run_FasterThan_A_Speeding_Bullet()  {
KilometersRan = 0
KeepRunning = true
XRunningPosition = 0
YRunningPosition = 0
for (let YRunningPosition2 = 0; YRunningPosition2 <= 5; YRunningPosition2++) {
for (let XRunningPosition2 = 0; XRunningPosition2 <= 5; XRunningPosition2++) {
led.plot(XRunningPosition2, YRunningPosition2)
led.unplot(XRunningPosition2 - 1, YRunningPosition2 - 1)
basic.pause(70)
}
}
}
input.onGesture(Gesture.Shake, () => {
basic.showIcon(IconNames.Surprised)
})``````

## 问答

### 问题 1

1. 函数让我们能够重用代码而无须重新输入，这样代码就会更加精简一些。
2. 如果我们需要作出一些改变，我们只需要在一个地方改动代码就可以了。
3. 函数能够让我们更好地组织代码。

### 问题 2

``````摇晃

### 问题 3

``````游戏控制函数

``````制作冰淇淋锥函数