## 帮助!如何移动图像？¶

pete@shinners.org

### 只有屏幕上的像素¶

PYGAME有一个显示界面。这基本上是一个在屏幕上可见的图像，该图像由像素组成。更改这些像素的主要方法是调用Blit()函数。这会将像素从一个图像复制到另一个图像。

### 让我们倒退一步¶

```>>> screen = [1, 1, 2, 2, 2, 1]
>>> print(screen)
[1, 1, 2, 2, 2, 1]
```

```>>> screen[3] = 8
>>> print(screen)
[1, 1, 2, 8, 2, 1]
```

### 让英雄行动起来¶

```>>> playerpos = 3
>>> screen[playerpos] = 8
>>> print(screen)
[1, 1, 2, 8, 2, 1]
```

```>>> playerpos = playerpos - 1
>>> screen[playerpos] = 8
>>> print(screen)
[1, 1, 8, 8, 2, 1]
```

### 创建地图¶

```>>> background = [1, 1, 2, 2, 2, 1]
>>> screen = [0]*6                         #a new blank screen
>>> for i in range(6):
...     screen[i] = background[i]
>>> print(screen)
[1, 1, 2, 2, 2, 1]
>>> playerpos = 3
>>> screen[playerpos] = 8
>>> print(screen)
[1, 1, 2, 8, 2, 1]
```

### 《让英雄动起来》(镜头2)¶

```>>> print(screen)
[1, 1, 2, 8, 2, 1]
>>> screen[playerpos] = background[playerpos]
>>> playerpos = playerpos - 1
>>> screen[playerpos] = 8
>>> print(screen)
[1, 1, 8, 2, 2, 1]
```

```>>> screen[playerpos] = background[playerpos]
>>> playerpos = playerpos - 1
>>> screen[playerpos] = 8
>>> print(screen)
[1, 8, 2, 2, 2, 1]
```

### 定义：“blit”¶

Blit：基本上，blit的意思是将图形从一个图像复制到另一个图像。更正式的定义是将数据数组复制到位图数组目的地。你可以把Blit想象成 “分配” 像素。与上面的屏幕列表中的设置值非常相似，blotting会指定图像中像素的颜色。

### 从名单到屏幕¶

```>>> background = [terrain1, terrain1, terrain2, terrain2, terrain2, terrain1]
>>> screen = create_graphics_screen()
>>> for i in range(6):
...     screen.blit(background[i], (i*10, 0))
>>> playerpos = 3
>>> screen.blit(playerimage, (playerpos*10, 0))
```

```>>> screen.blit(background[playerpos], (playerpos*10, 0))
>>> playerpos = playerpos - 1
>>> screen.blit(playerimage, (playerpos*10, 0))
```

### 屏幕坐标¶

PYGAME附带了一个方便的容器来存放这些坐标，它是一个RECT。矩形基本上表示这些坐标中的一个矩形区域。它有一个最大的边角和一个尺寸。RECT提供了许多方便的方法来帮助您移动和定位它们。在接下来的例子中，我们将用Rect来表示我们的对象的位置。

### 流畅移动¶

```>>> screen = create_screen()
>>> screen.blit(background, (0, 0))        #draw the background
>>> position = player.get_rect()
>>> screen.blit(player, position)          #draw the player
>>> pygame.display.update()                #and show it all
>>> for x in range(100):                   #animate 100 frames
...     screen.blit(background, position, position) #erase
...     position = position.move(2, 0)     #move player
...     screen.blit(player, position)      #draw new player
...     pygame.display.update()            #and show it all
...     pygame.time.delay(100)             #stop the program for 1/10 second
```

### 首先，神秘的功能¶

```>>> player = pygame.image.load('player.bmp').convert()
```

```>>> screen = pygame.display.set_mode((640, 480))
```

### 处理一些输入¶

```>>> while True:
...     for event in pygame.event.get():
...         if event.type in (QUIT, KEYDOWN):
...             sys.exit()
...     move_and_draw_all_game_objects()
```

### 移动多个图像¶

```>>> class GameObject:
...     def __init__(self, image, height, speed):
...         self.speed = speed
...         self.image = image
...         self.pos = image.get_rect().move(0, height)
...     def move(self):
...         self.pos = self.pos.move(0, self.speed)
...         if self.pos.right > 600:
...             self.pos.left = 0
```

### 把这一切放在一起¶

```>>> screen = pygame.display.set_mode((640, 480))
>>> screen.blit(background, (0, 0))
>>> objects = []
>>> for x in range(10):                    #create 10 objects</i>
...     o = GameObject(player, x*40, x)
...     objects.append(o)
>>> while True:
...     for event in pygame.event.get():
...         if event.type in (QUIT, KEYDOWN):
...             sys.exit()
...     for o in objects:
...         screen.blit(background, o.pos, o.pos)
...     for o in objects:
...         o.move()
...         screen.blit(o.image, o.pos)
...     pygame.display.update()
...     pygame.time.delay(100)
```

Edit on GitHub