1

It this game the score is set you 0 at the beginning and added to if you answer a question correctly, If i check the score by printing while I'm playing it gives the correct value but when the score is shown on the certificate it displays 0 no matter what.

Here's the code for the first file (The game part)

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)

And here is the code for the part where it puts the value on a certificate which currently throws up nothing but the number 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)

How do I get the second file to grab the updated value of score instead of the static 0?

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
user3415453
  • 31
  • 1
  • 7
  • You pasted too much code, you'd better just provide code snippet which is enough to describe your problem, and this will help you get help. – WKPlus Jul 13 '14 at 08:35
  • Apologies, I made it significantly shorter to explain my problem – user3415453 Jul 13 '14 at 08:39
  • Please search before asking a question. Since you already had a suspect on the culprit you could simply search for `python if __main__ == __name__` in the questions and the first result would have solved your problem. -1 for no research effort. – Bakuriu Jul 13 '14 at 09:01
  • @Bakuriu I disagree with your statement saying that he has not done his research. Based upon his question, like me, he's probably newer to Python, and has just learned about the if __name__ == '__main__'. From reading the gist of what the function of it is, he may have just learned that it just causes code to only execute if it's from the main file. Also, his question was not necessarily asking what the it does, he was asking as to why his issue wasn't working. If possible, could you unmark this as a duplicate? I believe I can help out his problem, as I have experienced this in the past. – Kyle Me Jul 13 '14 at 09:08
  • @Bakuriu Actually, I don't think it's completely duplicated. Since there is another problem here: the OP want to get a dynamic score value from another module, which can't be achieved simply by `import score`. – WKPlus Jul 13 '14 at 09:24
  • @WKPlus Where do you see that in the question? If the OP removed the code as not relevant, then it isn't relevant. I'll re-open the question only if the OP edits it into something that is not an exact duplicate. As it currently stands it **is** an exact duplicate. Also, there are already a lot of questions about the topic, so the result will probably be closing as duplicate of a different question. – Bakuriu Jul 13 '14 at 09:27
  • @Bakuriu Agree, it stands as duplicated now. I have seen the first edition of this question, so I knew the problems the OP encountered but maybe not realized. Well, if the OP edits this question again, you may re-open it. – WKPlus Jul 13 '14 at 09:39

2 Answers2

2

Yes, it will certainly give you 0.

When imported by another file, the code under if __name__ == '__main__' will not be executed.

You can put the logic under if __name__ == '__main__' into a function in the first py file, for example function start_game. And then in the second py file call the start_game function to let the game run. Besides this, you need to add a function to return the score in the first file like this:

First file:

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

def get_score():
    return score

Second file:

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

When you run the second file, you will get:

0
1

You get a static value by using from firstfile import score, if you want to get the dynamic value, you need to add a function(get_score in the above code).

WKPlus
  • 6,955
  • 2
  • 35
  • 53
  • I don't want the second file to import anything else but the score which is why the rest of all my code is underneath, how did I go about updating the variable above the if __name__ == '__main__' – user3415453 Jul 13 '14 at 08:50
  • I think I know the solution to your problem, however, I'm not able to reply because someone marked this as a duplicate. Mine involves making a class (it's easy!). I have also your problem in the past. – Kyle Me Jul 13 '14 at 09:01
  • @KyleMe Do you think you could post in on pastebin perhaps? – user3415453 Jul 13 '14 at 10:24
2

So what I think you should do is make a class. A class might sound like a bit of work, but it's definitely worth it to get used to them, and using a class in this way is very easy.

#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

The downside to using this method, is for each function that you want to go to, I think you have to pass player as an argument for almost every function, which can get a bit tedious. However, player can hold multiple variables or other stats that you want to keep track of. You only need one player variable (now object), just in the initializer put more variables.

When I was having a similar issue to you, I found this to be the easiest solution.

Kyle Me
  • 336
  • 2
  • 5
  • 13