Invent Your Own Computer Games with Python(第二版)(第一章(中))

本书通过游戏源代码教授Python编程,读者可在IDLE环境中运行示例代码并理解其工作原理。每章以特色程序开篇,复杂游戏代码可供下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一章(中)

开始Python之旅

  如果你的操作系统是windows XP,你可以从开始->程序->python3.1->IDLE(Python GUI)启动python。当你打开python后你就会看到如下图1-2所示的界面

  IDLEInteractive DeveLopment Environment的缩写。我们将用IDLE来编写和运行Python程序。

  第一次运行IDLE你所看到的窗口叫做交互式shellshell是一个能让你输入计算机指令的程序。Python shell就是一个让你输入python指令的程序。Shell将这些python指令传给一个叫python解释器的软件来执行。你可以在shell中输入python指令,因为shell是交互式的,电脑会读到我们的指令然后做出相应的反应。

怎样使用本书

  在开始之前你需要明白几点,本书不同于其他的编程书籍,因为它注重的是通过一些游戏的源代码来是你理解。而不像其他编程书那样通过介绍一些概念来理解游戏程序是怎样运行的。

特色程序

  大部分章节都是以一个特色的程序开始,这些简单的程序将让你看到程序是怎样运行的,需要用户输入的以黑体显示。

  也有部分章节是比较复杂的游戏代码,但是请记住:你不需要一行一行的输入这些代码,你可以先阅读它们,当你理解每行代码后再输入。

  你也可以从这个网站(http://inventwithpython.com/source)上下载源代码。

行号和空格

  当你输代码的时候你不要输入出现在每行前边的行号。例如,你会看到这样的例子。

      

9. number = random.randint(1, 20)

但你自己输入的时候,你不要输入‘9.’和它们后边的空格。只要这样输入就可以了:

number = random.randint(1, 20)

这些数字只是用来做标记的,不是程序的一部分。

(本部分未完待续。。。)

Who is this book for? ?Anyone who wants to teach themselves computer programming, even if they have no previous experience programming. ?Kids and teenagers who want to learn computer programming by programming games. Kids as young as 9 or 10 years old should be able to follow along. ?Adults and teachers who wish to teach others programming. ?Programmers who want to teach others "real" programming by example. This book is available for free under a Attribution/Share-Alike Creative Commons license. You can make as many copies of it as you like, as long as credit to the author is left in. The Python programming language software this book teaches is also freely available from www.python.org. Table of Contents Chapter 1 - "Hello World!" - Your First Program x Hello! x Downloading and Installing Python x Starting the Python Interpreter x Some Simple Math Stuff x Evaluating Expressions x Variables x Strings x Writing Programs x Hello World! x The Difference Between Statements and Expressions x "My Favorite Stuff" x Crazy Answers and Crazy Names for our Favorite Stuff x Capitalizing our Variables x Chapter 2 - Guess the Number x Source Code x Arguments x Blocks x Conditions and Booleans x if Statements x Step by Step, One More Time x Some Changes We Could Make x What Exactly is Programming? x A Web Page for Program Tracing x Chapter 3 - Jokes x How Programs Run on Computers x Source Code x Some Other Escape Characters x Quotes and Double Quotes x Chapter 4 - Dragon Realm x Source Code x def Statements x Boolean Operators x Variable Scope x Parameters x Local Variables and Global Variables with the Same Name x Where to Put Function Defintions x The Colon : x Step by Step, One More Time x Designing the Program x A Web Page for Program Tracing x Chapter 5 - Hangman x ASCII Art x Source Code x Designing the Program x Multi-line Strings x Constant Variables x Lists x Changing the Values of List Items with Index Assignment x List Concatenation x The in Operator x Removing Items from Lists with del Statements x Lists of Lists x Methods x The len() Function x The range() Function x for Loops x Strings Act Like Lists x List Slicing and Substrings x elif ("Else If") Statements x And that's it! x Dictionaries x Sets of Words for Hangman x Chapter 6 - Tic Tac Toe x Source Code x Designing the Program x Game AI x List References x Short-Circuit Evaluation x The None Value x A Web Page for Program Tracing x Chapter 7 - Bagels x Source Code x Augmented Assignment Operators x The sort() List Method x The join() String Method x String Interpolation x Chapter 8 - Sonar x Grids and Cartesian Coordinates x Negative Numbers x Changing the Signs x Absolute Values x Coordinate System of a Computer Monitor x Source Code x Designing the Program x The remove() List Method x Chapter 9 - Caesar Cipher x About Cryptography x ASCII, and Using Numbers for Letters x The chr() and ord() Functions x Source Code x The isalpha() String Method x The isupper() and islower() String Methods x Cryptanalysis x Brute Force x Chapter 10 - Reversi x How to Play Reversi x Source Code x The bool() Function x The random.shuffle() Function x Tips for Inventing Your Own Games x Chapter 11 - AI Simulation x "Computer vs. Computer" Games x Percentages x Integer Division x The round() Function x Learning New Things by Running Simulation Experiments x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值