1

I'm trying to make a program that toggles the background color from black to white when I press the space bar.

Does anyone know what is wrong with my code:

import pygame, sys

clock = pygame.time.Clock()
wn_size = (800, 600)
pygame.init()

screen = pygame.display.set_mode(wn_size)

bg_change = False

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if bg_change:
                    bg_change = True
                else:
                    bg_change = False

    if bg_change == True:
        screen.fill(255,255,255)
    else:
        screen.fill((0, 0, 0))

    pygame.display.update()
    clock.tick(60)
mightygulp
  • 13
  • 3
  • To toggle a switch, you dont need to do ` if bg_change: bg_change = True else: bg_change = False`. Instead do `bg_change = Not bg_change – Joe Ferndz Jan 02 '21 at 04:59

3 Answers3

0

Seems like you added three different parameters instead of a tuple in screen.fill(255,255,255).

You can fix this by just adding another layer of brackets, like this: screen.fill((255,255,255)).

The if statement should look like this:

    if bg_change:
        screen.fill((255,255,255))
    else:
        screen.fill((0, 0, 0))

EDIT: As @JoeFerndz mentioned, you should also change if bg_change == True to just if bg_change since adding == True is redundant.

Axiumin_
  • 2,107
  • 2
  • 15
  • 24
0

You can use basic toggle switch to change the values.

Here are two options:

1: boolean switch

bg_change = False

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bg_change = not bg_change

    if bg_change:
        screen.fill((255,255,255))
    else:
        screen.fill((0, 0, 0))

2: set fill value as toggle

t = 255 ^ 0
x = 0
x ^= t #x is set to 255 first. If you want to start with 0, comment this line

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                x ^= t

    screen.fill((x,x,x))

Here's how this works:

>>> t = 255 ^ 0
>>> x ^= t
>>> x
255
>>> x ^= t
>>> x
0
>>> x ^= t
>>> x
255

Every time you have to switch, the value is toggled automatically. So you don't have to worry about the toggle switch. You are using the value of x to toggle.

For more options on toggle, see the impressive solution written up by @Raymond Hettinger in the post How to toggle a value in Python

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • `Not` is not Python. Python is case senitive. – Rabbid76 Jan 02 '21 at 08:12
  • sorry, it should be `not` not `Not` :) Edited the answer. Thanks for catching that. Never thought I will write 3 not in one sentence :) – Joe Ferndz Jan 02 '21 at 08:16
  • Using the value of `x` itself to make the toggle overloads the utility of the variable. I'd suggest writing a `Toggle` class if you really want such a tool. – Acorn Jan 02 '21 at 09:19
0

The mistake is in the lines:

if bg_change:
   bg_change = True
else:
   bg_change = False

This code doesn't actually do anything. If bg_change is True it is set True and if bg_change is False it is set False. What you actually want to do is:

if event.key == pygame.K_SPACE:
    if bg_change:
        bg_change = False
    else:
        bg_change = True

You can simplify the code with the not operator. not inverts (toggles) a Boolean variable:

if event.key == pygame.K_SPACE:
    bg_change = not bg_change

fill has a single argument, which can be a tuple with the RGB color values:

screen.fill(255,255,255)

screen.fill((255, 255, 255))

Complete code:

import pygame, sys

clock = pygame.time.Clock()
wn_size = (800, 600)
pygame.init()

screen = pygame.display.set_mode(wn_size)

bg_change = False

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bg_change = not bg_change

    if bg_change == True:
        screen.fill((255,255,255))
    else:
        screen.fill((0, 0, 0))

    pygame.display.update()
    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174