这是用户在 2024-9-18 23:27 为 https://cs50.harvard.edu/x/2024/notes/0/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Lecture 0 第 0 讲

Welcome! 欢迎您!

  • This class is about more than computer programming!
    这门课不仅仅是关于计算机编程的!
  • Indeed, this class is about problem-solving in a way that is exceedingly empowering! You will likely take the problem solving that you learn here will likely be instantly applicable to your work beyond this course and even your career as a whole!
    事实上,这门课是关于以极具赋权感的方式解决问题!您在这里学到的解决问题的方法很可能会立刻适用到您课程之外的工作,甚至是整个职业生涯!
  • However, it will not be easy! You will be “drinking from the firehose” of knowledge during this course. You’ll be amazed at what you will be able to accomplish in the coming weeks.
    不过,这将是一项挑战!在这门课程中,你将像“从喷口中喝水”一样汲取知识。你会惊讶于在接下来的几周里你能够取得的成就。
  • This course is far more about you advancing “you” from “where you are today” than hitting some imagined standard.
    这门课程更关注的是如何让你从“今天的自己”不断进步,而不是追求某种理想的标准。
  • The most important opening consideration in this course: Give the time you need to learn through this course. Everyone learns differently. If something does not work out well at the start, know that with time you will grow and grow in your skill.
    本课程中最重要的考虑是:给自己足够的时间来学习这门课程。每个人的学习方式都不同。如果一开始遇到困难,请记住,随着时间的流逝,你的技能会不断提高。
  • Don’t be scared if this is your first computer science class! For most of your peers, this is their first computer science class too!
    如果这是你第一次上计算机科学课,不要感到害怕!对于大多数同学来说,这也是他们的第一次计算机科学课!

What’s Ahead 未来会发生什么

  • You will be learning this week about Scratch, a visual programming language.
    你将学习 Scratch,这是一种可视化编程语言。
  • Then, in future weeks, you will learn about C. That will look something like this:
    之后,在接下来的几周内,你将会学习 C 语言。它看起来会是这样的:

    #include <stdio.h>
    
    int main(void)
    {
      printf("hello, world\n");
    }
    
  • Further, as the weeks progress, you will learn about algorithms.
    此外,随着时间的推移,你将会学习到各种算法。
  • You will learn about memory.
    你将学习关于记忆的内容。
  • You will learn about buggy code and what causes computer crashes.
    你将学习有关有问题的代码以及导致计算机崩溃的原因。
  • You will learn about data structures such as a hash table.
    你将学习诸如哈希表等数据结构。
  • Then, we will transition to a new, higher-level language called Python. Your code will look something like this:
    然后,我们将切换到一种名为 Python 的新高级语言。你的代码大致如下:

    print("hello, world")
    
  • This class will give you a strong understanding of how recent programming languages developed from the earlier ones.
    这门课程将帮助你深入理解现代编程语言是如何从早期语言演变而来的。
  • You will learn SQL, JavaScript, HTML, and CSS.
    你将学习 SQL、JavaScript、HTML 和 CSS 编程语言。
  • We will also be looking at how we can use databases and third-party frameworks to build web applications.
    我们还将探讨如何利用数据库和第三方框架来构建 web 应用。

Community! 社区!

  • You are part of a community of those taking this course at Harvard College, Harvard Extension School, and via edX.org.
    您是哈佛学院、哈佛扩展学院和 edX.org 上参加本课程的社区成员之一。
  • Puzzle Day and the CS50 Fair
    拼图日与 CS50 嘉年华
  • You can attend CS50 Lunches and CS50 Hackathon, if you are student on Harvard’s campus.
    如果你是哈佛大学校园的学生,可以参加 CS50 午餐和 CS50 黑客马拉松。

