2

I've been fixing problems with my code over the last few days, as I've been trying to add a start screen into my pygame code, I've sorted many errors but i couldn't figure out what was wrong with this one.

I'm using a variable name of inactive in the code to call it for a colour that's used for the 'buttons'on the screen.

this is the full error message

Traceback (most recent call last):
  File "D:\Computing Homework\Programming Projects\pong development.py", line 8, in <module>
    gameDisplay = pygame.display.set_mode([display_width,display_height,inactive,active])
NameError: name 'inactive' is not defined

I have tried to define inactive in different places throughout the code but to no advancement, I defined it at the top of the code, and i stopped getting the error message, but it also meant that when i ran the code nothing appeared not even the window for the start up screen showed up and 0 errors where thrown up.

Below is the code using the inactive:

def button(msg,x,y,width,height,active,inactive,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if ((x+width > mouse[0]) > x and (y+height > mouse [1] > y)):
        pygame.draw.rect(gameDisplay, active,[x,y,width,height])

        if click[0] == 1 and action != None:
            if action == "play 1":
                game_loop_one()
            elif action == "play 2":
                game_loop_two()  


    if x+width > mouse[0] > x and y+height > mouse[1] > y:
         pygame.draw.rect(gameDisplay, active,(x,y,width,height))
    else:
        pygame.draw.rect(gameDisplay, inactive,(x,y,width,height))

        smallText = pygame.font.Font("freesansbold.ttf",20) #defining the small text to be used on the buttons.
        textSurface, textRect = text_objects(msg, smallText)
        textRect.center = ( (x+(width/2)), (y+hight/2))
        gameDisplay.blit(textSurface, textRect)

And here are the functions that are at the top of the program incase they are needed for fixing the problem :

display_width = 640
display_height = 480
gameDisplay = pygame.display.set_mode([display_width,display_height,inactive,active])
pygame.display.set_caption('Ethong')
fps = 60
pygame.font.init()
font = pygame.font.Font('freesansbold.ttf',115)
clock = pygame.time.Clock()
pygame.display.set_caption('ethong')
green = (0,200,0)#base colours.
white = (255,255,255)#base colours.
black = (0,0,0)#base colours.
red = (255,0,0)#base colours.
blue = (0,0,255)#base colours.
bright_green =(0,255,0)#defining colours this will be used to make the button 'glows' when hoovered over.

I expected the start screen to run and then display a start screen with the name of the game and two buttons, the buttons would be green, and then when hovered over, they would become bright green.

Instead I am stuck on an error to do with the inactive not being defined.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • 4
    When you call `pygame.display.set_mode([display_width,display_height,inactive,active])`, at the top of the file, what are the intended values of `inactive` and `active`? – Rob Streeting Sep 24 '19 at 09:13
  • The inactive and active at the top of the file is intended to then be used later in the file when I've called the start screen and defined the button ``` button("player1",150,450,100,50,bright_green,green,"play 1") button("2player",550,450,100,50,bright_green,green,"play 2") ``` where the "bright_green" and "green" are in the code is supposed to be the inactive and active being used. – Ethan Ibrahim Sep 24 '19 at 09:17
  • 2
    Ok, but what do you expect the actual values inside those variables to be at the time `set_mode` is called? When `set_mode` is called, Python is going to first evaluate every variable in the argument, effectively turning them all into concrete values. The width and height variables have values assigned, but inactive and active do not, which is why Python returns this error. – Rob Streeting Sep 24 '19 at 09:34
  • @RobStreeting, okay thank you, I'll look into that now. – Ethan Ibrahim Sep 24 '19 at 09:38
  • Look at the [docs](https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode) to see what parameters are accepted by `set_mode` and their meaning. – Valentino Sep 24 '19 at 09:40
  • ahhhh okay, I'll definitely look into the docs to see if it can help me fix the problem. – Ethan Ibrahim Sep 24 '19 at 09:47

0 Answers0