【问题标题】:How to make an imported module blit onto a surface from the main script?如何从主脚本将导入的模块 blit 到表面上?
【发布时间】:2021-01-07 22:10:26
【问题描述】:

在我的主文件中,我有一个名为 win 的表面。我像from draw import * 这样导入一个模块。

def draw(image,x,y):
    win.blit(image,(x,y))

这是来自draw 的函数。它不起作用,因为未定义 win。如何定义?

【问题讨论】:

    标签: python module pygame undefined blit


    【解决方案1】:

    为函数添加目标曲面的附加参数:

    def draw(target, image, x, y):
        target.blit(image, (x, y))
    

    win 传递给draw 函数:

    draw(win, image, x, y)
    

    或者,您可以在模块的全局命名空间中创建一个变量:

    draw.py

    traget = None
    
    def draw(image, x, y):
        traget.blit(image, (x, y))
    

    使用模块:

    import pygame
    import draw
    
    # [...]
    
    draw.target = win
    
    # [...]
    
    draw.draw(image, x, y)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-24
      • 2012-02-21
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      相关资源
      最近更新 更多