Computer Science 电脑科学

  • Essentially, computer programming is about taking some input and creating some output - thus solving a problem. What happens in between the input and output, what we could call a black box, is the focus of this course.
    本质上,计算机编程就是接收输入并生成输出,以此来解决问题。输入和输出之间的过程,我们称之为黑箱,这是本课程的重点。

    Black box with input and output

  • For example, we may need to take attendance for a class. We could use a system called unary to count, one finger at a time.
    例如,我们可能需要为一个班级点名。我们可以使用一元制计数法,每次用一根手指来计数。
  • Computers today count using a system called binary. It’s from the term binary digit that we get a familiar term called bit. A bit is a zero or one: on or off.
    现在的计算机使用一种称为二进制的系统。这个术语源于“二进制数字”,由此我们得到了一个常用的词“比特”。比特只有两种状态:0 或 1,表示开或关。
  • Computers only speak in terms of zeros and ones. Zeros represent off. Ones represent on. Computers are millions, and perhaps billions, of transistors that are being turned on and off.
    计算机只用零和一进行交流。零代表关闭,壹代表开启。计算机由数百万,甚至数十亿个晶体管组成,这些晶体管在不断被打开和关闭。
  • If you imagine using a light bulb, a single bulb can only count from zero to one.
    如果你想象用一个灯泡,一个灯泡只能从零数到一。
  • However, if you were to have three light bulbs, there are more options open to you!
    但是,如果你有三个灯泡,你就有更多选择!
  • Using three light bulbs, the following could represent zero:
    利用三个灯泡,以下内容可以表示为零:

    0 0 0
    
  • Similarly, the following would represent one:
    同样地,以下将代表一个例子:

    0 0 1
    
  • By this logic, we could propose that the following equals two:
    根据这个逻辑,我们可以提出以下等式:二

    0 1 0
    
  • Extending this logic further, the following represents three:
    进一步扩展此逻辑,以下代表三个:

    0 1 1
    
  • Four would appear as:
    四会呈现为:

    1 0 0
    
  • We could, in fact, using only three light bulbs count as high as seven!
    事实上,我们只使用三个灯泡就能够数到七!

    1 1 1
    
  • As a heuristic, we could imagine that the following values represent each possible place in our binary digit:
    作为一种启发式方法,我们可以设想以下值代表二进制数字中每个可能的位数位置:

    4 2 1
    
  • Computers use ‘base-2’ to count. This can be pictured as follows:
    计算机使用“二进制”进行计数。可以这样理解:

    2^2  2^1  2^0
    4    2    1
    
  • Therefore, you could say that it would require three bits (the four’s place, the two’s place, and the one’s place) to represent a number as high as seven.
    因此,我们可以说需要三个比特位(分别代表四位、两位和一位)来表示高达七的数字。
  • Computers generally use eight bits (also known as a byte) to represent a number. For example, 00000101 is the number 5 in binary. 11111111 represents the number 255.
    计算机通常使用八位(也称为字节)来表示一个数字。例如, 00000101 在二进制中表示数字 5。 11111111 表示数字 255。

ASCII ASCII 码

  • Just as numbers are binary patterns of ones and zeros, letters are represented using ones and zeros too!
    就像数字是由 1 和 0 组成的二进制模式一样,字母也用 1 和 0 表示!
  • Since there is an overlap between the ones and zeros that represent numbers and letters, the ASCII standard was created to map specific letters to specific numbers.
    由于表示数字和字母的 0 和 1 之间存在重叠,ASCII 标准应运而生,用于将特定字母映射到特定数字。
  • For example, the letter A was decided to map to the number 65. 01000001 represents the number 65 in binary.
    例如,字母 A 被决定映射到数字 65。 01000001 代表 65 的二进制表示。
  • If you received a text message, the binary under that message might represent the numbers 72, 73, and 33. Mapping these out to ASCII, your message would look as follows:
    如果你收到了一条短信,短信中的二进制代码可能代表数字 72、73 和 33。将这些数字转换为 ASCII 码,你的短信内容将会是:

    H   I   !
    72  73  33
    
  • Thank goodness for standards like ASCII that allow us to agree upon these values!
    感谢像 ASCII 这样的标准,它让我们能够对这些值达成一致!
  • Here is an expanded map of ASCII values:
    这是一个详细的 ASCII 值图表:

    ASCII map

  • If you wish, you can learn more about ASCII.
    如果你想,可以了解更多有关 ASCII 的知识。
  • Since binary can only count up to 255 we are limited to the number of characters represented by ASCII.
    由于二进制只能表示到 255,我们所能使用的字符数量受到了 ASCII 的限制。

