python儿童编程谷风参考

上传人:人*** 文档编号:568486618 上传时间:2024-07-24 格式:PPT 页数:72 大小:4.89MB
返回 下载 相关 举报
python儿童编程谷风参考_第1页
第1页 / 共72页
python儿童编程谷风参考_第2页
第2页 / 共72页
python儿童编程谷风参考_第3页
第3页 / 共72页
python儿童编程谷风参考_第4页
第4页 / 共72页
python儿童编程谷风参考_第5页
第5页 / 共72页
点击查看更多>>
资源描述

《python儿童编程谷风参考》由会员分享,可在线阅读,更多相关《python儿童编程谷风参考(72页珍藏版)》请在金锄头文库上搜索。

1、初初级编程程2018/3/172018/3/17并非所有的蛇都会爬行并非所有的蛇都会爬行1谷风书苑第一章开始你将了解 什么是python 在计算机上安装并使用python2谷风书苑1.Python介绍一种计算机语言 高级语言(Java,Vb,Ruby,Python,C等多达上百种)和人类一样,计算机使用多种语言进行交流。一个编程语言只是一种与计算机对话的特殊方式。人类和计算机都能理解的指令。3谷风书苑2.安装Python-1获取安装程序(下载) https:/www.python.org/downloads/windows/ 注意根据操作系统选择下载64或32位版本(可执行文件)l在windo

2、ws下执行安装程序4谷风书苑2.安装Python-2启动python shell(IDLE)这就是Python ShellPython Shell就是在计算机上解释执行python语言的控制台。相当于你的大脑负责解释你和别人所说的话,并按照要求进行动作。5谷风书苑3.和计算机交流吧你告诉计算机的第一句话 print(Hello World) Hello World 让计算机做几道数学题 3 * 52 156 3670 - 156 3514 Symbol Operation+ Addition(加)- Subtraction(减)* Multiplication(乘)/ Division(除)6

3、谷风书苑第二章编程第一步(变量)你将了解 什么是变量? 它能干什么? 如何使用它7谷风书苑4.什么是变量变量(variable) 编程中的变量描述了存储信息的地方。比如数字、文本、数字和文本等等。从另一方面看,变量就像一个标签。 fred = 100 #定义一个变量,并给变量赋值 print(fred) #告诉计算机把变量表示的内容显示出来100 fred = 200 #定义一个变量,并给变量赋值 john = fred #定义另一个变量,并把fred的值赋值给它 print(john)200 found_coins = 20 magic_coins = 10 stolen_coins = 3

4、 found_coins + magic_coins * 2 - stolen_coins * 3318谷风书苑第三章编程第二步(常用数据)你将了解 STRINGS - 字符串 LISTS - 列表 TUPLES - 元组 MAPS - 地图9谷风书苑1.字符串StringString(字符串)在编程术语中,我们通常称文本为字符串。你可以把一个字符串看作字母的集合,本资料里所有的字母、数字和符号都是一串字符。 fred = What is pink and fluffy? Pink fluff! print(fred)What is pink and fluffy? Pink fluff!创造

