
Python 是一个有条理的和强大的面向对象的程序设计语言,类似于Perl, Ruby, Scheme, 或 Java.
如果你是在程序中让其自动退出,则可以使用:
exit()
quit()
执行到此命令时,程序终止。
如果是程序陷入死循环,想强制结束,则按Ctrl + C。
Python解析器肯定要装,另外就是一个编辑器。用pycharm相对比较友好。当然,也可以用vim/emacs,或者vscode,sublime什么的。一般要有代码提示。另外一个就是代码跳转,这个看别人代码很重要。
python的构造方法
构造方法与其他普通方法不同的地方在于,当一个对象被创建后,会立即调用构造方法。
在python中创建一个构造方法很简单,只需要把init方法的名字从简单的init修改为魔法版本__init__即可。
class foobar:
def __init__(self):
self.somevar = 42
f = foobar()
f.somevar
42
1.Python基础:
计算机组成原理和开发环境
基础语法
函数
文件操作
面向对象
异常处理
常见模块
项目:飞机大战
2.Python高级和linux操作系统
Linux操作系统
网络编程
项目:网络web服务器
MySQL数据库
项目:Web框架
3.Python全栈开发
HTML+CSS
HTML+CSS
JS
jquery
Vue
后端开发常用框架
后端开发Django
全栈项目:电商平台
4.爬虫开发
爬虫开发
Mongodb应用开发
Scrapy框架项目:定制化爬虫框架TaskSpide
等等
PyPy是用Python实现的Python解释器。
转载:physique
python实现贪吃蛇
贪吃蛇的算法还是比较简单的,蛇的移动我是通过不停添加一个head方块,然后判断应该加到蛇头的哪个方向,加完后删掉蛇尾就行了,如果吃到食物就不删蛇尾。
只是一个贪吃蛇只需要70行代码左右就可以了,后来又加了计分,失败后重新游戏,暂停功能····结果现在代码乱成渣了。。
重新游戏部分肯定有更好的方法,我写的太乱了。。求大神指教。由于没用网格,判断吃到的时候是用范围判断的,有时候有些偏差···
代码:
复制代码
1 #-- coding: utf-8 --
2 import pygame, sys, random, time
3 from pygame.locals import *
4
5 pygame.init()
6 font = pygame.font.Font(u"c:\windows\fonts\MSYH.ttf",30)
7 mainClock = pygame.time.Clock()
8 wsurface = pygame.display.set_mode((800,600),0,32)
9 pygame.display.set_caption("My_Snake~")
10
11 Len = 20
12 snakeRect = []
13 for i in range(10,13):
14 snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len))
15 food = pygame.Rect(10 (Len), 10 (Len), Len, Len)
16 ml = False
17 mr = True
18 mu = False
19 md = False
20 score = 0
21 black = (0, 0, 0)
22 green = (0, 255, 0)
23 white = (255, 255, 255)
24 global FPSCLOCK
25 FPSCLOCK = pygame.time.Clock()
26 #####################################################
27 def judge():
28 if snakeRect[0].left - 15 <= food.left <= snakeRect[0].left + 15 and snakeRect[0].top - 15 <= food.top <= snakeRect[0].top + 15:
29 return True
30 def judge2(a, b):
31 if a.left - 15 <= b.left <= a.left + 15 and a.top - 15 <= b.top <= a.top + 15:
32 return True
33 def checkForKeyPress():
34 #checkForQuit()
35 for event in pygame.event.get([KEYDOWN, KEYUP]):
36 if event.type == KEYUP:
37 continue
38 return event.key
39 return None
40 #######################################################
41 flagg = True
42 speed = 8
43 while True:
44 for event in pygame.event.get():
45 if event.type == QUIT:
46 pygame.QUIT()
47 sys.exit()
48 if event.type == KEYDOWN:
49 if event.key == K_r:
50 snakeRect = []
51 for i in range(10,13):
52 snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len))
53 food = pygame.Rect(10 (Len), 10 (Len), Len, Len)
54 ml = False
55 mr = True
56 mu = False
57 md = False
58 score = 0
59 flagg = True
60 speed = 8
61 if event.key == K_p:
62 wsurface.fill(black)
63 text_surface1 = font.render('Pause!' , True, (0, 0, 255))
64 wsurface.blit(text_surface1, (10, 50))
65 while checkForKeyPress() == None:
66 pygame.display.update()
67 FPSCLOCK.tick()
68 if event.key == K_LEFT and mr == False:
69 ml = True
70 mr = False
71 mu = False
72 md = False
73 if event.key == K_RIGHT and ml == False:
74 ml = False
75 mr = True
76 mu = False
77 md = False
78 if event.key == K_UP and md == False:
79 ml = False
80 mr = False
81 mu = True
82 md = False
83 if event.key == K_DOWN and mu == False:
84 ml = False
85 mr = False
86 mu = False
87 md = True
88 head = pygame.Rect(snakeRect[0].left,snakeRect[0].top,snakeRect[0].width,snakeRect[0].height)
89 if flagg == False:
90 continue
91 if ml == True:
92 head.right = head.left - 1
93 if mr == True:
94 head.left = head.right + 1
95 if mu == True:
96 head.bottom = head.top - 1
97 if md == True:
98 head.top = head.bottom + 1
99 snakeRect.insert(0, head)
100 #判断失败和重新游戏
101 if head.right < 0 or head.left > 800 or head.bottom < 0 or head.top > 600 or snakeRect[0] in snakeRect[1:]:
102 wsurface.fill(white)
103 text_surface2 = font.render('Press R to restart!' , True, (0, 0, 255))
104 wsurface.blit(text_surface2, (50, 80))
105 pygame.display.update()
106 while checkForKeyPress() == None:
107 pygame.display.update()
108 FPSCLOCK.tick()
109 break
110 flagg = False
111 continue
112 flagg = True
113 if judge():
114 score = score + 10
115 speed = speed + 1
116 while True:
117 flag = True
118 food.left = random.randrange(10,800)
119 food.top = random.randrange(10,600)
120 for temp in snakeRect:
121 if judge2(food, temp):
122 flag = False
123 if flag == True:
124 break
125 else:
126 snakeRect.pop()
127 wsurface.fill(black)
128 for i in range(len(snakeRect)):
129 pygame.draw.rect(wsurface,green,snakeRect[i])
130 pygame.draw.rect(wsurface,white,food)
131 text_surface = font.render(u"分数: " + str(score), True, (0, 0, 255))
132 wsurface.blit(text_surface, (10, 50))
133 pygame.display.update()
134 mainClock.tick(speed)
不明白你说的什么意思哦
Python 是一个有条理的和强大的面向对象的程序设计语言,类似于Perl, Ruby, Scheme, 或 Java.
当参数为*arg时,表示接受一个元组;
当参数为**arg时,表示接受一个字典
python 是C语言开发的
python有对应的包sympy,用来计算积分,无论不定积分还是定积分。
主要用到integrate这个函数,
:from sympy import integrate
:integrate?
可以找到integrate函数的用法,
基本上都是APP模拟器方式:qpython3或Termux
Python 是一个有条理的和强大的面向对象的程序设计语言,类似于Perl, Ruby, Scheme, 或 Java.
Python 是一个有条理的和强大的面向对象的程序设计语言,类似于Perl, Ruby, Scheme, 或 Java.
self代表类的实例,而非类。
实例来说明
class Test:
def prt(self):
print(self)
print(self.__class__)
t = Test()
t.prt()
执行结果如下
<__main__.Test object at 0x000000000284E080>
从上面的例子中可以很明显的看出,self代表的是类的实例。而self.__class__则指向类。
使用python执行系统命令,比如curl
import os
data = ['www.baidu.com',
'www.csdn.cn']
for item in data:
tmpres = os.popen('curl %s' % item).readlines()
print(tmpres)
print("ok..")
==tempfile 模块==
[Example 2-6 #eg-2-6] 中展示的 tempfile
模块允许你快速地创建名称唯一的临时文件供使用.
====Example 2-6. 使用 tempfile 模块创建临时文件====[eg-2-6]
File: tempfile-example-1.py
import tempfile
import os
tempfile = tempfile.mktemp()
print "tempfile", "=>", tempfile
file = open(tempfile, "w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()), "bytes"
file.close()
try:
# must remove file when done
os.remove(tempfile)
except OSError:
pass
tempfile => C:\TEMP\~160-1
bytes
TemporaryFile
函数会自动挑选合适的文件名, 并打开文件, 如 [Example 2-7 #eg-2-7] 所示.
而且它会确保该文件在关闭的时候会被删除. (在 Unix 下, 你可以删除一个已打开的文件, 这
时文件关闭时它会被自动删除. 在其他平台上, 这通过一个特殊的封装类实现.)
====Example 2-7. 使用 tempfile 模块打开临时文件====[eg-2-7]
File: tempfile-example-2.py
import tempfile
file = tempfile.TemporaryFile()
for i in range(100):
file.write("*" * 100)
file.close() # removes the file!
一、什么是爬虫
爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息。
二、Python爬虫架构
Python 爬虫架构主要由五个部分组成,分别是调度器、URL管理器、网页下载器、网页解析器、应用程序(爬取的有价值数据)。
pop()将列表指定位置的元素移除,同时可以将移除的元素赋值给某个变量,不填写位置参数则默认删除最后一位
pop()根据键将字典中指定的键值对删除,同时可以将删除的值赋值给变量
举个例子:
1 a = ["hello", "world", "dlrb"]
2 b = ["hello", "world", "dlrb"]
3 a.pop(1)
4 b1 = b.pop(0)
5 print(a)
6 print(b1)
输出结果:
['hello', 'dlrb']
hello
我们将列表a的位置1的元素移除
将列表b的位置0的元素移除并赋值给变量b1