S. Walter
S. Walter

Reputation: 1

how do you create multiple rectangles and make them move independently

I am trying to create a flappy bird game with python but i cant get multiple walls to appear on the page and move across.

I was wondering if there was a way to make multiple walls (rectangles) and move them without defining them individually. Im using Pygame.

import pygame

pygame.init()

white = (255,255,255)
yellow = (255,200,0)
green = (0,255,0)

displayHeight = 600
displayWidth = 500

gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
clock = pygame.time.Clock()

crash = False

def bird(b_X, b_Y, b_R, b_Colour):
    pygame.draw.circle(gameDisplay, b_Colour, (b_X, b_Y), b_R)

def game_loop():

    bX = 50
    bY = 300
    bR = 20
    bColour = yellow

    velocity = 0
    gravity = 0.6
    lift = -15

    while crash == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.QUIT
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    velocity += lift

        gameDisplay.fill(white)

        bird(bX, bY, bR, bColour)

        velocity = velocity + gravity
        bY += velocity
        bY = round(bY)

        if bY > displayHeight:
            bY = displayHeight
            velocity = 0

        if bY < 0:
            bY = 0
            velocity = 0

        pygame.display.update()

        clock.tick(60)

game_loop()
pygame.quit()
quit()

Is there a way to make a big population of blocks and move them individually without defining them one by one.

Upvotes: 0

Views: 375

Answers (1)

Veztar
Veztar

Reputation: 39

I do not know how the code exactly works out, but you can store multiple wall 'objects' in a list, and looping over that list every time you update the screen. In pseudocode, it works like this

wall-list = []
for wanted_wall_amount in range():
wall = create_a_wall
    wall-list.append(wall)

game loop:
    for wall in wall-list:
        wall.x_coordinate += movespeed

because of the for loop within the game loop, every wall object you stored will update it's position, or whatever you want to move simultanously. I hope you understand the weird pseudocode

Upvotes: 1

Related Questions