如何从主脚本将导入的模块 blit 到表面上? [英] How to make an imported module blit onto a surface from the main script?

查看:58
本文介绍了如何从主脚本将导入的模块 blit 到表面上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my main file I have a surface called win. I import a module like so from draw import *.

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

这是draw 中的一个函数.它不起作用,因为 win 未定义.如何定义?

This is a function from draw. It doesn't work because win is not defined. How to make it defined?

推荐答案

向函数中添加目标表面的附加参数:

Add an additional parameter for the target surface to the function:

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

win传递给draw函数:

draw(win, image, x, y)


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


Alternatively, you can create a variable in the module's global namespace:

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)

这篇关于如何从主脚本将导入的模块 blit 到表面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