【发布时间】: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来实现。