Unicode 字符编码

  • As time has rolled on, there are more and more ways to communicate via text.
    随着时间的推移,人们通过文本交流的方式越来越多。
  • Since there were not enough digits in binary to represent all the various characters that could be represented by humans, the Unicode standard expanded the number of bits that can be transmitted and understood by computers. Unicode includes not only special characters, but emoji as well.
    由于二进制表示所有人类可以表示的各种字符的位数不够,Unicode 标准扩展了计算机可以传输和理解的位数。Unicode 不仅包含特殊字符,还包含表情符号。
  • There are emoji that you probably use every day. The following may look familiar to you:
    你可能每天都会用到一些表情符号。以下这些你可能很熟悉:

    emoji

  • Computer scientists faced a challenge when wanting to assign various skin tones to each emoji to allow the communication to be further personalized. In this case, the creators and contributors of emoji decided that the initial bits would be the structure of the emoji itself, followed by skin tone.
    计算机科学家们面临着一个挑战,他们希望为每个表情符号分配不同的肤色,以进一步个性化沟通。在这种情况下,表情符号的创作者和贡献者们决定,表情符号的初始部分将是其基本结构,而肤色则是跟随其后的元素。
  • For example, the unicode for a generic thumbs up is U+1F44D. However, the following represents the same thumbs up with a different skin tone: U+1F44D U+1F3FD.
    例如,通用的点赞符号的 Unicode 编码是 U+1F44D 。不过,下面这个符号代表的是同样的点赞,但肤色有所不同: U+1F44D U+1F3FD
  • More and more features are being added to the Unicode standard to represent further characters and emoji.
    Unicode 标准不断添加新功能,以支持更多字符和表情符号。
  • If you wish, you can learn more about Unicode.
    如果你想的话,可以进一步了解 Unicode 的相关知识。
  • If you wish, you can learn more about emoji.
    如果你想的话,可以了解更多关于表情符号的内容。

Representation 表达方式

  • Zeros and ones can be used to represent color.
    二进制数字可以用来表示颜色。
  • Red, green, and blue (called RGB) is a combination of three numbers.
    红色、绿色和蓝色(称为 RGB )是由三种数字组合而成的。

    red green blue boxes

  • Taking our previously used 72, 73, and 33, which said HI! via text, would be interpreted by image readers as a light shade of yellow. The red value would be 72, the green value would be 73, and the blue would be 33.
    如果我们使用之前用过的 72、73 和 33,用文本表示为 HI! ,图像阅读器会将其解释为浅黄色。其中红色值为 72,绿色值为 73,蓝色值为 33。

    yellow box

  • Further, zeros and ones can be used to represent images, videos, and music!
    此外,零和一可以用来表示图像、视频和音乐!
  • Images are simply collections of RGB values.
    图像只是 RGB 值的集合。
  • Videos are sequences of many images that are stored together, just like a flipbook.
    视频就像一本翻页书,是由许多图片按顺序排列而成的。
  • Music can be represented through MIDI data.
    音乐可以用 MIDI 数据进行表示。

Algorithms 算法学

  • Problem-solving is central to computer science and computer programming.
    解决问题是计算机科学和计算机编程的中心环节。
  • Imagine the basic problem of trying to locate a single name in a phone book.
    试想在电话簿中寻找一个名字的基本问题。
  • How might you go about this?
    你会如何去做这个?
  • One approach could be to simply read from page one to the next to the next until reaching the last page.
    一种方法可以是从第一页开始,依次阅读到最后一页。
  • Another approach could be to search two pages at a time.
    另一种方法是同时搜索两个页面。
  • A final and perhaps better approach could be to go to the middle of the phone book and ask, “Is the name I am looking for to the left or to the right?” Then, repeat this process, cutting the problem in half and half and half.
    一种最终可能更好的方法是去电话簿的中间问:“我在找的名字是在左边还是右边?”然后重复这个过程,将问题逐步减半。
  • Each of these approaches could be called algorithms. The speed of each of these algorithms can be pictured as follows in what is called big-O notation:
    这些方法都可以称为算法。它们的速度可以通过所谓的大 O 表示法来描述:

    big o notation

    Notice that the first algorithm, highlighted in red, has a big-O of n because if there are 100 names in the phone book, it could take up to 100 tries to find the correct name. The second algorithm, where two pages were searched at a time, has a big-O of ‘n/2’ because we searched twice as fast through the pages. The final algorithm has a big-O of log2n as doubling the problem would only result in one more step to solve the problem.
    请注意,第一个算法(以红色突出显示)的 Big-O 为 n ,因为如果电话簿中有 100 个姓名,则可能需要最多 100 次尝试才能找到正确的姓名。第二个算法(每次搜索两页)的 Big-O 为 'n/2',因为我们搜索速度是原来的两倍。最后一个算法的 Big-O 为 log2n,因为将问题规模扩大一倍只会导致多一步来解决问题。

