Python Forum
I dont know where my local variable has gone
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I dont know where my local variable has gone
#1
I am aiming to create a basic maths game, and I am creating a scoring system, after every question right the score should increment, it does so for a second before turning back to 0, thanks for any help. P.S. the code below uses the pygame module.
#relevent moduals are being imported into the python IDLE
import pygame
import system as sys
import math
import time
import random
from random import randrange
  
#pygame initialisation
pygame.init()
  
# colours being defined for future use
GREEN = (0, 255, 0)
DARK_GREEN = (0, 100, 0)
RED = (255, 0, 0)
GOLD = (255, 215, 0)
PINK = (255, 20, 147)
PURPLE = (128, 0, 128)
LIME = (0, 255, 0)
LIGHT_BLUE = (135, 206, 250)
YELLOW = (255, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 168)
ORANGE = (255, 165, 0)

# A Array of global variables to be incremented through to create a optical effect
rainbow =[RED, DARK_GREEN, GOLD, PINK, PURPLE, LIME, LIGHT_BLUE, YELLOW, WHITE, BLUE, ORANGE]

standard_font = pygame.font.SysFont('arial', 70)
A = standard_font.render("A", 1, BLACK)
B = standard_font.render("B", 1, BLACK)
C = standard_font.render("C", 1, BLACK)

To_Win = 5


unit = 80

Box_width = (2*unit)
pygame_window_width = ((6*unit)+Box_width)
pygame_window_height = (7*unit)
pygame_window_size = (pygame_window_width, pygame_window_height)
pygame_screen = pygame.display.set_mode(pygame_window_size)

Easy_Bank = ['Q1','Q2','Q3','Q4','Q5']
Medium_Bank = ['Q6()','Q7()','Q8()','Q9()','Q10()']
Hard_Bank = ['Q11()','Q12()','Q13()','Q14()','Q15()']

def Q_display(Q_label, score, Answer, A1, A2, A3, X, E, D):
    cover(score)
    Question_label = standard_font.render(Q_label, 1, BLACK)
    Correct_label = standard_font.render("Correct", 1, DARK_GREEN)
    Incorrect_label = standard_font.render("Incorrect", 1, RED)
    A_label_1 = standard_font.render(str(A1), 1, BLACK)
    A_label_2 = standard_font.render(str(A2), 1, BLACK)
    A_label_3 = standard_font.render(str(A3), 1, BLACK)
    pygame_screen.blit(Question_label, (E,D))
    pygame_screen.blit(A, (X,250))
    pygame_screen.blit(B, ((X+140),250))
    pygame_screen.blit(C, ((X+265),250))
    pygame_screen.blit(A_label_1, (X, 320))
    pygame_screen.blit(A_label_2, (X+140, 320))
    pygame_screen.blit(A_label_3, (X+265, 320))
    update()
    select = False
    while select == False:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    Response = A1
                    select = True
                if event.key == pygame.K_b:
                    Response = A2
                    select = True
                if event.key == pygame.K_c:
                    Response = A3
                    select = True
    if Response == Answer:
        cover(score)
        pygame_screen.blit(Correct_label, (100, 200))
        score = score + 1
        score_track(score)
    else:
        score = 0
        cover(score)
        pygame_screen.blit(Incorrect_label, (80,200))
    if score == To_Win:
        pass
    update()
    pygame.time.wait(1000)
    return score
    

    return score
def Q1(score):
    cover(score)
    Q_Label = ("6 + 8 =")
    Answer = 14
    A1 = 15
    A2 = Answer
    A3 = 13
    X = 70
    E = 150
    D = 100
    Q_display(Q_Label, score, Answer, A1, A2, A3, X, E, D)
    return score

def Q2(score):
    cover(score)
    Q_Label = ("4 x 7 =")
    Answer = 28
    A1 = 32
    A2 = 25
    A3 = Answer
    X = 70
    E = 150
    D = 100
    Q_display(Q_Label, score, Answer, A1, A2, A3, X, E, D)
    return score
    
def Q3(score):
    cover(score)
    Q_Label = ("100 ÷ 25 =")
    Answer = 4
    A1 = 5
    A2 = Answer
    A3 = 2.5
    X = 80
    E = 80
    D = 100
    Q_display(Q_Label, score, Answer, A1, A2, A3, X, E, D)
    return score

def Q4(score):
    cover(score)
    Q_Label = ("(10x33) - 60 =")
    Answer = 270
    A1 = Answer
    A2 = 250
    A3 = 280
    X = 70
    E = 35
    D = 100
    Q_display(Q_Label, score, Answer, A1, A2, A3, X, E, D)
    return score

def Q5(score):
    cover(score)
    Q_Label = ("39 ÷ 3 =")
    Answer = 13
    A1 = Answer
    A2 = 11
    A3 = 16
    X = 70 
    E = 150
    D = 100
    Q_display(Q_Label, score, Answer, A1, A2, A3, X, E, D)
    return score

def Box_Draw(score):
    pygame.draw.rect(pygame_screen, PURPLE, [490, 0, 150, pygame_window_height])
    update()
    return score

def score_track(score):
    score_font = pygame.font.SysFont('arial', 30)
    score_label = score_font.render(("Score: "+str(score)), 1, LIGHT_BLUE)
    pygame.draw.rect(pygame_screen, PURPLE, [510, 163, 120, 30])
    pygame_screen.blit(score_label, ((510),160))
    update()
    return score

def cover(score):
    pygame.draw.rect(pygame_screen, LIGHT_BLUE, (0, 0, pygame_window_width, pygame_window_height))
    Box_Draw(score)
    score_track(score)
    return score
  
def update():
    # Updates screen
    pygame.display.update()

def start():
    score = 0
    game_over = False
    main_code(score, game_over)

def Questions(level, score):
    if level == 'Easy':
        Questions = Easy_Bank
    elif level == 'Medium':
        Questions = Medium_Bank
    elif level == 'Hard':
        Questions = Hard_Bank
    for i in range (1,5):
        Q = random.choice(Questions)
        Question_Load(Q, score)
        Questions.remove(Q)
    return True

def Question_Load(Q, score):
    if Q == 'Q1':
        Q1(score)
    if Q == 'Q2':
        Q2(score)
    if Q == 'Q3':
        Q3(score)
    if Q == 'Q4':
        Q4(score)
    if Q == 'Q5':
        Q5(score)
    return score
    
def main_code(score, game_over):
    level = 'Easy'
    while game_over == False:
        game_over = Questions(level, score)

# Calls the function which start the program
start()
Reply


Messages In This Thread
I dont know where my local variable has gone - by Help_me_Please - Mar-30-2020, 11:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  local variable 'spider_1_health' referenced before assignment Tbot1000 1 1,786 Sep-01-2020, 09:07 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020