Jayson Galante
Jayson Galante

Reputation: 3

after i click on image for blit when MOUSEBUTTONDOWN the new blit then disappear in pygame

I'm new with python especially pygame and wonder how to keep my new image blit after MOUSEBUTTONDOWN Here's my code:

color = pygame.image.load('billes.png')
red = color.subsurface(50, 70, 79, 71)
r = display.blit(red,(918,920))

def play():  
    if event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
        if r.collidepoint(pos):
            display.blit(red, (10,10))
while True:
    ...
    for event in pygame.event.get():
        ...
        if event.type == MOUSEBUTTONDOWN and event.button == 1:
            if r.collidepoint(pos):
                display.blit(red, (800, 700))

it display my new image when i ckick the left button of the mouse but disappear after one second even the button still down. I also tried to use pygame.mouse.get_pressed()[0] but same result. How can I maintain the new blit after MOUSEBUTTONDOWN? I was thinking to put a condition with MOUSEBUTTONUP but I don't how to do it.

Upvotes: 0

Views: 886

Answers (1)

user4414248
user4414248

Reputation:

import pygame
pygame.init()
red = (200,0,0)
white = (255,255,255)
gameDisplay=pygame.display.set_mode((800,600))

FPS=30
clock=pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()
    cur = pygame.mouse.get_pos() #taking click events every time
    click=pygame.mouse.get_pressed() ##again, every time
    if click[0] == 1: #if left-clicked
        pygame.draw.rect(gameDisplay,red, (100,200,100,200)) #draw a red rectangle
        pygame.display.update() #and update the display


    clock.tick(FPS)

The problem is, you have used pygame.display.update() at incorrect place. I have corrected it and now it's working.

Actually this draws rectangle whenever you do left-click, but you can see only one rectangle because they are on the same co-ordinates.

If you want to be sure about this, try:

import pygame,random
pygame.init()
red = (200,0,0)
white = (255,255,255)
black = (0,0,0)
gameDisplay=pygame.display.set_mode((800,600))

FPS=30
clock=pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()
    cur = pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    if click[0] == 1:
        x=random.randint(1,700) #random coords of x
        y=random.randint(50,500) #random coords of y
        wx=random.randint(10,100) #random size of x
        hy=random.randint(60,200) #random size of y
        pygame.draw.rect(gameDisplay,red, (x,y,wx,hy)) #drawing with random variables
        pygame.display.update()


    clock.tick(FPS)

This shows our method is working. Also take care that if you keep on pressing the left-click, it'll continue to draw rectangles. It was drawing rectangles earlier too but we were not able to see them because they were on the same co-ordinates, that's why it seems like there is only one rectangle at that position.

If you want to delete that rectangle, for example with right-click:

if click[2] == 1:
    pygame.draw.rect(gameDisplay,black, (100,200,100,200))
    pygame.display.update()

Add this one bottom of first if click statement. Basically it will draw another rectangle on first rectangle,care same coords, but same color with background. So your first rectangle seems erased :-).

Upvotes: 1

Related Questions