Как выкладывать некоторые объекты на весь экран - PullRequest
1 голос
/ 19 октября 2019

Я учусь кодировать в Pygame. И есть задача распределить звезды по всему экрану.

Я сделал свой код, но он распространяет звезды только по левому верхнему углу экрана, и я действительно не знаю, как разместить звезды навесь экран. Этот код ответа для распространения:

def x_demension(custom_settings, star):
    available_x = custom_settings.width - 2*star.rect.width #available space along abscissa axis
    count_numbers = int(available_x / (2*star.rect.width)) #quantity of stars throught weight
    return count_numbers


def y_demension(custom_settings, star, spaceship):
    available_y = custom_settings.height - 2*star.rect.height - spaceship.rect.height #available space along ordinate axis
    number_rows = int(available_y / (2*star.rect.height)) #quantity of rows throught height
    return number_rows


def create_star(screen, custom_settings, number, row_number, stars):
    star = Star(screen, custom_settings) #create an instance of a star

    star.rect.x = star.rect.width + 2*star.rect.width * randint(0,number) #coordinate x of a star
    x = randint(0, star.rect.x)
    star.rect.x = x

    star.rect.y = star.rect.height + 2*star.rect.height * randint(0,row_number) #coordinate y of a star
    y = randint(0,star.rect.y)
    star.rect.y = y

    stars.add(star) #added stars
    if len(stars) > 50: #limiter of the star adding
        stars.remove(star)
    else:
        stars.add(star)


def starsky(custom_settings, screen, spaceship, stars):
    star = Star(custom_settings, screen) #created as an instance of a random star
    number_stars = x_demension(custom_settings, star)
    number_rows = y_demension(custom_settings, star, spaceship)
    for row_number in range(number_rows):
        for number in range(number_stars):
            create_star(screen, custom_settings, number, row_number, stars)

Это то, что я получаю

1 Ответ

1 голос
/ 19 октября 2019

Если я что-то упустил, разве это не должно быть просто:

def create_star(screen, custom_settings, number, row_number, stars):
    star = Star(screen, custom_settings)
    w, h = pygame.display.get_surface().get_size()
    x = randint(0, w)
    star.rect.x = x
    y = randint(0, h)
    star.rect.y = y

    stars.add(star) #added stars
    if len(stars) > 50: #limiter of the star adding
        stars.remove(star)
    else:
        stars.add(star)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...