Python 在pygame中使块弹离直线

Python 在pygame中使块弹离直线,python,pygame,python-3.4,Python,Pygame,Python 3.4,运行下面的代码可以让3个方块在屏幕上移动,然后它们可以自由漫游,从边缘反弹,直到它们遇到一条蓝线。他们也受到严格的命令,要跳出这条线。我能做到这一点,当方块从右边来,从线的右边反弹,但不是从左边来,从线的左边反弹。有关更多信息,请参阅代码 import pygame, sys, time from pygame.locals import * pygame.init() WINDOWWIDTH = 200 WINDOWHEIGHT = 300 screen = pygame.display.s

运行下面的代码可以让3个方块在屏幕上移动,然后它们可以自由漫游,从边缘反弹,直到它们遇到一条蓝线。他们也受到严格的命令,要跳出这条线。我能做到这一点,当方块从右边来,从线的右边反弹,但不是从左边来,从线的左边反弹。有关更多信息,请参阅代码

import pygame, sys, time
from pygame.locals import *

pygame.init()
WINDOWWIDTH = 200
WINDOWHEIGHT = 300
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Animation')

DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9

MOVESPEED = 1

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


b1 = {'rect':pygame.Rect(0, 50, 25, 25), 'color':RED, 'dir':DOWNRIGHT}
b2 = {'rect':pygame.Rect(0, 100, 25, 25), 'color':GREEN, 'dir':DOWNRIGHT}
b3 = {'rect':pygame.Rect(0, 150, 25, 25), 'color':BLUE, 'dir':DOWNRIGHT}
blocks = [b1, b2, b3]

while True:
    # check for the closing of the 'x' button
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.fill(BLACK)
    pygame.draw.line(screen,BLUE,(150,0),(150,150),5)


    for b in blocks:
        #moves the blocks
        if b['dir'] == DOWNLEFT:
            b['rect'].left -= MOVESPEED
            b['rect'].top += MOVESPEED
        if b['dir'] == DOWNRIGHT:
            b['rect'].left += MOVESPEED
            b['rect'].top += MOVESPEED
        if b['dir'] == UPLEFT:
            b['rect'].left -= MOVESPEED
            b['rect'].top -= MOVESPEED
        if b['dir'] == UPRIGHT:
            b['rect'].left += MOVESPEED
            b['rect'].top -= MOVESPEED

        # check if the block has move out of the window
        if b['rect'].top < 0:
            # block has moved past the top
            if b['dir'] == UPLEFT:
                b['dir'] = DOWNLEFT
            if b['dir'] == UPRIGHT:
                b['dir'] = DOWNRIGHT
        if b['rect'].bottom > WINDOWHEIGHT:
            # block has moved past the bottom
            if b['dir'] == DOWNLEFT:
                b['dir'] = UPLEFT
            if b['dir'] == DOWNRIGHT:
                b['dir'] = UPRIGHT
        if b['rect'].left < 0:
            # block has moved past the left side
            if b['dir'] == DOWNLEFT:
                b['dir'] = DOWNRIGHT
            if b['dir'] == UPLEFT:
                b['dir'] = UPRIGHT
        if b['rect'].right > WINDOWWIDTH:
            # block has moved past the right side
            if b['dir'] == DOWNRIGHT:
                b['dir'] = DOWNLEFT
            if b['dir'] == UPRIGHT:
                b['dir'] = UPLEFT
        if b['rect'].left < 150 and b['rect'].top > 0 and b['rect'].top < 150:
            if b['dir'] == DOWNLEFT:
                b['dir'] = DOWNRIGHT
            if b['dir'] == UPLEFT:
                b['dir'] = UPRIGHT
##        if b['rect'].right > 150 and b['rect'].top > 0 and b['rect'].top < 150:
##            if b['dir'] == DOWNLEFT:
##                b['dir'] = DOWNRIGHT
##            if b['dir'] == UPLEFT:
##                b['dir'] = UPRIGHT
        print(b['rect'].right)
        pygame.draw.rect(screen, b['color'], b['rect'])

    pygame.display.update()
    time.sleep(0.0001)
