python 小程序

用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)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值