﻿ Program Arcade Games With Python And Pygame

# Program Arcade GamesWith Python And Pygame

 < Previous Home Next >

# Chapter 8: 动画入门

## 8.1 弹跳的矩形

screen.fill(BLACK)


pygame.draw.rect(screen, WHITE, [50, 50, 50, 50])


rect_x = 50
pygame.draw.rect(screen, WHITE, [rect_x, 50, 50, 50])


rect_x = 50
pygame.draw.rect(screen, WHITE, [rect_x, 50, 50, 50])
rect_x += 1


# 从矩形的x位置开始
# 注意它是如何在主循环之外的
rect_x = 50

# -------- 程序主循环 -----------
while not done:
for event in pygame.event.get(): # 用户做了些操作
if event.type == pygame.QUIT: # 如果用户点击了关闭
done = True # 树下旗帜表明我们已经完成了所以要退出循环

# 设置背景色
screen.fill(BLACK)

pygame.draw.rect(screen, WHITE, [rect_x, 50, 50, 50])
rect_x += 1


rect_x += 5


We can expand this code and increase both x and y, causing the square to move both down and right:

# 矩形的起始位置
rect_x = 50
rect_y = 50

# -------- 程序主循环 -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

# 设置屏幕背景
screen.fill(BLACK)

# 画出矩形
pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])

# 移动矩形的起始位置
rect_x += 5
rect_y += 5


# 矩形的起始位置
rect_x = 50
rect_y = 50

# 矩形的方向和速度
rect_change_x = 5
rect_change_y = 5

# -------- 循环主程序 -----------
while done == False:
for event in pygame.event.get(): # 用户做了些操作
if event.type == pygame.QUIT: # 如果用户点击了关闭
done = True # 树下旗帜表明我们已经完成了所以要退出循环

# 设置屏幕背景
screen.fill(BLACK)

# 画出矩形
pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])

# 移动矩形的起始位置
rect_x += rect_change_x
rect_y += rect_change_y


# 如有需要反转移动的方向
if rect_y > 450:
rect_change_y = rect_change_y * -1


$500-50=450$.

# 如有需要反转移动的方向
if rect_y > 450 or rect_y < 0:
rect_change_y = rect_change_y * -1
if rect_x > 650 or rect_x < 0:
rect_change_x = rect_change_x * -1


# 在一个白色巨新内画一个红色矩形矩形ygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])
pygame.draw.rect(screen, RED, [rect_x + 10, rect_y + 10 ,30, 30])


## 8.2 下雪动画

### 8.2.1 代码解释

for i in range(50):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
pygame.draw.circle(screen, WHITE, [x, y], 2)


for i in range(50):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
snow_list.append([x, y])


print(snow_list[0])


[[34, 10],
[10, 50],
[20, 18]]


print(snow_list[0][1])


print(snow_list[20][0])


# 处理雪片列表里的每一个雪片
for i in range(len(snow_list)):
# 画雪片
pygame.draw.circle(screen, WHITE, snow_list[i], 2)


# 处理列表里每一个雪片位置的复制品
for xy_coord in snow_list:
# 画雪片
pygame.draw.circle(screen, WHITE, xy_coord, 2)


# 处理雪片列表里的每一个雪片
for i in range(len(snow_list)):

# 画雪片
pygame.draw.circle(screen, WHITE, snow_list[i], 2)

# 把雪片向下移动一个像素
snow_list[i][1] += 1



	# 如果雪片已经落在屏幕底端之外
if snow_list[i][1] > 400:
# 把它重设在顶端之上
y = random.randrange(-50, -10)
snow_list[i][1] = y
# 给它一个新的x坐标值
x = random.randrange(0, 400)
snow_list[i][0] = x


### 8.2.2 完整程序的清单

"""
Animating multiple objects using a list.
Sample Python/Pygame Programs
Simpson College Computer Science
http://simpson.edu/computer-science/

Explanation video: http://youtu.be/Gkhz3FuhGoI
"""

# Import a library of functions called 'pygame'
import pygame
import random

# Initialize the game engine
pygame.init()

BLACK = [0, 0, 0]
WHITE = [255, 255, 255]

# Set the height and width of the screen
SIZE = [400, 400]

screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Snow Animation")

# Create an empty array
snow_list = []

# Loop 50 times and add a snow flake in a random x,y position
for i in range(50):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
snow_list.append([x, y])

clock = pygame.time.Clock()

# Loop until the user clicks the close button.
done = False
while not done:

for event in pygame.event.get():   # User did something
if event.type == pygame.QUIT:  # If user clicked close
done = True   # Flag that we are done so we exit this loop

# Set the screen background
screen.fill(BLACK)

# Process each snow flake in the list
for i in range(len(snow_list)):

# Draw the snow flake
pygame.draw.circle(screen, WHITE, snow_list[i], 2)

# Move the snow flake down one pixel
snow_list[i][1] += 1

# If the snow flake has moved off the bottom of the screen
if snow_list[i][1] > 400:
# Reset it just above the top
y = random.randrange(-50, -10)
snow_list[i][1] = y
# Give it a new x position
x = random.randrange(0, 400)
snow_list[i][0] = x

# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
clock.tick(20)

# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()



## 8.3 3D动画

blue_object["x_change"]
blue_object["y_change"]

if语句用拉检查蓝色物体是否抵达了屏幕边缘和反置的方向。 不像2D游戏里的像素，这里物理的坐标肯恩是浮点类型。 定义在5和6之间的一个位置，设置成5.5是被允许的。

# 导入Blender游戏引擎
import bge

# 获得一份蓝色物体的参考
cont = bge.logic.getCurrentController()
blue_object = cont.owner

# 打印蓝色物体的坐标位置x,y
print(blue_object.position[0], blue_object.position[1] )

# 根据x_change和y_change来改变位置
# x_change和y_change是蓝色物体这个对象所关联的游戏属性
blue_object.position[0] += blue_object["x_change"]
blue_object.position[1] += blue_object["y_change"]

# 检查物体是否到达了边缘
# 如果是的，反转方向。4个边缘都要囊括。
if blue_object.position[0] > 6 and blue_object["x_change"] > 0:
blue_object["x_change"] *= -1

if blue_object.position[0] < -6 and blue_object["x_change"] < 0:
blue_object["x_change"] *= -1

if blue_object.position[1] > 6 and blue_object["y_change"] > 0:
blue_object["y_change"] *= -1

if blue_object.position[1] < -6 and blue_object["y_change"] < 0:
blue_object["y_change"] *= -1


Blender可以在这里下载:
http://www.blender.org/