用python写的一个小程序:
import pygame, sys, time
from pygame.locals import*
pygame.init()
W_WIDTH = 400
W_HEIGHT = 400
surface = pygame.display.set_mode((W_WIDTH,W_HEIGHT), 0, 32 )
pygame.display.set_caption('across')
DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9
MOVESPEED = 4
BLACK = (0, 0, 0)
RED = (255, 0, 0)
x = 125
y = 200
b1 = {'rect': pygame.Rect(x , y, 100, 10), 'color':RED, 'dir': UPLEFT}
b2 = {'rect':pygame.Rect(x, y, 10, 200), 'color':RED, 'dir':UPLEFT}
b3= {'rect1': pygame.Rect(x , y,10 ,10 ), 'color':RED, 'dir1': UPRIGHT}
blocks1 = [b1, b2]
blocks2 = [b3]
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
surface.fill(BLACK)
# draw the block onto the surface
flag = True
for a in blocks1:
for b in blocks2: # change x, y value
if b['dir1'] == UPRIGHT:
b['rect1'].left += MOVESPEED
b['rect1'].top -= MOVESPEED
x = b['rect1'].left
y = b['rect1'].top
elif b['dir1'] == DOWNRIGHT:
b['rect1'].left += MOVESPEED
b['rect1'].top += MOVESPEED
x = b['rect1'].left
y = b['rect1'].top
elif b['dir1'] == UPLEFT:
b['rect1'].left -= MOVESPEED
b['rect1'].top -= MOVESPEED
x = b['rect1'].left
y = b['rect1'].top
elif b['dir1'] == DOWNLEFT:
b['rect1'].left -= MOVESPEED
b['rect1'].top += MOVESPEED
x = b['rect1'].left
y = b['rect1'].top
if a['rect'].top < 0:
if b['dir1'] == UPLEFT:
b['dir1'] = DOWNLEFT
if b['dir1'] == UPRIGHT:
b['dir1'] = DOWNRIGHT
if a['rect'].bottom > W_HEIGHT:
# block has moved past the bottom
if b['dir1'] == DOWNLEFT:
b['dir1'] = UPLEFT
if b['dir1'] == DOWNRIGHT:
b['dir1'] = UPRIGHT
if a['rect'].left < 0:
# block has moved past the left side
if b['dir1'] == DOWNLEFT:
b['dir1'] = DOWNRIGHT
if b['dir1'] == UPLEFT:
b['dir1'] = UPRIGHT
if a['rect'].right > W_WIDTH:
# block has moved past the right side
if b['dir1'] == DOWNRIGHT:
b['dir1'] = DOWNLEFT
if b['dir1'] == UPRIGHT:
b['dir1'] = UPLEFT
if flag:
a['rect'].left = x - 45
a['rect'].top = y
flag = False
else:
a['rect'].left = x
a['rect'].top = y - 50
flag = False
pygame.draw.rect(surface, a['color'], a['rect'])
# draw the window onto the screen
pygame.display.update()
time.sleep(0.04)