Yubin Lee
Yubin Lee

Reputation: 882

Pygame - rect appears and immediately disappears

I am rewriting my question Pygame - rect disappears before I reach it because I think I have not described it clearly.

Full code:

#! /usr/bin/python

import pygame, time, sys, random, os
from pygame.locals import *
from time import gmtime, strftime

pygame.init()

w = 640
h = 400

screen = pygame.display.set_mode((w, h),RESIZABLE)
clock = pygame.time.Clock()
x = y = 100

def starting():
    basicfont = pygame.font.SysFont(None, 48)
    text = basicfont.render('Starting...', True, (255, 255, 255), (0, 0, 255))
    textrect = text.get_rect()
    textrect.centerx = screen.get_rect().centerx
    textrect.centery = screen.get_rect().centery
    screen.fill((0, 0, 255))
    screen.blit(text, textrect)
    pygame.display.update()

def taskbar():
    basicfont = pygame.font.SysFont(None, 24)
    pygame.draw.rect(screen, ((0, 255, 0)), (0, h-40, w, 40), 0)
    text = basicfont.render(strftime("%Y-%m-%d", gmtime()), True, (0, 0, 0))
    text2 = basicfont.render(strftime("%H:%M:%S", gmtime()), True, (0, 0, 0))
    screen.blit(text, (w - 100, h - 37))
    screen.blit(text2, (w - 100, h - 17))
    logoimage = pygame.image.load("C:\Users\chef\Desktop\logo.png").convert()
    logoimage = pygame.transform.scale(logoimage, (40, 40))
    screen.blit(logoimage, (0, h-40),)

def start():
    button1, button2, button3 = pygame.mouse.get_pressed()
    mousex, mousey = pygame.mouse.get_pos()
    if 0 < mousex < 40 and h-40 < mousey < h:
        if button1:
            pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0)
    pygame.display.update()

starting()
#time.sleep(2)

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()  
        elif event.type==VIDEORESIZE:
            w = event.dict['size'][0]
            h = event.dict['size'][1]
            screen=pygame.display.set_mode(event.dict['size'],RESIZABLE)
    screen.fill((255, 255, 255))
    taskbar()
    start()
    pygame.display.update()
    clock.tick(60)

Just like my original question, when I press logoimage.png and the red rectangle pops up, it shows for a second then immediately disappears. How can I make it so that the rect will stay until I press outside of it?

Upvotes: 0

Views: 2002

Answers (2)

Anthony
Anthony

Reputation: 680

You should move the screen.fill((255, 255, 255)) out of the while loop. This is causing problems, because each time the program loops, it colors the display white, writing over whatever you had there before. One way to work around this is to move the screen.fill((255, 255, 255)) out of the loop and place it before the pygame.draw.rect function. This will color the screen before you place the rect, and will not continue to fill it. For instance:

if button1:
            screen.fill((255, 255, 255))
            pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0)

You could also move it elsewhere or come up with a way to draw rect every loop after you fill the window.

Hope this helped.

Upvotes: 2

Andy M
Andy M

Reputation: 606

You need to flag a boolean that it's been clicked. Something like:

btnClicked = false

def start():
    button1, button2, button3 = pygame.mouse.get_pressed()
    mousex, mousey = pygame.mouse.get_pos()
    if button1:
        if 0 < mousex < 40 and h-40 < mousey < h:
            btnClicked = true
        else:
            btnClicked = false

    if btnClicked:
        pygame.draw.rect(screen, ((255, 0, 0)), (0, int(h/2), int(w/6), int(h/2)-40), 0)
    pygame.display.update()

Upvotes: 0

Related Questions