【问题标题】:Importing variable not giving the desired value导入变量未给出所需值
【发布时间】:2014-09-03 10:33:42
【问题描述】:

在这个游戏中,分数在开始时设置为 0,如果您正确回答问题,则添加到该分数中证书无论如何都显示为 0。

这是第一个文件的代码(游戏部分)

import pygame, time
pygame.init()
WHITE = ( 255, 255, 255)
BLACK = ( 0, 0, 0)
screen = pygame.display.set_mode((600, 600))
ButtonA = pygame.image.load("A.png")
ButtonB = pygame.image.load("B.png")
ButtonC = pygame.image.load("C.png")
a = screen.blit(ButtonA,(50,400))
b = screen.blit(ButtonB,(250,400))
c = screen.blit(ButtonC,(450,400))
pygame.display.flip()

score = 0
if __name__ == '__main__':
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            if a.collidepoint(pos):
                screen.fill(BLACK)
                Correct = font.render("You are correct!", True, WHITE)
                Correct_rect = Correct.get_rect()
                Correct_x = screen.get_width() / 2 - Correct_rect.width / 2
                Correct_y = 150 - Correct_rect.height / 2
                screen.blit(Correct, [Correct_x, Correct_y])
                pygame.display.flip()
                score = score + 1
                print score
                time.sleep(2)

这是将值放在证书上的部分的代码,该证书当前只抛出数字 0

import pygame, sys, eztext
from pygame.locals import *
from eztext import Input
#Importing the variable score
from Question1Easy import score
Name = self.value
#Setting the variable score (an int) to a string
Score = str(score)

如何让第二个文件获取更新后的分数而不是静态的 0?

【问题讨论】:

  • 您粘贴的代码过多,您最好提供代码sn-p,足以描述您的问题,这将有助于您获得帮助。
  • 抱歉,解释我的问题的时间大大缩短了
  • 提问前请先搜索。由于您已经有了罪魁祸首的嫌疑人,您只需在问题中搜索python if __main__ == __name__,第一个结果就可以解决您的问题。 -1 表示没有研究工作。
  • @Bakuriu 我不同意你的说法,即他没有进行研究。根据他的问题,像我一样,他可能对 Python 比较陌生,并且刚刚了解了 if name == 'main'。通过阅读它的功能要点,他可能刚刚了解到它只会导致代码仅在来自主文件的情况下执行。此外,他的问题不一定是问它做了什么,他问的是为什么他的问题不起作用。如果可能的话,你能把它取消标记为重复吗?我相信我可以帮助解决他的问题,因为我过去曾遇到过这种情况。
  • @Bakuriu 实际上,我不认为它是完全重复的。由于这里还有一个问题:OP想要从另一个模块获取动态分值,这不能简单地通过import score来实现。

标签: python variables import


【解决方案1】:

所以我认为你应该做的就是创建一个类。一个类可能听起来有点工作,但习惯它们绝对值得,并且以这种方式使用一个类非常容易。

#players.py 
class Player():
    def __init__(self):
        self.score = 0

#games.py
def checkers(player):
    #blahblahblah dostuff
    player.score += 1
    print(player.score) #if everything goes right, this should print 1

#main.py
from players import player
from games import checkers
player = Player()
checkers(player)
player.score += 1
print(player.score) #if everything goes right, this should print 2

使用这种方法的缺点是,对于您想要访问的每个函数,我认为您必须将 player 作为参数传递给几乎每个函数,这可能会有点乏味。但是,玩家可以保存多个变量或您想要跟踪的其他统计数据。您只需要一个播放器变量(现在是对象),只需在初始化程序中放置更多变量。

当我遇到与您类似的问题时,我发现这是最简单的解决方案。

【讨论】:

    【解决方案2】:

    是的,它肯定会给你0。

    当被其他文件导入时,if __name__ == '__main__'下的代码不会被执行。

    您可以将if __name__ == '__main__'下的逻辑放入第一个py文件中的函数中,例如函数start_game。然后在第二个py文件中调用start_game函数让游戏运行。除此之外,您需要添加一个函数来返回第一个文件中的分数,如下所示:

    第一个文件:

    score = 0
    def start_game():
        global score
        score += 1
    
    def get_score():
        return score
    

    第二个文件:

    from firstfile import get_score, start_game
    print get_score()
    start_game()
    print get_score()
    

    当你运行第二个文件时,你会得到:

    0
    1
    

    使用from firstfile import score获取静态值,如果要获取动态值,需要添加一个函数(上面代码中的get_score)。

    【讨论】:

    • 我不希望第二个文件导入除分数之外的任何其他内容,这就是为什么我的所有代码都在下面,我如何去更新 if name 上方的变量 == '主要'
    • 我想我知道您的问题的解决方案,但是,我无法回复,因为有人将此标记为重复。我的涉及制作课程(这很容易!)。我过去也有你的问题。
    • @KyleMe 你认为你可以在 pastebin 上发帖吗?
    猜你喜欢
    • 2016-07-08
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    相关资源
    最近更新 更多