python:同一根目录下不同的py文件,为什么不用导入却可以访问并修改其他py文件中类的属性值?

同一根目录下不同的py文件,为什么不用导入却可以访问并修改其他py文件中类的属性值?

例如从入门到实践中的12.5章节game_function模块访问修改ship模块下的movin_right属性值:
game_function模块代码如下:

import sys
import pygame
from bullet import Bullet
#此处并未导入ship

def check_keydown_event(event,ai_settings,screen,ship,bullets):
    """相应按键事件"""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_UP:
        ship.moving_up = True
    elif event.key == pygame.K_DOWN:
        ship.moving_down = True
        #修改了ship中的属性值
    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings,screen,ship,bullets)



def check_keyup_event(event,ship):
    """相应松开事件"""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False
    elif event.key == pygame.K_UP:
        ship.moving_up = False
    elif event.key == pygame.K_DOWN:
        ship.moving_down = False


def check_events(ai_settings,screen,ship,bullets):
    """响应按键和鼠标事件"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_event(event,ai_settings,screen,ship,bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_event(event,ship)


def update_screen(ai_settings,screen,ship,bullets):
    """更新屏幕的图像并切换到新屏幕"""
    #每次循环时都重新绘制屏幕
    screen.fill(ai_settings.bg_color)
    #在飞船和外星人后面重绘所有子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    ship.blitme()
    #让最近绘制的屏幕可见
    pygame.display.flip()

def update_bullets(bullets):
    """更新子弹位置,并删除已消失的子弹"""
    #更新子弹位置
    bullets.update()

    #删除已消失的子弹
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)


def fire_bullet(ai_settings,screen,ship,bullets):
    """如果子弹还没达到上限,就发射一个子弹"""
    if len(bullets) < ai_settings.bullets_allowed:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

ship模块代码如下:

import pygame

class Ship():

    def __init__(self,ai_settings,screen):
        """初始化飞船并初始化其位置"""
        self.screen = screen

        #加载飞船图像并获取其外接矩形
        self.image = pygame.image.load('images/ship.bmp')#加载图片
        self.rect = self.image.get_rect()#图片赋予给显示矩形
        self.screen_rect = screen.get_rect()#将显示矩形赋予给screen_rect
        self.ai_settings = ai_settings#引用settings

        #将每艘船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx #飞船的中心x坐标 设置为表示屏幕的矩形属性cenrerx
        self.rect.bottom = self.screen_rect.bottom #飞船的下边缘y坐标 设置为表示屏幕的矩形属性bottom
        #centerx-中心线,bottom-底部

        #在飞船的属性centen存储小数值
        self.center = float(self.rect.centerx)


        #移动标志
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """根据标志移动飞船位置"""
        #更新飞船的centerx值而不是rect

        if self.moving_right and self.rect.right <  self.screen_rect.right:
            self.center += self.ai_settings.ship_speed_factor


        if self.moving_left and self.rect.left >  0:
            self.center -= self.ai_settings.ship_speed_factor


        if self.moving_up:
            self.rect.top -= self.ai_settings.ship_speed_factor
            if self.rect.top < 0:
                self.rect.top = self.screen_rect.top


        if self.moving_down:
            self.rect.bottom += self.ai_settings.ship_speed_factor
            if self.rect.bottom > 700:
                self.rect.bottom = self.screen_rect.bottom

        #根据self.centerx更新rect对象
        self.rect.centerx = self.center



    def blitme(self):
        """在指定位置绘制飞船"""
        self.screen.blit(self.image,self.rect)

1个回答

亲,文件一中的ship是一个函数参数,他的具体属性和调用是具体传入的参数相关
不是指的ship模块

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问