使用导入的类中的方法在空的init()对象中调用变量

时间:2019-05-20 18:20:34

标签: python pygame

当im使用“ Ship”类调用(屏幕)时,在Alieninvasion.py中显示ship = Ship(屏幕)的确切位置是什么?当(screen)被调用时,它是在调用空的init()对象并在其上添加“ ship”吗?究竟将飞船添加到屏幕上的程度如何?对于这种愚蠢的问题,我有点困惑。

这是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)

0 个答案:

没有答案