import pygame,sys,time
从pygame.locals导入*
pygame.init()
窗宽=200
窗高=300
screen=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
pygame.display.set_标题('动画')
左下=1
彻头彻尾=3
英尺=7
直立=9
移动速度=1
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
b1={'rect':pygame.rect(0,50,25,25),'color':RED,'dir':DOWNRIGHT}
b2={'rect':pygame.rect(0,100,25,25),'color':绿色,'dir':完全正确的}
b3={'rect':pygame.rect(0,150,25,25),'color':BLUE,'dir':DOWNRIGHT}
区块=[b1、b2、b3]
尽管如此:
#检查“x”按钮是否关闭
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
屏幕填充(黑色)
pygame.draw.line(屏幕,蓝色,(150,0),(150150),5)
对于块中的b:
#移动积木
如果b['dir']==左下角:
b['rect'].左-=移动速度
b['rect'].top+=MOVESPEED
如果b['dir']==完全正确:
b['rect'].左+=移动速度
b['rect'].top+=MOVESPEED
如果b['dir']==UPLEFT:
b['rect'].左-=移动速度
b['rect'].top-=移动速度
如果b['dir']==竖直:
b['rect'].左+=移动速度
b['rect'].top-=移动速度
#检查挡块是否已移出车窗
如果b['rect'].top<0:
#积木已移过顶部
如果b['dir']==UPLEFT:
b['dir']=左下角
如果b['dir']==竖直:
b['dir']=完全正确
如果b['rect'].bottom>WINDOWHEIGHT:
#积木已移过底部
如果b['dir']==左下角:
b['dir']=1英尺
如果b['dir']==完全正确:
b['dir']=直立
如果b['rect'].左<0:
#街区已移过左侧
如果b['dir']==左下角:
b['dir']=完全正确
如果b['dir']==UPLEFT:
b['dir']=直立
如果b['rect'].right>WINDOWWIDTH:
#街区已移过右侧
如果b['dir']==完全正确:
b['dir']=左下角
如果b['dir']==竖直:
b['dir']=1英尺
如果b['rect'].left<150且b['rect'].top>0且b['rect'].top<150:
如果b['dir']==左下角:
b['dir']=完全正确
如果b['dir']==UPLEFT:
b['dir']=直立
##如果b['rect'].right>150且b['rect'].top>0且b['rect'].top<150:
##如果b['dir']==左下角:
##b['dir']=完全正确
##如果b['dir']==UPLEFT:
##b['dir']=直立
打印(b['rect']右)
pygame.draw.rect(屏幕,b['color'],b['rect']))
pygame.display.update()
睡眠时间(0.0001)
导入pygame、系统、时间
从pygame.locals导入*
pygame.init()
窗宽=200
窗高=300
screen=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
pygame.display.set_标题('动画')
左下=1
彻头彻尾=3
英尺=7
直立=9
移动速度=1
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
b1={'rect':pygame.rect(0,50,25,25),'color':RED,'dir':DOWNRIGHT}
b2={'rect':pygame.rect(0,100,25,25),'color':绿色,'dir':完全正确的}
b3={'rect':pygame.rect(0,150,25,25),'color':BLUE,'dir':DOWNRIGHT}
区块=[b1、b2、b3]
尽管如此:
#检查“x”按钮是否关闭
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
屏幕填充(黑色)
pygame.draw.line(屏幕,蓝色,(150,0),(150150),5)
对于块中的b:
#移动积木
如果b['dir']==左下角:
b['rect'].左-=移动速度
b['rect'].top+=MOVESPEED
如果b['dir']==完全正确:
b['rect'].左+=移动速度
b['rect'].top+=MOVESPEED
如果b['dir']==UPLEFT:
b['rect'].左-=移动速度
b['rect'].top-=移动速度
如果b['dir']==竖直:
b['rect'].左+=移动速度
b['rect'].top-=移动速度
#检查挡块是否已移出车窗
如果b['rect'].top<0:
#积木已移过顶部
如果b['dir']==UPLEFT:
b['dir']=左下角
如果b['dir']==竖直:
b['dir']=完全正确
如果b['rect'].bottom>WINDOWHEIGHT:
#积木已移过底部
如果b['dir']==左下角:
b['dir']=1英尺
如果b['dir']==完全正确:
b['dir']=直立
如果b['rect'].左<0:
#街区已移过左侧
如果b['dir']==左下角:
b['dir']=完全正确
如果b['dir']==UPLEFT:
b['dir']=直立
如果b['rect'].right>WINDOWWIDTH:
#街区已移过右侧
如果b['dir']==完全正确:
b['dir']=左下角
如果b['dir']==竖直:
b['dir']=1英尺
如果b['dir']==UPLEFT或b['dir']==downlight:#在这种情况下,很容易检查应进行方向更改的确切条件。由于移动速度仅为1,因此在某个点上b['rect'],右/左正好为150。检查这一点比处理大于或小于的问题更容易
import pygame, sys, time
from pygame.locals import *