Pseudocode 伪代码

  • The ability to create pseudocode is central to one’s success in both this class and in computer programming.
    能够编写伪代码是你在这门课和计算机编程中取得成功的关键。
  • Pseudocode is a human-readable version of your code. For example, considering the third algorithm above, we could compose pseudocode as follows:
    伪代码是您代码的人类可读版本。例如,考虑上述第三个算法,我们可以将其伪代码编写如下:

    1  Pick up phone book
    2  Open to middle of phone book
    3  Look at page
    4  If person is on page
    5      Call person
    6  Else if person is earlier in book
    7      Open to middle of left half of book
    8      Go back to line 3
    9  Else if person is later in book
    10     Open to middle of right half of book
    11     Go back to line 3
    12 Else
    13     Quit
    
  • Pseudocoding is such an important skill for at least two reasons. First, when you pseudocode before you create formal code, it allows you to think through the logic of your problem in advance. Second, when you pseudocode, you can later provide this information to others that are seeking to understand your coding decisions and how your code works.
    伪代码是一项至关重要的技能,主要有两个原因。首先,在编写正式代码之前,通过编写伪代码可以提前思考问题的逻辑。其次,编写伪代码后,你可以将这些信息提供给其他人,帮助他们理解你的编码决策和代码的工作原理。
  • Notice that the language within our pseudocode has some unique features. First, some of these lines begin with verbs like pick up, open, look at. Later, we will call these functions.
    请注意,我们伪代码中的语言有一些独特的特点。首先,这些行的开头使用了动词,比如“拾起”、“打开”、“查看”。稍后我们会将这些称为函数。
  • Second, notice that some lines include statements like if or else if. These are called conditionals.
    其次,请注意有些行包含像 ifelse if. 这样的语句。这些被称为条件语句。
  • Third, notice how there are expressions that can be stated as true or false, such as “person is earlier in the book.” We call these boolean expressions.
    第三,请注意,有些表达可以判断真假,例如“人物在书中出现得更早”。我们称这些表达式为布尔表达式。
  • Finally, notice how these statements like “go back to line 3.” We call these loops.
    最后,请注意这些表达,比如“回到第三行”。我们称这些为循环。
  • These building blocks are the fundamentals of programming.
    这些构件是编程的基础。
  • In the context of Scratch, which is discussed below, we will use each of the above basic building blocks of programming.
    在下面将要讨论的 Scratch 环境中,我们将使用上述所有基本的编程模块。

Artificial Intelligence 人工智能

  • Consider how we can utilize the building blocks above to start creating our own artificial intelligence. Look at the following pseudocode:
    思考我们如何利用上述构建模块来开始创建自己的人工智能。请查看以下伪代码:

    If student says hello
        Say hello back
    Else if student says goodbye
        Say goodbye back
    Else if student asks how you are
        Say you're well
    Else if student asks why 111 in binary is 7 in decimal
    ...
    

    Notice how just to program a handful of interactions, many lines of code would be required. How many lines of code would be required for thousands or tens of thousands of possible interactions?
    请注意,仅编写少量的交互就需要很多行代码,那么对于成千上万或数万种可能的交互,需要多少行代码呢?

  • large language models look at patterns in large blocks of language. Such language models attempt to create a best guess of what words come after one another or alongside one another.
    large language models 观察大型语言文本中的模式,这些语言模型试图预测下一个或相邻的单词。
  • As very useful in many avenues of life and work, we stipulate that the utilization of AI-based software other than CS50’s own is not reasonable.
    我们认为,人工智能软件在生活和工作中的许多领域都是非常有用的,因此,除了 CS50 自身的工具之外,使用其他人工智能软件是不合理的。
  • CS50’s own AI-based software tool called CS50 Duck is an AI helper that you can use during this course. It will help you, but not give away the entire answers to the course’s problems.
    CS50 独创的人工智能软件工具 CS50 Duck 是您在本课程中可以使用的 AI 助手。它会帮助您,但不会提供课程问题的全部答案。