5、一个字符串,把它放在变量里,让计算机显示出来说明字符串用” 或者 来定义字符串转义符号 , 试着顶一个 IAM COMPUTER10谷风书苑1.字符串String在字符串种嵌入值 myscore = 1000 message = I scored %s points print(message % myscore)I scored 1000 points nums = What did the number %s say to the number %s? Nice belt! print(nums % (0, 8)What did the number 0 say to the number

6、 8? Nice belt!字符串乘法 print(10 * a)Aaaaaaaaaa试试下面的输出结果spaces = * 25print(%s 12 Butts Wynd % spaces)11谷风书苑2.比字符串更强大的列表(list)LIST(列表) 很多变量的集合,用进行定义 some_numbers = 1, 2, 5, 10, 20 some_strings = Which, Witch, Is, Which定义一个list你可以对list进行如下操作 some_ some_strings.append(bear burp) #追加项目 del some_strings2 #删除

7、第3项 print(some_strings2:3) #显示第3-4项 print(some_strings) #显示所有项 print(some_numbers + some_strings) #可以做加法 print(some_numbers * 5) #可以做乘法除法,减法不行哦!考虑一下为什么12谷风书苑2.另一种列表元祖(tuples)TUPLE(元祖)元组类似于使用圆括号的列表,用()进行定义,区别是创建后不能更改 fibs = (0, 1, 1, 2, 3) print(fibs3)定义一个tuple你不可以改变tuple的内容否则计算机给给你报错 fibs0 = 4Traceb

8、ack (most recent call last):File , line 1, in fibs0 = 4TypeError: tuple object does not support item assignment13谷风书苑2.帮你找到你想要的(字典)MAP(字典)字典中的每一项都有一个键和一个对应的值。你可以根据键找到值。 favorite_sports = Ralph Williams : Football,Michael Tippett :Basketball,Edward Elgar : Baseball,Rebecca Clarke : Netball,Ethel Smyt

9、h : Badminton,Frank Bridge : Rugby定义一个map你可以对字典做如下操作 print(favorite_sportsRebecca Clarke) #找到RebeccaClarke喜欢的运动del favorite_sportsEthel Smyth #从字典中删除EthelSmyth数据favorite_sportsEthel Smyth = Ice Hockey #修改Ethel Smyth喜欢的运动favorite_sportsCan Can=tennis #追加cancan喜欢的项目14谷风书苑第四章海龟画图你可以画出绚丽的图案15谷风书苑1.什么是海龟

10、Turbles是一个画板模块,你可以利用它绘图。正如你写字并不需要你去制造铅笔和纸张,你可以利用turtle去绘画16谷风书苑2.海龟绘图import turtle #引进海龟,你可以开始使用它turtle.pencolor(red) #设置画笔颜色(红色)turtle.pensize(1) #设置画笔粗细turtle.forward(100) #让海龟前进50个像素turtle.left(90) #左转90度turtle.forward(100) #让海龟继续前进50个像素turtle.left(90) #左转90度turtle.forward(100) #让海龟继续前进50个像素turtl

11、e.left(90) #左转90度turtle.forward(100) #让海龟继续前进50个像素turtle.up() #让海龟抬起笔turtle.left(90) #左转90度turtle.forward(50) #让海龟继续前进25个像素turtle.down() #让海龟放下笔turtle.pencolor(green) #设置画笔颜色(绿色)turtle.pensize(3) #设置画笔粗细turtle.circle(50) #画一个半径50的圆17谷风书苑3.运用技巧import turtle #引进海龟,你可以开始使用它myColor=red,green,brownindex

12、=0for x in range(250): turtle.pencolor(myColorindex) index +=1 if index = 3: index = 0 turtle.forward(x*2) turtle.left(92)右边的图怎么画出来的?看看下面的代码让计算机干了什么18谷风书苑第五章逻辑判断用IF ELSE判断逻辑19谷风书苑1. 逻辑判断age = 10if age = 20: print(oh!you are yong)Elif age 20 and age = 10 and age myval = None if myval = None:print(The

13、 variable myval doesnt have a value)什么都没有保存的空值 age=10 if age = 10:print(The variable myval doesnt have a value)数值是字符串还是数字? age=10 if age = 10:print(The variable myval doesnt have a value) age = 10 converted_age = int(age) age = 10 converted_age = str(age) age = 10.5 converted_age = int(age) if age =

14、 10:print(The variable myval doesnt have a value)结果如何23谷风书苑第六章重复事件处理24谷风书苑1. 循环作业要抄写100遍? NO! print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”).print(“homework”)print(“homework”)print(“homework”)p

