Использование метода из импортированного класса для вызова переменной в пустом объекте init () - PullRequest
0 голосов
/ 20 мая 2019

Где написано «Корабль = Корабль (экран)» в alieninvasion.py, когда я использую класс «Корабль» для вызова (экрана), что именно происходит?Когда вызывается (screen), вызывает ли он пустой объект init () и добавляет на него «корабль»?Как именно корабль добавляется на экран?Я немного запутался, извините за глупый вопрос.

Это alieninavasion.py

import sys

import pygame

from settings import Settings
from ship import Ship

def run_game():

    # Initialize pygame, settings, and screen object.
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode(
    (ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

    #Make a ship.
    ship = Ship(screen)

    #Set the background color.
    bg_color = (230, 230, 230)

    # Start the main loop for the game.
    while True:

        # Watch for the keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # Redraw the screen during each pass through the loop.
        screen.fill(ai_settings.bg_color)
        ship.blitme()

        #Make the most recently drawn screen visible.
        pygame.display.flip()

run_game()

Это Ship.py, чтобы сделать его понятнее

import pygame

class Ship():

    def __init__(self, screen):
        """Initialize the ship and set its starting position."""
        self.screen = screen

        #Load the ship image and get its rect.
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        #Start each new ship at the bottom center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

    def blitme(self):
        """Draw the ship at its current location"""
        self.screen.blit(self.image, self.rect)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...