Azurite
Azurite

Reputation: 33

How do I make randomly sized shapes fall from the top to the bottom of the screen?

I'm trying to make a shoot 'em up-like game in Python with pygame. I'm pretty new to Python and I'm not sure how do I make objects fall from the top of my screen, anyway, here's my code (I'm using Python 3.6):

import pygame
import random
pygame.init()

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

width=600
height=650

screen=pygame.display.set_mode((width,height))
pygame.display.set_caption('Videojuego')
done=False
screen.fill(WHITE)
pygame.display.flip()

background_image=pygame.image.load('fondo.jpg').convert()
screen.blit(background_image,[0,0])

player_image=pygame.image.load('rocket.png').convert()
player_image=pygame.transform.scale(player_image,(50,75))
pygame.display.flip()

pygame.mouse.set_visible(False)

count=0
clock=pygame.time.Clock()

rect_x=random.randrange(0,580)
rect_y=random.randrange(0,2)
rect_chx=5

size1=random.randrange(10,70)
size2=random.randrange(15,84)

while(not done):
    for event in pygame.event.get():
    if(event.type==pygame.QUIT):
        print('Quit')
        done=True
    else:
        done=False
    screen.blit(background_image,[0,0])
    player_position=pygame.mouse.get_pos()
    x=player_position[0]
    y=player_position[1]
    screen.blit(player_image,(x,y))
    fig=pygame.draw.rect(screen,RED,[rect_x,rect_y,size1,size2])
    if(rect_y>550 or rect_y<5):
        rect_chx=rect_chx*1
    rect_y+=rect_chx
    pygame.display.flip()
    clock.tick(60)

 pygame.quit()

This code only makes one rectangle fall, and I don't know how to make more do so.

Upvotes: 3

Views: 1551

Answers (1)

Adrien Logut
Adrien Logut

Reputation: 820

You can create a simple class Rect that will encapsulate a rectangle, and print it into the screen:

class Rect():
    def __init__(self):
        self.x=random.randrange(0,580)
        self.y=random.randrange(0,2)

        self.size_x=random.randrange(10,70)
        self.size_y=random.randrange(15,84)

    def update(self, screen, fall_speed):
        self.y += fall_speed
        pygame.draw.rect(screen, RED, [self.x, self.y, self.size_x, self.size_y])

Then you can create some Rect into a list : rect_list = [Rect() for _ in range(10)] (change 10 by the number of Rect you want).

Finally, you iterate over your list at each loop:

while(not done):
    for event in pygame.event.get():
    if(event.type==pygame.QUIT):
        print('Quit')
        done=True
    screen.blit(background_image,[0,0])
    player_position=pygame.mouse.get_pos()
    x=player_position[0]
    y=player_position[1]
    screen.blit(player_image,(x,y))
    for rect in rect_list:
        # Here 5 is the rect_chx, or fall speed
        rect.update(screen, 5)
    pygame.display.flip()
    clock.tick(60)

This will create 10 rect right at the beginning. You want perhaps to detect when a Rect is at the bottom and then remove it from the list (it won't be displayed anymore), or add new Rect to the list to make them pop at the top.


I recommend you to learn how to do basic python with functions, classes, loops and lists. This is mandatory if you want to create games, even the smallest ones.

Upvotes: 2

Related Questions