Scratch 刮擦

  • Scratch is a visual programming language developed by MIT.
    Scratch 是一款由麻省理工学院开发的图形化编程语言。
  • Scratch utilizes the same essential coding building blocks that we covered earlier in this lecture.
    Scratch 使用了我们在本讲座前面介绍过的相同的基本编码构建块。
  • Scratch is a great way to get into computer programming because it allows you to play with these building blocks in a visual manner, not having to be concerned about the syntax of curly braces, semicolons, parentheses, and the like.
    Scratch 是一个很好的计算机编程入门方式,因为它允许你以可视化的方式进行创作,不必担心大括号、分号、括号等的语法。
  • Scratch IDE (integrated development environment) looks like the following:
    Scratch IDE (集成开发环境)看起来是这样的:

    scratch interface

    Notice that on the left, there are building blocks that you can use in your programming. To the immediate right of the building blocks, there is the area to which you can drag blocks to build a program. To the right of that, you see the stage where a cat stands. The stage is where your programming comes to life.
    请注意,左侧是您可以用于编程的积木块。在积木块的右侧是程序构建区域,您可以将积木块拖动到那里。再往右,您会看到一个猫站在舞台上,这就是您的程序变为现实的地方。

  • Scratch operates on a coordinate system as follows:
    Scratch 使用以下坐标系:

    scratch coordinate system

    Notice that the center of the stage is at coordinate (0,0). Right now, the cat’s position is at that same position.
    请注意舞台中心位于坐标 (0,0) 处。目前,猫的位置也位于该坐标处。

Hello World 您好,世界

  • To begin, drag the “when green flag clicked” building block to the programming area. Then, drag the say building block to the programming area and attach it to the previous block.
    首先,将“当点击绿色旗帜时”这个块拖到编程区域。接着,将 say 这个块拖到编程区域,并将其连接到前一个块。

    whenclickedsayhello, world

    Notice that when you click the green flag now, on the stage, the cat says, “hello world.”
    请注意,当你点击舞台上的绿色旗帜时,猫会说:“你好,世界。”

  • This illustrates quite well what we were discussing earlier regarding programming:
    这很好地展示了我们之前讨论的编程相关内容:

    scratch with black box

    Notice that the input hello world is passed to the function say, and the side effect of that function running is the cat saying hello world.
    注意输入 hello world 被传递给函数 say ,运行该函数的副作用是猫说 hello world

Hello, You 你好

  • We can make your program more interactive by having the cat say hello to someone specific. Modify your program as below:
    我们可以通过让猫对某个特定的人说 hello 来让你的程序更具互动性。请按照以下方式修改你的程序:

    whenclickedaskWhat's your name?andwaitsayjoinhello,answer

    Notice that when the green flag is clicked, the function ask is run. The program prompts you, the user, What's your name? It then stores that name in the variable called answer. The program then passes answer to a special function called join, which combines two strings of text hello, and whatever name was provided. These collectively are passed to the say function. The cat says, Hello, and a name. Your program is now interactive.
    注意,当点击绿色旗帜时,函数 ask 会被调用。程序会提示你,作为用户, What's your name? 然后将这个名字存储在名为 answer 的变量中。程序接着将 answer 传递给一个名为 join 的特殊函数,它将两个字符串 hello 与提供的名字结合起来。这些组合最终会传递给 say 函数。猫说: Hello, 和一个名字。现在,你的程序已经变得互动起来。

  • Quite similarly, we can modify our program as follows:
    我们可以以类似的方式修改我们的程序,如下所示:

    whenclickedaskWhat's your name?andwaitspeakjoinhello,answer

    Notice that this program, when the green flag is clicked, passes the same variable, joined with hello, to a function called speak.
    请注意,当您点击绿色标志时,此程序会将相同的变量与 hello 连接起来,并传递给一个名为 speak 的函数。