15、rint(“homework”)so easy!for x in range(0, 99): print(homework)for x in range(0, 99): print(hello %s % x)试试这个25谷风书苑2. 列表(list)的循环 print(list(range(10, 20)10, 11, 12, 13, 14, 15, 16, 17, 18, 19简单的列表打印class_list = class1,class2,class3,class4,class5 for x in range(0, 4): print(hello %s % class_listx)循环方

16、式的列表打印循环方式的遍历列表 wizard_list = spider legs, toe of frog, snail tongue,bat wing, slug butter, bear burp for i in wizard_list:print(i)左边的1和2实现方式有什么区别?hugehairypants = huge, hairy, pantsfor i in hugehairypants: print(i) for j in hugehairypants: print(j)推测一下下面的结果26谷风书苑3. 一道循环的计算题问题 宝箱里有20枚金币,每天会增加10枚,但是乌

17、鸦每周会偷走3枚,请计算一年53周每周宝箱内会剩余多少金币 found_coins = 20 magic_coins = 70 stolen_coins = 3u coins = found_coinsv for week in range(1, 53):w coins = coins + magic_coins - stolen_coinsx print(Week %s = %s % (week, coins)27谷风书苑4. 循环处理的几种语法for step in range(0, 20): print(step)FOR循环x = 45y = 80 while x 50 and y 10

18、0: x = x + 1 y = y + 1 print(x, y)WHILE循环for x in range(0, 20): print(hello %s % x) if x 9: breakBreak可以提前退出循环28谷风书苑第七章模块和函数函数是一些处理逻辑的集合模块是函数,变量的集合拥有更强大的功能海龟就是一个绘图模块29谷风书苑1. 函数构成def testfunc(myname): print(hello %s % myname)函数名,参数,处理testfunc(Mary)print(savings(10, 10, 5)执行函数def testfunc(fname, lname

19、): print(Hello %s %s % (fname, lname)函数可以有多个参数函数可以有返回值def savings(pocket_money, paper_route, spending): return pocket_money + paper_route spending30谷风书苑2. 一个函数的例子每周生产X个罐子,计算出一年中每周位置总共生产的罐子。def spaceship_building(cans): total_cans = 0 for week in range(1, 53): total_cans = total_cans + cans print(Wee

20、k %s = %s cans % (week, total_cans)函数调用spaceship_building(2) #A工厂每周只能生产2个spaceship_building(10) #B工厂每周只能生产10个考虑一下使用函数的好处31谷风书苑3. 模块(moudle)如何导入模块import sys #导入系统模块Import turtle #导入海龟绘图模块只有导入模块后,才可以使用它32谷风书苑4. 使用sys模块sys模块内部有一个特殊的对象称为stdin(标准输入),它提供了一个相当有用的函数readline。ReadLine函数用于读取一行文本类型在键盘上,直到按回车键。S

21、tandard input的略称import sysdef ageEV(): print(How old are you?) age = int(sys.stdin.readline() if age 15 and age ozwald = Giraffes(100) gertrude = Giraffes(150) print(ozwald.giraffe_spots)100 print(gertrude.giraffe_spots)150初期化函数的例子初期化函数的例子初期化函数的使用实例初期化函数的使用实例41谷风书苑第九章python自带的常用函数42谷风书苑1. Python自带函数

22、-1获得绝对值获得绝对值 abs() print(abs(10)10布尔变量布尔变量 bool() print(bool(0)False print(bool(1)True print(bool(a)Dir函数函数 print(bool(0)False print(bool(1)True print(bool(a)#用它来计算绝对值#用它来取得逻辑真假,可进行IF判断 还记得条件语法吗 if elif else#它的参数是任意类型,执行结果可以告诉你,可以处理这种类型所有的函数。你需要从一堆结果中找出自己有用 的信息。看看下面的记过,对于整数你可以利用那些函数。 print(dir(1)_ab

23、s_, _add_, _and_, _bool_, _ceil_, _class_, _delattr_, _dir_, _divmod_, _doc_, _eq_, _float_, _floor_, _floordiv_, _format_, _ge_, _getattribute_, _getnewargs_, _gt_, _hash_, _index_, _init_, _init_subclass_, _int_, _invert_, _le_, _lshift_, _lt_, _mod_, _mul_, _ne_, _neg_, _new_, _or_, _pos_, _pow_,

24、 _radd_, _rand_, _rdivmod_, _reduce_, _reduce_ex_, _repr_, _rfloordiv_, _rlshift_, _rmod_, _rmul_, _ror_, _round_, _rpow_, _rrshift_, _rshift_, _rsub_, _rtruediv_, _rxor_, _setattr_, _sizeof_, _str_, _sub_, _subclasshook_, _truediv_, _trunc_, _xor_, bit_length, conjugate, denominator, from_bytes, im

25、ag, numerator, real, to_bytes43谷风书苑2. Python自带函数-2获得帮助获得帮助help help(abs)Help on built-in function abs in module builtins:abs(x, /) Return the absolute value of the argument.执执行命令函数行命令函数 eval your_calculation = input(Enter a calculation: )Enter a calculation: 12*52 eval(your_calculation)624#用它让Python

26、告诉你函数的使用方法,不过都是英文哦!执执行命令函数行命令函数 eval my_small_program = print(ham)print(sandwich) exec(my_small_program)hamsandwich区别 eval 可以有返回值 exec 无返回值44谷风书苑3. Python自带函数-3浮点值浮点值 float() print(abs(10)10整数整数 int() float(123.456789)123.456789 your_age = input(Enter your age: )Enter your age: 20 age = float(your_a

27、ge) if age 13:print(You are %s years too old % (age - 13)You are 7.0 years too old#带很多位小数的值 int(123.456)123 int(123)123 int(123.456)Traceback (most recent call last):File , line 1, in int(123.456)ValueError: invalid literal for int() with base 10: 123.456出错了!字符串123.456不可以45谷风书苑4. Python自带函数-4取得长度取得长

28、度 len len(this is a test string)21 creature_list = unicorn, cyclops, fairy, elf, dragon,troll print(len(creature_list)6取得最大数,最小值取得最大数,最小值 max min numbers = 5, 4, 10, 30, 22 print(max(numbers)30 strings = s,t,r,i,n,g,S,T,R,I,N,G print(max(strings)t范围函数范围函数 range for x in range(0, 5):print(x) count_by

29、_twos = list(range(0, 30, 2) print(count_by_twos)0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28 count_down_by_twos = list(range(40, 10, -2) print(count_down_by_twos)40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 1246谷风书苑5. Python自带函数-5计算和计算和文件访问文件访问 test_file = open(c:test.txt) text

30、= test_file.read() print(text)文件内容xxxxxxxxx my_list_of_numbers = list(range(0, 500, 50) print(my_list_of_numbers)0, 50, 100, 150, 200, 250, 300, 350, 400, 450 print(sum(my_list_of_numbers)2250 test_file = open(c:myfile.txt, w) test_file.write(What is green and loud? A froghorn!) test_file.close()读取文

31、件写入文件47谷风书苑第十章python常用的模块Python模块是函数、类和变量的集合。为了使它们更容易使用。 Python使用模块来分组函数和类。例如,海龟模块,我们在前几章使用它,用它创建的画布在屏幕上画画。48谷风书苑1. 复制模块copy -1导入复制模块导入复制模块复制模块的使用实例复制模块的使用实例 class Animal:def _init_(self, species, number_of_legs, color):self.species = speciesself.number_of_legs = number_of_legsself.color = colorimpo

32、rt copy import copy #导入复制模块 harry = Animal(hippogriff, 6, pink) #创建harry对象 harriet = copy.copy(harry) #把harry复制到harriet print(harry.species) #输出harry的species属性hippogriff print(harriet.species) #输出hariet的species属性hippogriff作用 把一个对象复制给另一个对象就像你在复印机上复印资料一样写入文件创建一个动物类49谷风书苑2. 复制模块copy-2Copy 和和 deepcopy h

33、arry = Animal(hippogriff, 6, pink) carrie = Animal(chimera, 4, green polka dots) billy = Animal(bogill, 0, paisley) my_animals = harry, carrie, billy more_animals = copy.copy(my_animals) print(more_animals0.species)hippogriff print(more_animals1.species)Chimera my_animals0.species = ghoul print(my_a

34、nimals0.species)ghoul print(more_animals0.species)ghoul more_animals = copy.deepcopy(my_animals) my_animals0.species = wyrm print(my_animals0.species)Wyrm print(more_animals0.species)ghoul50谷风书苑3. Python的关键字模块关键字关键字 keyword import keyword print(keyword.iskeyword(if)True print(keyword.iskeyword(ozwal

35、d)False print(keyword.kwlist)False, None, True, and, as, assert, break, class,continue, def, del, elif, else, except, finally,for, from, global, if, import, in, is, lambda,nonlocal, not, or, pass, raise, return, try, while,with, yield通过关键字模块输出python关键字,帮助我们认识到python语言中那些单词是有特殊意义的,我们定义变量和函数时需要避开重名。51

36、谷风书苑4. 随机函数模块randomrandom 返回制定范围的随机值返回制定范围的随机值 import random print(random.randint(1, 100)58 print(random.randint(100, 1000)861choice 从列表随机取出一个项目从列表随机取出一个项目 import random desserts = ice cream, pancakes, brownies, cookies,candy print(random.choice(desserts)browniesShuffle 把列表洗牌重新排序把列表洗牌重新排序 import ran

37、dom desserts = ice cream, pancakes, brownies, cookies,candy random.shuffle(desserts) print(desserts)pancakes, ice cream, candy, brownies, cookies52谷风书苑5. 系统模块对控制台进行操作sysexit 关闭控制带关闭控制带 import sys sys.exit()stdin.readline 从控制台读入输入信息从控制台读入输入信息 import sys v = sys.stdin.readline()He who laughs last thin

38、ks slowest print(v)He who laughs last thinks sloweststdout.write 把内容输出到控制台把内容输出到控制台 import sys sys.stdout.write(What does a fish say when it swims into a wall?Dam.)What does a fish say when it swims into a wall? Dam.52 import sys print(sys.version)3.1.2 (r312:79149, Mar 21 2013, 00:41:52) MSC v.1500

39、 32 bit (Intel)version 显示系统版本显示系统版本53谷风书苑6. 时间模块time-1time 取得现在时间取得现在时间 import time print(time.time()1300139149.34 def lots_of_numbers(max):u t1 = time.time()v for x in range(0, max):print(x)w t2 = time.time()x print(it took %s seconds % (t2-t1) lots_of_numbers(1000)January 1, 1970, at 00:00:00计算经过的

40、时间time.asctime 取得可读的时间取得可读的时间 import time print(time.asctime()Mon Mar 11 22:03:41 2013 import time t = (2020, 2, 23, 10, 30, 48, 6, 0, 0) print(time.asctime(t)Sun Feb 23 10:30:48 2020time.asctime 自己定义一个时间自己定义一个时间54谷风书苑7. 时间模块time-2time.localtime 取得现在时间的列表取得现在时间的列表 import time print(time.localtime()t

41、ime.struct_time(tm_year=2020, tm_mon=2, tm_mday=23, tm_hour=22,tm_min=18, tm_sec=39, tm_wday=0, tm_yday=73, tm_isdst=0) t = time.localtime() year = t0 month = t1 print(year)2020 print(month)2time.sleep 让计算机休息一会儿让计算机休息一会儿 for x in range(1, 61):print(x)time.sleep(1)55谷风书苑8. 保存信息模块pickle保存保存map信息到文件信息到

42、文件 import picklev game_data = player-position : N23 E45,pockets : keys, pocket knife, polished stone,backpack : rope, hammer, apple,money : 158.50w save_file = open(save.dat, wb)x pickle.dump(game_data, save_file)y save_file.close()从文件读取保存的信息从文件读取保存的信息 load_file = open(save.dat, rb) loaded_game_data

43、 = pickle.load(load_file) load_file.close() print(loaded_game_data)money: 158.5, backpack: rope, hammer, apple,player-position: N23 E45, pockets: keys, pocket knife,polished stone56谷风书苑第十章高级海龟绘图57谷风书苑1. 进阶海龟绘图运运用学到的知识试试海龟画出下面的图用学到的知识试试海龟画出下面的图58谷风书苑第十一章图形界面59谷风书苑1. 什么是图形界面你现在使用的计算机就是图形界面(例如你现在使用的计算机

44、就是图形界面(例如)60谷风书苑2. Python的图形界面Python的图形包的图形包Import tkinter要开发图形界面,首先要导入图形包Python的图形的图形接接口口tkniter.Tk()创建基本的窗口Python的窗口控件的窗口控件tkniter.Button()按键tkniter.Canvas()用来在窗口画图的画布等等。Python的窗口更新显示的窗口更新显示xxxx.Pack()当你画了控件xxxx后需要用执行Pack来让它显示61谷风书苑3. Python的图形界面Python的标准图形控件的标准图形控件控件描述Button按钮控件;在程序中显示按钮。Canvas画布

45、控件;显示图形元素如线条或文本Checkbutton多选框控件;用于在程序中提供多项选择框Entry输入控件;用于显示简单的文本内容Frame框架控件;在屏幕上显示一个矩形区域,多用来作为容器Label标签控件;可以显示文本和位图Listbox列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户Menubutton菜单按钮控件,由于显示菜单项。Menu菜单控件;显示菜单栏,下拉菜单和弹出菜单Message消息控件;用来显示多行文本,与label比较类似Radiobutton单选按钮控件;显示一个单选的按钮状态Scale范围控件;显示一个数值刻度,为输出限定范围的数字区间Scro

46、llbar滚动条控件,当内容超过可视化区域时使用,如列表框。.Text文本控件;用于显示多行文本Toplevel容器控件;用来提供一个单独的对话框,和Frame比较类似Spinbox输入控件;与Entry类似,但是可以指定输入范围值PanedWindowPanedWindow是一个窗口布局管理的插件,可以包含一个或者多个子控件。LabelFramelabelframe 是一个简单的容器控件。常用与复杂的窗口布局。tkMessageBox用于显示你应用程序的消息框。62谷风书苑4. 实现你的第一个图形界面import tkinterdef hello(): print(hello there)t

47、k=tkinter.Tk()btn = tkinter.Button(tk,text=click me,command=hello,width=8,height=1) btn.pack()canvas = tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_line(0, 0, 500, 500)导入tkinter定义一个函数,在控制台输出hello there创建窗口在窗口加入按键,尺寸为8,1 显示click 按下按键后执行hello函数显示按键创建画布尺寸为500,500显示画布在画布尺上画一条线这是执行结果

48、63谷风书苑5. 常用的绘图方法-1绘制盒子绘制盒子import tkinterimport randomtk=tkinter.Tk()canvas = tkinter.Canvas(tk,width=500,height=500)canvas.pack()def random_rectangle(width,height,fill_color): x1= random.randrange(width) y1= random.randrange(height) x2= x1+random.randrange(width) y2= y1+random.randrange(height) canv

49、as.create_rectangle(x1,y1,x2,y2,fill=fill_color)for x in range(0,100): random_rectangle(400,400,#eb5699)64谷风书苑5. 常用的绘图方法-2绘制绘制圆弧圆弧import tkintertk=tkinter.Tk()canvas = tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_arc(10,10,200,80,extent=359,style=tkinter.ARC)canvas.create_arc(10

50、0,100,200,200,extent=359,style=tkinter.ARC)参数的意义1.图形左上角坐标2.图形右下角坐标3.绘制角度4.绘制圆弧常量65谷风书苑6. 常用的绘图方法-3绘制多边形绘制多边形import tkintertk=tkinter.Tk()canvas = tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_polygon(1,1,100,10,100,110,fill=,outline=black)canvas.create_polygon(200, 10, 240, 30, 1

51、20, 100, 140, 120, fill=,outline=black)参数的意义1.给出所有顶点的坐标66谷风书苑7. 常用的绘图方法-4显示文字显示文字import tkintertk=tkinter.Tk()canvas = tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_text(150, 150, text=He said, Its my curse,font=(Times, 15)canvas.create_text(200, 200, text=But it could be worse,f

52、ont=(Helvetica, 20)canvas.create_text(220, 250, text=My cousin rides round,font=(Courier, 22)canvas.create_text(220, 300, text=on a goose., font=(Courier, 30)67谷风书苑8. 常用的绘图方法-5显示背景图片显示背景图片import tkintertk=tkinter.Tk()canvas = tkinter.Canvas(tk,width=800,height=500)canvas.pack()my_image=tkinter.Photo

53、Image(file=5414231.gif)canvas.create_image(0,0,anchor=tkinter.NW,image=my_image)注意Tkinter只能处理gif图片,如果要处理其他图片需要用到python图形库注意NW告诉tkinter图片左上角是原点,否则将以图片中心作为原点68谷风书苑9. 常用的绘图方法-6创作你的蒙太奇创作你的蒙太奇import tkinterimport timetk=tkinter.Tk()canvas = tkinter.Canvas(tk,width=800,height=500)canvas.pack()mytriangle=c

54、anvas.create_polygon(10, 10, 10, 60, 50, 35)for x in range(0, 60): canvas.move(1, 5, 0) tk.update() time.sleep(0.01)def movetriangle(event): if event.keysym = Up: canvas.move(1, 0, -3) canvas.itemconfig(mytriangle, fill=blue) elif event.keysym = Down: canvas.move(1, 0, 3) canvas.itemconfig(mytriangl

55、e, fill=red) elif event.keysym = Left: canvas.move(1, -3, 0) canvas.itemconfig(mytriangle, fill=green) else: canvas.move(1, 3, 0) canvas.itemconfig(mytriangle, fill=black)canvas.bind_all(, movetriangle)canvas.bind_all(, movetriangle)canvas.bind_all(, movetriangle)canvas.bind_all(, movetriangle)画三角蒙太奇动作键盘事件动作键盘事件绑定Move三个参数1.画布上图片代号2.X坐标移动距离3.Y坐标移动距离69谷风书苑下一阶段恭喜你学完python基本知识,下面可以完成2个简单的游戏70谷风书苑弹球游戏71谷风书苑火柴人游戏72谷风书苑

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号