简体   繁体   English

PYTHON从模块中的类导入方法

[英]PYTHON importing methods from class in module

I am having a hard time using an attribute from a class in a module I made.我很难在我制作的模块中使用类中的属性。 I am using PyCharm and am trying to create a small game with pygame.我正在使用 PyCharm 并尝试使用 pygame 创建一个小游戏。

Here is the content of the module Player_class.py :这是模块 Player_class.py 的内容:

import pygame
# Créer une classe qui représente le joueur
class Player(pygame.sprite.Sprite):  

 def __init__(self):
 super().__init__()  # Permet d'utiliser le paramètre sprite de la super classe pygame.sprite.Sprite
 self.health = 100  # Points de vie variables durant le jeu
 self.max_health = 100
 self.attack = 10
 self.velocity = 5  # Vitesse du joueur en pixels
 self.image = pygame.image.load(r"C:\Users\Utilisateur\PycharmProjects\GravenJeu\assets\Jump (32x32).png")
 self.rect = self.image.get_rect()  # Pour avoir la position du joueur
 self.rect.x = 500
 self.rect.y = 600

 def move_right(self):
 self.rect.x += self.velocity

def move_left(self):
 self.rect.x -= self.velocity

Here is the code in the module Game_class.py: from Player_class import Player这是 Game_class.py 模块中的代码: from Player_class import Player

class Game:
 def __init__(self):
 self.player = Player()
 self.pressed = {}

And here is the code in main.py:这是 main.py 中的代码:

import pygame
from Game_class import Game
pygame.init()

pygame.display.set_caption("Beach Brawl")
screen = pygame.display.set_mode((1080,720))

# Import background
background = pygame.image.load("assets/beachBG.jpg")

# Charger le jeu
game = Game

# Charger le joueur
player = game.player()

# Boucle tant que cette condition est vraie
running = True
while running:

# Appliquer le background
 screen.blit(background,(-200,-50))

# Appliquer l'image du joueur
 screen.blit(player.image, player.rect)

# Mettre à jour l'écran
 pygame.display.flip()

#Si le joueur ferme cette fenêtre
 for event in pygame.event.get():
 # Pour vérifier que event = fermeture de fenetre
 if event.type == pygame.QUIT:
            running = False
 pygame.quit()

# To detect if the player is pressing & releasing keys
 elif event.type == pygame.KEYDOWN:
            game.pressed[event.key] = True
        elif event.type == pygame.KEYUP:
            game.pressed[event.key] = False

The error I get from PyCharm is "Unresolved attribute reference 'pressed' for class 'Game'" I know the trouble has to do with importing modules... We can either import a full module and use it like this :我从 PyCharm 得到的错误是“Unresolved attribute reference 'pressed' for class 'Game'”我知道问题与导入模块有关......我们可以导入一个完整的模块并像这样使用它:

import Full_module

Full_module.function_in_module()

Or import the specific function from a module或从模块导入特定功能

from Full_module import function_in_module

function_in_module()

But since I am using classes and methods (I believe methods to be functions inside of classes), it might not be the same when it comes to importing and using them from a module.但是由于我使用的是类和方法(我相信方法是类中的函数),因此在从模块导入和使用它们时可能会有所不同。

My problem is with the self.pressed attribute in the Game class "Unresolved attribute reference 'pressed' for class 'Game'"我的问题是游戏类中的 self.pressed 属性“Unresolved attribute reference 'pressed' for class 'Game'”

Please help out a noob:)请帮助一个菜鸟:)

Don't use ...不要用...

game = Game

... but use ... ...但使用...

game = Game()

... instead. ... 反而。

A game = Game creates a variable game which receives the class (!!!) Game . A game = Game创建一个变量game ,它接收类 (!!!) Game

A game = Game() creates a variable game which will receive an instance of a class (!!!) of Game . game = Game()创建一个变量game ,它将接收一个Game类 (!!!) 的实例。

This should resolve your issue.这应该可以解决您的问题。

Remark: What you import from modules (typically) are functions and classes.备注:从模块导入的(通常)是函数和类。 While you invoke functions directly you (typically) dont's access members on classes directly.当您直接调用函数时,您(通常)不会直接访问类上的成员。 You can only do that if these members are static.如果这些成员是静态的,您只能这样做。 However variables such as pressed aren't static, therefore you need to create an instance of your class Game first.但是, pressed类的变量不是静态的,因此您需要先创建类Game实例 (This is the very very very very typical way of how to structure programs. Static members are used only in very specific situations that don't apply here in your case.) (这是构建程序的非常非常非常典型的方式。静态成员仅在非常特定的情况下使用,在您的情况下不适用。)

(So: This is not a matter of importing.) (所以:这不是导入的问题。)

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

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