Meow and Abstraction 喵与抽象艺术

  • Along with pseudocoding, abstraction is an essential skill and concept within computer programming.
    除了伪代码之外,抽象在计算机编程中是一个必不可少的技能和概念。
  • Abstraction is the act of simplifying a problem into smaller and smaller problems.
    抽象是指将一个复杂的问题分解成更小、更易于解决的子问题。
  • For example, if you were hosting a huge dinner for your friends, the problem of having to cook the entire meal could be quite overwhelming! However, if you break down the task of cooking the meal into smaller and smaller tasks (or problems), the big task of creating this delicious meal might feel less challenging.
    例如,如果你要为朋友们举办一场大型晚宴,烹饪所有菜肴可能会让你感到压力很大。但是,如果你将烹饪任务分解成更小的步骤,那么完成这顿美味大餐的任务就会变得更容易。
  • In programming, and even within Scratch, we can see abstraction in action. In your programming area, program as follows:
    在编程中,甚至在 Scratch 中,我们可以看到抽象概念的实际应用。在你的编程区域中,可以按照以下方式进行编程:

    whenclickedplaysoundMeowuntildonewait1secondsplaysoundMeowuntildonewait1secondsplaysoundMeowuntildone

    Notice that you are doing the same thing over and over again. Indeed, if you see yourself repeatedly coding the same statements, it’s likely the case that you could program more artfully – abstracting away this repetitive code.
    你一直在重复做同样的事情,注意到了吗?如果发现自己反复编写相同的代码,就说明可以更巧妙地抽象这些重复部分,让代码更简洁。

  • You can modify your code as follows:
    你可以按照以下方式修改你的代码:

    whenclickedrepeat3playsoundMeowuntildonewait1seconds

    Notice that the loop does exactly as the previous program did. However, the problem is simplified by abstracting away the repetition to a block that repeats the code for us.
    请注意,该循环的功能与之前的程序完全相同。然而,通过将重复代码抽象成一个代码块,问题得到了简化。

  • We can even advance this further by using the define block, where you can create your own block (your own function)! Write code as follows:
    我们甚至可以通过使用 define 块来进一步推进,您可以自己创建一个块(即您自己的函数)!请按照以下方式编写代码:

    definemeowplaysoundMeowuntildonewait1secondswhenclickedrepeat3meow

    Notice that we are defining our own block called meow. The function plays the sound meow, then waits one second. Below that, you can see that when the green flag is clicked, our meow function is repeated three times.
    请注意,我们正在定义一个名为 meow 的自定义代码块。该代码块会播放声音 meow ,然后等待一秒钟。在下方,您可以看到当点击绿色旗帜时,我们的喵喵函数会重复执行三次。

  • We can even provide a way by which the function can take an input n and repeat a number of times:
    我们甚至可以提供一种方法,让函数可以接收一个输入 n ,并重复执行多次

    definemeowntimesrepeatnplaysoundmeowuntildonewait1seconds

    Notice how n is taken from “meow n times.” n is passed to the meow function through the define block.
    请注意, n 是从“喵几次”中提取的。 n 通过 define 块传递给喵函数。

  • The cat, by the way, we can call a sprite – a general term used in game programming for an object or character on the screen with which the player will interact.
    顺便提一下,我们可以把猫称作 sprite ——这是游戏编程中用于指屏幕上玩家将要互动的对象或角色的通用术语。

