## 冲浪阵列游戏攻略¶

pete@shinners.org

### 数字 Python¶

```>>> from numpy import *                    #import numeric
>>> a = array((1,2,3,4,5))                 #create an array
>>> a                                      #display the array
array([1, 2, 3, 4, 5])
>>> a[2]                                   #index into the array
3
>>> a*2                                    #new array with twiced values
array([ 2,  4,  6,  8, 10])
```

```>>> len(a)                                 #get array size
5
>>> a[2:]                                  #elements 2 and up
array([3, 4, 5])
>>> a[:-2]                                 #all except last 2
array([1, 2, 3])
>>> a[2:] + a[:-2]                         #add first and last
array([4, 6, 8])
>>> array((1,2,3)) + array((3,4))          #add arrays of wrong sizes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (3,) (2,)
```

```>>> a                                      #show our starting array
array([1, 2, 3, 4, 5])
>>> aa = a[1:3]                            #slice middle 2 elements
>>> aa                                     #show the slice
array([2, 3])
>>> aa[1] = 13                             #chance value in slice
>>> a                                      #show change in original
array([ 1, 2, 13,  4,  5])
>>> aaa = array(a)                         #make copy of array
>>> aaa                                    #show copy
array([ 1, 2, 13,  4,  5])
>>> aaa[1:4] = 0                           #set middle values to 0
>>> aaa                                    #show copy
array([1, 0, 0, 0, 5])
>>> a                                      #show original again
array([ 1, 2, 13,  4,  5])
```

```>>> row1 = (1,2,3)                         #create a tuple of vals
>>> row2 = (3,4,5)                         #another tuple
>>> (row1,row2)                            #show as a 2D tuple
((1, 2, 3), (3, 4, 5))
>>> b = array((row1, row2))                #create a 2D array
>>> b                                      #show the array
array([[1, 2, 3],
[3, 4, 5]])
>>> array(((1,2),(3,4),(5,6)))             #show a new 2D array
array([[1, 2],
[3, 4],
[5, 6]])
```

```>>> b                                      #show our array from above
array([[1, 2, 3],
[3, 4, 5]])
>>> b[0,1]                                 #index a single value
2
>>> b[1,:]                                 #slice second row
array([3, 4, 5])
>>> b[1]                                   #slice second row (same as above)
array([3, 4, 5])
>>> b[:,2]                                 #slice last column
array([3, 5])
>>> b[:,:2]                                #slice into a 2x2 array
array([[1, 2],
[3, 4]])
```

```>>> c = arange(10)                         #like range, but makes an array
>>> c                                      #show the array
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> c[1:6:2]                               #slice odd values from 1 to 6
array([1, 3, 5])
>>> c[4::4]                                #slice every 4th val starting at 4
array([4, 8])
>>> c[8:1:-1]                              #slice 1 to 8, reversed
array([8, 7, 6, 5, 4, 3, 2])
```

### 导入曲面阵列¶

```try:
import numpy as N
import pygame.surfarray as surfarray
except ImportError:
raise ImportError, "NumPy and Surfarray are required."
```

### 冲浪阵列游戏攻略¶

SURFARY中有两种主要类型的函数。用于创建作为表面像素数据的副本的阵列的一组函数。其他函数创建数组像素数据的引用副本，因此对数组的更改将直接影响原始曲面。还有其他函数允许您以数组的形式访问任何每像素的Alpha值，以及一些其他有用的函数。我们稍后将讨论这些其他函数。

NumPy模块使用机器的自然数类型来表示数据值，因此NumPy数组可以由8位、16位和32位的整数组成。 (数组还可以使用其他类型，如浮点数和双精度数，但对于我们的图像操作，我们主要需要担心整数类型) 。由于整数大小的限制，您必须格外小心，以确保引用像素数据的数组类型可以正确映射到适当类型的数据。从曲面创建这些数组的函数为：

surfarray.pixels2d(surface)

surfarray.array2d(surface)

surfarray.pixels3d(surface)

surfarray.array3d(surface)

32位

24位

16位

8位(c-map)

### 示例¶

```allblack = N.zeros((128, 128))
surfdemo_show(allblack, 'allblack')
```

```striped = N.zeros((128, 128, 3))
striped[:] = (255, 0, 0)
striped[:,::3] = (0, 255, 255)
surfdemo_show(striped, 'striped')
```

```imgsurface = pygame.image.load('surfarray.png')
rgbarray = surfarray.array3d(imgsurface)
surfdemo_show(rgbarray, 'rgbarray')
```

```flipped = rgbarray[:,::-1]
surfdemo_show(flipped, 'flipped')
```

```scaledown = rgbarray[::2,::2]
surfdemo_show(scaledown, 'scaledown')
```

```shape = rgbarray.shape
scaleup = N.zeros((shape[0]*2, shape[1]*2, shape[2]))
scaleup[::2,::2,:] = rgbarray
scaleup[1::2,::2,:] = rgbarray
scaleup[:,1::2] = scaleup[:,::2]
surfdemo_show(scaleup, 'scaleup')
```

```redimg = N.array(rgbarray)
redimg[:,:,1:] = 0
surfdemo_show(redimg, 'redimg')
```

```factor = N.array((8,), N.int32)
soften = N.array(rgbarray, N.int32)
soften[1:,:]  += rgbarray[:-1,:] * factor
soften[:-1,:] += rgbarray[1:,:] * factor
soften[:,1:]  += rgbarray[:,:-1] * factor
soften[:,:-1] += rgbarray[:,1:] * factor
soften //= 33
surfdemo_show(soften, 'soften')
```

```src = N.array(rgbarray)
dest = N.zeros(rgbarray.shape)
dest[:] = 20, 50, 100
diff = (dest - src) * 0.50
xfade = src + diff.astype(N.uint)
```

### 透明度¶

SURFARY模块有几种访问Surface的Alpha/Colorkey值的方法。所有Alpha函数都不受曲面整体透明度的影响，只受像素Alpha值的影响。以下是这些函数的列表。

surfarray.pixels_alpha(surface)

surfarray.array_alpha(surface)

surfarray.array_colorkey(surface)

### 其他SurfARRAY函数¶

SURFARY中只有几个其他功能可用。您可以获得一个更好的列表，其中包含更多关于 `surfarray reference page` 。不过，有一个非常有用的功能。

surfarray.blit_array(surface, array)

Edit on GitHub