Python: how to bounce off the screen

Well, I use an invented way to teach myself how to code. I am trying to reuse the code in my own way to understand how it works, but this part gives me problems:

in Chapter 17 There is a set of animation codes in which windows with different sizes bounce off the screen

for b in blocks:
    # move the block data structure
    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

I want to create it so that my block moves left and right and bounces on each side of the screen.

however, when I try to change my code to this code, all that happens is a block that crashes from the screen without bouncing. We tried several options, with all the same result. Currently it looks like this: Edit: update for full code

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

# set up pygame
pygame.init()

# set up the window
WINDOWWIDTH = 480
WINDOWHEIGHT = 800
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Jumper')

#Directions
LEFT = 4
RIGHT = 6
UP = 8
DOWN = 2

MOVESPEED = 4

# set up the colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


b1 = {'rect':pygame.Rect(240, 700, 20, 20), 'color':GREEN, 'dir':LEFT}
blocks = [b1]

# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()

# draw the black background onto the surface
windowSurface.fill(BLACK)

for b in blocks:
    # move the block data structure
    if b['dir'] == LEFT:
        b['rect'].left -= MOVESPEED
    if b['dir'] == RIGHT:
        b['rect'].left += MOVESPEED

        if b['rect'].left < 0:
             b['dir'] = RIGHT


        if b['rect'].right > WINDOWWIDTH:
                b['dir'] = LEFT

             # draw the block onto the surface
    pygame.draw.rect(windowSurface, b['color'], b['rect'])

# draw the window onto the screen
pygame.display.update()
time.sleep(0.02)
+4
1

- . , :

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

# set up pygame
pygame.init()

# set up the window
WINDOWWIDTH = 480
WINDOWHEIGHT = 800
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Jumper')

#Directions
LEFT = 4
RIGHT = 6
UP = 8
DOWN = 2

MOVESPEED = 4

# set up the colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


b1 = {'rect':pygame.Rect(240, 700, 20, 20), 'color':GREEN, 'dir':LEFT}
blocks = [b1]

# run the game loop
while True:
    # check for the QUIT event
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # draw the black background onto the surface
    windowSurface.fill(BLACK)

    for b in blocks:
        # move the block data structure
        if b['dir'] == LEFT:
            b['rect'].left -= MOVESPEED
        if b['dir'] == RIGHT:
            b['rect'].left += MOVESPEED

        if b['rect'].left < 0:
             b['dir'] = RIGHT
        if b['rect'].right > WINDOWWIDTH:
                b['dir'] = LEFT

        # draw the block onto the surface
        pygame.draw.rect(windowSurface, b['color'], b['rect'])

    # draw the window onto the screen
    pygame.display.update()

time.sleep(0.02)

Python , , . # , , , . Python . ( - , .)

, , .

. Inventing With Python - ... , .

, , , . ?

. DOWNLEFT, DOWNRIGHT, UPLEFT, UPRIGHT , , .

, , , : " ?"

, - .

, , , , .

, DOWNLEFT .. ? ? , .

, , : .

, /? , x y. , x=0, y=0, , , , , .

, ! DOWNRIGHT x=1, y=1, UPLEFT x=-1, y=-1 ..

. , (if b['rect'].left < 0). , DOWNLEFT DOWNRIGHT UPLEFT UPRIGHT, y x=-1 x=1.

, , x=1 x=-1. , x -1.

, : , , x -1. y.

, , :

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

class Block( object ):
    def __init__( self, rect, color, dir ):
        self.rect = rect
        self.color = color
        self.dir = dir
    def move( self ):
        # reverse direction if the block will move out of the window
        if self.rect.left < SPEED or self.rect.right > WIN_WIDTH - SPEED:
            self.dir.x *= -1
        if self.rect.top < SPEED or self.rect.bottom > WIN_HEIGHT - SPEED:
            self.dir.y *= -1
        # move the block
        self.rect.left += self.dir.x * SPEED
        self.rect.top += self.dir.y * SPEED
    def draw( self ):
        pygame.draw.rect( windowSurface, self.color, self.rect )

class Direction( object ):
    def __init__( self, x, y ):
        self.x = x
        self.y = y

# set up pygame
pygame.init()

# set up the window
WIN_WIDTH = 400
WIN_HEIGHT = 400
windowSurface = pygame.display.set_mode( ( WIN_WIDTH, WIN_HEIGHT ), 0, 32 )
pygame.display.set_caption( 'Animation' )

# set up the movement speed
SPEED = 4

# set up the colors
BLACK = ( 0, 0, 0 )
RED = ( 255, 0, 0 )
GREEN = ( 0, 255, 0 )
BLUE = ( 0, 0, 255 )

# set up the block objects
blocks = [
    Block( pygame.Rect( 300, 80, 50, 100 ), RED, Direction( -1, 1 ) ),
    Block( pygame.Rect( 200, 200, 20, 20 ), GREEN, Direction( -1, -1 ) ),
    Block( pygame.Rect( 100, 150, 60, 60 ), BLUE, Direction( 1, -1 ) ),
]

# run the game loop
while True:
    # check for the QUIT event
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # draw the black background onto the surface
    windowSurface.fill( BLACK )

    # move and draw each block
    for block in blocks:
        block.move()
        block.draw()

    # draw the window onto the screen
    pygame.display.update()
    time.sleep( 0.02 )

, . , " " .

#. Python - , __init__() - , . , - .

. , , , . , , if self.rect.left < SPEED if self.rect.left < 0.

+2

Source: https://habr.com/ru/post/1584751/


All Articles