Conditionals 条件语句

  • conditionals are an essential building block of programming, where the program looks to see if a specific condition has been met. If a condition is met, the program does something.
    条件语句是编程的基础,程序会检查是否满足某个特定条件。如果条件满足,程序就会执行相应的操作。
  • To illustrate a conditional, write code as follows:
    要演示条件语句,请编写以下代码:

    whenclickedforeveriftouchingmouse-pointer?thenplaysoundMeowuntildone

    Notice that the forever block is utilized such that the if block is triggered over and over again, such that it can check continuously if the cat is touching the mouse pointer.
    注意 forever 块的运用,使得 if 块不断被触发,从而可以持续检测猫是否触碰鼠标指针。

  • We can modify our program as follows to integrate video sensing:
    为了集成视频感知,我们可以对程序进行如下修改

    whenvideomotion>50playsoundMeowuntildone
  • Remember, programming is often a process of trial and error. If you get frustrated, take time to talk yourself through the problem at hand. What is the specific problem that you are working on right now? What is working? What is not working?
    记住,编程经常需要反复尝试。如果你感到沮丧,花点时间思考一下你遇到的问题。你具体遇到了什么问题?哪些部分有效?哪些部分无效?

Oscartime 奥斯卡颁奖时间

  • We showed you in this lecture a number of Scratch programs to stoke your imagination.
    在这节课中,我们展示了一些 Scratch 程序,以激发您的创造力。
  • Oscartime is one of David’s own Scratch programs – though the music may haunt him because of the number of hours he listened to it while creating this program. Take a few moments to play through the game yourself.
    Oscartime 是大卫用 Scratch 自己编写的程序之一,尽管他在创作这个程序时听了无数遍音乐,但这些音乐可能仍然会萦绕在他的脑海中。花点时间自己玩一下这个游戏吧。
  • Building Oscartime ourselves, we first add the lamp post.
    我们自己搭建奥斯卡时间,首先要添加路灯。

    oscartime interface

  • Then, write code as follows:
    然后,请按照以下方式编写代码:

    whenclickedswitchcostumetooscar1foreveriftouchingmouse-pointer?thenswitchcostumetooscar2elseswitchcostumetooscar1

    Notice that moving your mouse over Oscar changes his costume. You can learn more by exploring these code blocks.
    注意,将鼠标悬停在奥斯卡身上会改变他的服装。您可以通过探索这些代码块来了解更多信息。

  • Then, modify your code as follow to create a falling piece of trash:
    然后,按照以下方式修改您的代码,以创建一个掉落的垃圾块

    whenclickedgotox:pickrandom-240to240y:180foreverifdistancetofloor>0thenchangeyby-3

    Notice that the trash’s position on the y-axis always begins at 180. The x position is randomized. While the trash is above the floor, it goes down 3 pixels at a time. You can learn more by exploring these code blocks.
    请注意,垃圾在 Y 轴上的初始位置始终为 180。X 轴位置则是随机的。当垃圾位于地板上方时,它会以每次下降 3 像素的速度向下移动。你可以通过探索这些代码块来了解更多信息。

  • Next, modify your code as follows to allow for the possibility of dragging trash.
    接下来,请按以下方式修改代码,以便支持拖动垃圾。

    whenclickedforeverifmousedown?andtouchingmouse-pointer?thengotomouse-pointer

    You can learn more by exploring these code blocks.
    您可以通过探索这些代码块来了解更多信息。

  • Next, we can implement the scoring variables as follows:
    接下来,我们可以按照以下方式实现评分变量:

    whenclickedforeveriftouchingOscar?thenchangescoreby1gotox:pickrandom-240to240y:180

    You can learn more by exploring these code blocks.
    您可以通过探索这些代码块来了解更多信息。

  • Go try the full game Oscartime.
    去体验完整的游戏《奥斯卡时间》。

