简体   繁体   English

如何从主脚本将导入的模块 blit 到表面上?

[英]How to make an imported module blit onto a surface from the main script?

In my main file I have a surface called win .在我的主文件中,我有一个名为win的表面。 I import a module like so from draw import * .from draw import *导入一个像这样的模块。

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

This is a function from draw .这是来自draw的 function。 It doesn't work because win is not defined.它不起作用,因为win没有定义。 How to make it defined?如何定义?

Add an additional parameter for the target surface to the function:将目标曲面的附加参数添加到 function:

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

Pass win to the draw function:win传递给draw function:

draw(win, image, x, y)

Alternatively, you can create a variable in the module's global namespace:或者,您可以在模块的全局命名空间中创建一个变量:

draw.py绘制.py

traget = None

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

Use the module:使用模块:

import pygame
import draw

# [...]

draw.target = win

# [...]

draw.draw(image, x, y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM