导入类时没有定义error全局变量 [英] Error Global Variable not defined when importing class

查看:185
本文介绍了导入类时没有定义error全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个运动系统,玩家的举动是朝着点击位置。但我一直运行到一些问题的运动方法的参数。

I am trying to implement a movement system where the player move's towards the click position. But I have been running into some problems with the arguments of the movement method.

运动方法目前需要采取所有的变量作为参数:

The movement method currently needs to take all the variables as arguments:

这code工作:

def move(self,event,mouse_pos, screen, background):
        if event.type == MOUSEBUTTONDOWN:
            if mouse_pos[1] > self.pos[1]:
                screen.blit(background, self.pos, self.pos) #erases players by bliting bg 
                self.speed = 1
                self.move_south() #moves player
            if mouse_pos[0] > self.pos[0]:
                screen.blit(background, self.pos, self.pos) #erases players by bliting bg 
                self.speed = 1
                self.move_east() #moves player
            screen.blit(self.image, self.pos) #draws player  

这code不工作:

def move(self,event):
        if event.type == MOUSEBUTTONDOWN:
            if mouse_pos[1] > self.pos[1]:
                screen.blit(background, self.pos, self.pos) #erases players by bliting bg 
                self.speed = 1
                self.move_south() #moves player
            if mouse_pos[0] > self.pos[0]:
                screen.blit(background, self.pos, self.pos) #erases players by bliting bg 
                self.speed = 1
                self.move_east() #moves player
            screen.blit(self.image, self.pos) #draws player   

全部类code:

Full Class code:

import pygame, sys
from pygame.locals import *

class GameObject:
    def __init__(self, image, height, speed):
        self.speed = speed
        self.image = image
        self.pos = image.get_rect().move(0, height) #initial placement

    def move_south(self):
        self.pos = self.pos.move(0, self.speed)
        if self.pos.right > 600:
            self.pos.left = 0

    def move_east(self):
        self.pos = self.pos.move(self.speed , 0)
        if self.pos.right > 600:
            self.pos.left = 0

    def move(self,event,mouse_pos, screen, background):
            if event.type == MOUSEBUTTONDOWN:
                if mouse_pos[1] > self.pos[1]:
                    screen.blit(background, self.pos, self.pos) #erases players by bliting bg 
                    self.speed = 1
                    self.move_south() #moves player
                if mouse_pos[0] > self.pos[0]:
                    screen.blit(background, self.pos, self.pos) #erases players by bliting bg 
                    self.speed = 1
                    self.move_east() #moves player
                screen.blit(self.image, self.pos) #draws player      

主要脚本code:

main script code:

import pygame, sys
from pygame.locals import *
from classes import *

screen = pygame.display.set_mode((640, 480))
#Importing Chars
player = pygame.image.load('green_hunter_small.png').convert()
#player.set_alpha(100) #makes whole player transparent
player.set_colorkey((0,0,0)) #sets background colour to transparent

ennemi =  pygame.image.load('red_hunter_small.png').convert()
ennemi.set_colorkey((0,0,0))

background = pygame.image.load('grass_map_640x640.png').convert()
screen.blit(background, (0, 0))
objects = []
mouse_pos = (320, 240)
objects.append(GameObject(player, 80, 0))
for x in range(2):      #create 2 objects
    o = GameObject(ennemi, x*40, 0)
    objects.append(o)
while True:
    for event in pygame.event.get(): #setting up quit
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN :
            mouse_pos = pygame.mouse.get_pos()
            print mouse_pos
            for o in objects:
                screen.blit(background, o.pos, o.pos) #erases players by bliting bg 
            for o in objects:
                o.move(event,mouse_pos,screen,background)
    pygame.display.update()
    pygame.time.delay(100)

我有两个问题:
1)如何减少参数的个数为我的方法是什么?
2)如何让我的球员不断朝着点击位置移动,而不是在一次,每次点击?

I've got two questions : 1) how to reduce the number of arguments for my method ? 2) how to get my player to move continually towards the click position rather than once at every click ?

推荐答案

您的问题是,蟒蛇全局变量实际上不是全球性的,而是局部它们被定义的模块,因此,你不能引用全局在 classes.py 文件中的 main.py 文件配置的。

Your problem is that "global" variables in python are not actually global, but rather local to the module in which they were defined in. Thus, you cannot reference globals decalred in the main.py file in the classes.py file.

有三种可能的解决方案:

There are three possible solutions:


  1. (不推荐)移动屏幕背景的声明到 classes.py 文件,这将使他们看似格格不入。

  1. (not recomended) Move the declarations of screen and background to the classes.py file, which would put them seemingly out of place.

(也不推荐)添加从主屏进口,背景 classes.py 文件将创建圆形进口的问题,迫使 main.py 模块做了从类导入* 确定屏幕背景

(also not recommended) Add a from main import screen, background to the classes.py file, which creates circular import problems, forcing the main.py module to do its from classes import * after defining screen and background

mouse_pos 变量是完全不必要的,误用:

The mouse_pos variable is totally unnecessary and misused:


  1. 您可以只使用 pygame.mouse.get_pos()直接在 classes.py 文件,但

  2. 使用 pygame.mouse.get_pos()本身就是extranious因为 pygame.MOUSEBUTTONDOWN 事件有一个 POS 属性指向当事件被解雇了鼠标的位置。

  1. You could just use pygame.mouse.get_pos() directly in the classes.py file, but
  2. Using pygame.mouse.get_pos() itself is extranious because pygame.MOUSEBUTTONDOWN events have a pos attribute pointing to the position of the mouse when the event was fired.

这篇关于导入类时没有定义error全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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