Ivy’s Hardest Game 艾薇的超级难游戏

  • Moving away from Oscartime to Ivy’s Hardest Game, we can now imagine how to implement movement within our program.
    从 Oscartime 转向 Ivy 的最难游戏,我们现在可以想象如何在我们的程序中实现移动功能。
  • Our program has three main components.
    我们的程序主要由三个部分组成。
  • First, write code as follows:
    首先,请按照以下代码编写:

    whenclickedgotox:0y:0foreverlistenforkeyboardfeelforwalls

    Notice that when the green flag is clicked, our sprite moves to the center of the stage at coordinates (0,0) and then listens for the keyboard and checks for walls forever.
    请注意,当点击绿色旗帜时,我们的角色会移动到舞台中心的坐标 (0,0),然后一直监听键盘并检查是否有墙壁。

  • Second, add this second group of code blocks:
    第二步,添加这组代码块:

    definelistenforkeyboardifkeyup arrowpressed?thenchangeyby1ifkeydown arrowpressed?thenchangeyby-1ifkeyright arrowpressed?thenchangexby1ifkeyleft arrowpressed?thenchangexby-1

    Notice how we have created a custom listen for keyboard script. For each of our arrow keys on the keyboard, it will move the sprite around the screen.
    请注意,我们已经创建了一个自定义的 listen for keyboard 脚本。每次按下键盘上的箭头键时,它都会让精灵在屏幕上移动。

  • Finally, add this group of code blocks:
    最后,添加这组代码块

    definefeelforwallsiftouchingleft wall?thenchangexby1iftouchingright wall?thenchangexby-1

    Notice how we also have a custom feel for walls script. When a sprite touches a wall, it moves it back to a safe position – preventing it from walking off the screen.
    请注意,我们还拥有一个自定义的 feel for walls 脚本。当精灵碰到墙壁时,它会将精灵移回安全位置,以防止它走出屏幕。

  • You can learn more by exploring these code blocks.
    您可以通过探索这些代码块来了解更多信息。
  • Go try the full game Ivy’s Hardest Game.
    去玩玩完整的“艾薇最难的游戏”。
  • Scratch allows for many sprites to be on the screen at once.
    Scratch 可以在屏幕上同时显示多个精灵。
  • Adding another sprite, add the following code blocks to your program:
    要添加另一个精灵,请将以下代码块添加到您的程序中:

    whenclickedgotox:0y:0pointindirection90foreveriftouchingleft wall?ortouchingright wall?thenturn180degreesmove1steps

    Notice how the Yale sprite seems to get in the way of the Harvard sprite by moving back and forth. When it bumps into a wall, it turns around until it bumps the wall again. You can learn more by exploring these code blocks.
    请注意,耶鲁小精灵似乎通过前后移动来干扰哈佛小精灵。当它碰到墙壁时,会转身,直到再次碰到墙壁。您可以通过探索这些代码块来获取更多信息。

  • You can even make a sprite follow another sprite. Adding another sprite, add the following code blocks to your program:
    你甚至可以设置一个精灵跟随另一个精灵。添加另一个精灵后,将以下代码块添加到你的程序中:

    whenclickedgotorandom positionforeverpointtowardsHarvardmove1steps

    Notice how the MIT logo now seems to follow around the Harvard one. You can learn more by exploring these code blocks.
    现在,MIT 的标志似乎围绕着哈佛的标志移动。您可以通过探索这些代码块来了解更多信息。

  • Go try the full game Ivy’s Hardest Game.
    去玩玩完整的“艾薇最难的游戏”。

Summing Up 概括

In this lesson, you learned how this course sits in the wide world of computer science and programming. You learned…
在本节课中,你了解到这门课程在计算机科学和编程广阔领域中的重要性。你学到了…

  • Few students come to this class with prior programming experience!
    很少有学生在上这门课之前具备编程经验!
  • You are not alone! You are part of a community.
    你并不孤单!你是这个社区的一部分。
  • Problem solving is the essence of the work of computer scientists.
    解决问题是计算机科学家工作的核心。
  • This course is not simply about programming – this course will introduce you to a new way of learning that you can apply to almost every area of life.
    这门课程不仅仅是关于编程,它将带你体验一种全新的学习方式,这种方式可以应用到生活的各个方面。
  • How numbers, text, images, music, and video are understood by computers.
    计算机是如何理解数字、文本、图像、音乐和视频的。
  • The fundamental programming skill of pseudocoding.
    假代码是基本的编程技能。
  • Reasonable and unreasonable ways to utilize AI in this course.
    在本课程中合理与不合理地利用人工智能的方法。
  • How abstraction will play a role in your future work in this course.
    抽象将在您今后的课程工作中扮演怎样的角色。
  • The basic building blocks of programming, including functions, conditionals, loops, and variables.
    编程的基本组成部分,包括函数、条件语句、循环和变量。
  • How to build a project in Scratch.
    如何用 Scratch 制作一个项目。

See you next time!
下次见!