HALLOPEOPLE
HALLOPEOPLE

Reputation: 165

Erasing screen with button

#Imported Pygame
import pygame

#The Colors
BLACK = ( 0, 0, 0)
GREEN = ( 0, 255, 0)
WHITE = ( 255, 255, 255)
RED = ( 255, 0, 0)
ORANGE = ( 255, 115, 0)
YELLOW = ( 242, 255, 0)
BROWN = ( 115, 87, 39)
PURPLE = ( 298, 0, 247)
GRAY = ( 168, 168, 168)
PINK = ( 255, 0, 234)
pygame.init()
#The Screen
screen = pygame.display.set_mode([1000,500])
#Name of the window
pygame.display.set_caption("My first game")

clock = pygame.time.Clock()

#The sounds

# Positions of graphics
background_position = [0,0]
singleplayer_position = [350, 200]
tutorial_position = [350,300]
sorry_position = [0,0]
developer_position = [0,450]
rules_position = [0,0]
#The graphics
background_image = pygame.image.load("Castle.png").convert()
singleplayer_image = pygame.image.load("SinglePlayer.png").convert()
singleplayer_image.set_colorkey(WHITE)
tutorial_button = pygame.image.load("Tutorial_button.png").convert()
sorry_message = pygame.image.load("Sorry.png").convert()
sorry_message.set_colorkey(WHITE)
developer_message = pygame.image.load("Developer.png").convert()
developer_message.set_colorkey(WHITE)
Rules_image = pygame.image.load("Rules.png").convert()
#Main Loop __________________________

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    # Copy of background or main menu
    screen.blit(background_image, background_position)

    #Copy of other images
    mouse_pos = pygame.mouse.get_pos()
    my_rect = pygame.Rect(350,200,393,75)
    tutorial_rect = pygame.Rect(350,300,393,75)
    screen.blit(singleplayer_image, singleplayer_position)
    screen.blit(tutorial_button, tutorial_position)
    screen.blit(developer_message, developer_position)
    if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
       screen.blit(sorry_message, sorry_position)
       correct = False
    if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
          #Here I make the screen fill white


    if python.mouse.get_pressed()[0]tutorial_rect.collidepoint(mouse.pos):
        correct = True
    if correct == True:
        screen.blit(Rules_image, rules_position)





    pygame.display.flip()
    clock.tick(60)
#To quit game
pygame.quit()

This is basically my code... When I hit the single player button I have it making the area white but it doesn't stay there. Like when I hit it once and hold the singleplayer button it stays white but when i unclick the screens back to what it was. Is there anyway I can just erase everything I did before and start a new screen when I hit the Singleplayer button?'

Ok back to the answer you gave me.. I structured my code like you said.

 if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
          color_white = True
          if color_white = True
             screen.fill(WHITE)

This isn't working because It still doesn't make the screen stay white. I tried this.

if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
     color_white = True
if color_white = True
     screen.fill(WHITE)

This also doesn't seem to work because it keeps saying color_white is undefined.

Upvotes: 0

Views: 104

Answers (1)

David Jay Brady
David Jay Brady

Reputation: 1154

Your confusion results from the while loop and how it behaves, so I'll explain that to answer your question.

Quick note: if you are not using a pygame clock object with tick at end of code, comment and I'll explain that at end, its important you do this.(http://www.pygame.org/docs/ref/time.html)

Okay, the problem: your picture is not remaining white after you click it. It stays white if you hold the mouse down, but it goes away once you lift up. I assume you want it to remain white even once you lift the mouse click.

Currently, your code colors the picture white inside of an if statement.

if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):

Review the docs on what .get_pressed() does. It returns True if the mouse button is pressed. So, when you click it, it says True, if you are holding it down, it says True. If you are not clicking or holding, its False. So it only colors it white when the mouse is clicked or held down, since thats when its told to do so. What makes it turn back to normal are your blits earlier in the loop. Each loop, pygame makes the image normal (via blit) and colors the picture white if your statement evaluates to True. This means whenever your if statement is False, the picture remains normal.

To make it remain painted white, use a boolean.

    if pygame.mouse.get_pressed()[0] and  my_rect.collidepoint(mouse_pos):
        color_white = True

And then instead of putting the code to color the white inside the if statement that now sets the boolean to true, make a new if statement before your loop ends.

if color_white:
    # Code to make the screen white.

This way, it can remain white even while not holding it down. If you want to make it back to normal with another click. You can expand your first if statement.

if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
      if color_white is True:
          color_white = False
      else:
          color_white = True

Which can be coded in a shorter fashion...

color_white = False if color_white == True else True

Edit: I wrote the previous code considering events. This code would work if you were using the MOUSEBUTTONDOWN event to change the color. However, if you want to use get_pressed(), you'll have to use a different mouse button. If you only use left click, how should the program know whether to turn it off or on with so many loops going by?

I'll rewrite the code with get_pressed in mind.

    if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
        color_white = True

    if pygame.mouse.get_pressed()[1] and my_rect.collidepoint(mouse_pos): # Not sure if it should be 1 or 2 in get_pressed, but I'm assuming they represent the right click and middle mouse button. So you can use these to turn the screen back to normal.
        color_white = False

Edit2: Your color_white is undefined, because it doesn't get defined until after the if statements in your code. So before you get a chance to click (and define it), a loop runs and gets to

if color_white:

But color_white doesn't exist to the computer yet. To solve, define color_white before your while loop.

color_white = False # Default to not color the screen white.

Upvotes: 1

Related Questions