pygame.init()
WINDOWWIDTH = 200
WINDOWHEIGHT = 300
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Animation')

DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9

MOVESPEED = 1

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


b1 = {'rect':pygame.Rect(0, 50, 25, 25), 'color':RED, 'dir':DOWNRIGHT}
b2 = {'rect':pygame.Rect(0, 100, 25, 25), 'color':GREEN, 'dir':DOWNRIGHT}
b3 = {'rect':pygame.Rect(0, 150, 25, 25), 'color':BLUE, 'dir':DOWNRIGHT}
blocks = [b1, b2, b3]


while True:
# check for the closing of the 'x' button
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.fill(BLACK)
    pygame.draw.line(screen,BLUE,(150,0),(150,150),5)


    for b in blocks:
    #moves the blocks
        if b['dir'] == DOWNLEFT:
            b['rect'].left -= MOVESPEED
            b['rect'].top += MOVESPEED
        if b['dir'] == DOWNRIGHT:
            b['rect'].left += MOVESPEED
            b['rect'].top += MOVESPEED
        if b['dir'] == UPLEFT:
            b['rect'].left -= MOVESPEED
            b['rect'].top -= MOVESPEED
        if b['dir'] == UPRIGHT:
            b['rect'].left += MOVESPEED
            b['rect'].top -= MOVESPEED

    # check if the block has move out of the window
        if b['rect'].top < 0:
        # block has moved past the top
            if b['dir'] == UPLEFT:
                b['dir'] = DOWNLEFT
            if b['dir'] == UPRIGHT:
                b['dir'] = DOWNRIGHT
        if b['rect'].bottom > WINDOWHEIGHT:
        # block has moved past the bottom
            if b['dir'] == DOWNLEFT:
                b['dir'] = UPLEFT
            if b['dir'] == DOWNRIGHT:
                b['dir'] = UPRIGHT
        if b['rect'].left < 0:
        # block has moved past the left side
            if b['dir'] == DOWNLEFT:
                b['dir'] = DOWNRIGHT
            if b['dir'] == UPLEFT:
                b['dir'] = UPRIGHT
        if b['rect'].right > WINDOWWIDTH:
        # block has moved past the right side
            if b['dir'] == DOWNRIGHT:
                b['dir'] = DOWNLEFT
            if b['dir'] == UPRIGHT:
                b['dir'] = UPLEFT
        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT: # In this case it's rather easy to check for the exact conditions when direction changes should be made. Being the movement speed is only 1, at some point the b['rect'].right/left will be exactly 150. It's easier to check for that than deal with greater than less than problems which tend to only work for one way. If movement was higher than one you'd have to switch to short ranges instead.
            if b['rect'].left == 150 and b['rect'].top > 0 and b['rect'].top < 150:
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT

        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if b['rect'].right == 150 and b['rect'].top < 150:
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT
        print(b['rect'].right)
        pygame.draw.rect(screen, b['color'], b['rect'])

    pygame.display.update()
    time.sleep(0.00001)