1. pygame
1.1. event
1.2. display
1.3. time
1.4. image
1.5. mixer
1.6. transform
1.7. blit
2. numpy
2.1. append
2.2. argmax
2.3. 列表解析
2.4. tile
event.pump()
让 pygame 内部自动处理事件。
1.2.1. pygame.display.set_mode(coordinate)
初始化一个准备显示的窗口或屏幕。
screen_width=700 #创建一个700*400的窗口
screen_height=400
screen=pygame.display.set_mode([screen_width, screen_height])
1.2.2. display.set_caption(name)
设置当前窗口的标题栏。
pygame.display.set_caption('High speed racing')
time.Clock
创建一个对象来帮助跟踪时间。
1.4.1. image.load(path).convert_alpha()
加载图片并带alpha通道的转换图片背景为透明。
1.4.2. image.load(path).convert()
加载图片并将背景转换成黑色。
1.5.1. pygame.mixer.Sound(path)
从文件或缓冲区对象创建新的Sound对象。
1.6.1. tranform.rotate(surface,angle)
将图像进行旋转。
1.6.2. transform.flip(Surface, xbool, ybool)
其中xbool=True
为水平翻转,ybool=True
为垂直翻转,返回Surface
。
1.6.3. transform.scale(Surface, (width, height), DestSurface = None)
其中(width, height)
为缩放的大小,返回Surface
。
surface.blit(image,(x,y),rect)
在屏幕上绘制图像,第三个参数为矩形绘制图像参数。
1.8.1. cur_font = pygame.font.SysFont("Times New Roman", 25)
获取系统字体,并设置文字大小 。
1.8.2. cur_font.set_bold(False)
设置是否加粗属性。
1.8.3. cur_font.set_italic(False)
设置是否斜体属性。
1.8.4. text = cur_font.render("I Love Python", 1, (0, 0, 0))
设置文本字体颜色。
append函数会在数组后加上相应的元素
import numpy as np
a=[]
for i in range(5):
a.append([])
for j in range(5):
a[i].append(i)
输出结果为:
[[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]]
np.argmax(matrix,axis)
返回的是最大数的索引,axis
默认是0,表示列,1表示行。
import numpy as np
a = np.array([[1, 5, 5, 2],
[9, 6, 2, 8],
[3, 7, 9, 1]])
print(np.argmax(a, axis=0))
[1,2,2,1]
showStr = [u" Vec:%3.2fKm/h AverVec:%3.2f Km/h" %(10*self.playerVelX, 10*self.totalRunLength/self.totalTime),
u"Mileage:%5.2fKm CarPassed:%d" %(self.totalRunLength*10/3600, self.score)]
2.3.1.
L = [ i**2 for i in range(1,11) if i >= 4 ]
print L
[16, 25, 36, 49, 64, 81, 100]
2.3.2.
a=5
b=[[] for _ in range(a)]
print(b)
[[], [], [], [], []]
numpy.tile([0,0],(2,1)) #在列方向上重复[0,0]1次,行2次
array([[0, 0],
[0, 0]])
2、当程序出现错误,python会自动引发异常,也可以通过raise显示地引发异常。一旦执行了raise
语句,raise
后面的语句将不能执行。raise
语法格式为raise [Exception [, args [, traceback]]]
,语句中 Exception
是异常的类型(例如,NameError)参数标准异常中任一种,args
是自已提供的异常参数。最后一个参数是可选的(在实践中很少使用),如果存在,是跟踪异常对象。
def mye( level ):
if level < 1:
raise Exception("Invalid level!")
# 触发异常后,后面的代码就不会再执行
try:
mye(0) # 触发异常
except Exception as err:
print(1,err)
else:
print(2)
输出结果为
1 Invalid level!
try
和except
用法如下:
try:
正常逻辑
except Exception,err:
触发自定义异常
else:
其余代码