简体   繁体   中英

How can i delete a rectangle in pygame?

I tried to set the color of the rectangle the same as the background color, but i wonder if there's a more simple method to do it. This is what i've done:

screen = display.set_mode(500, 500)

BLACK = (255, 255, 255)
color2 = (127, 127, 127) #Grey

screen.fill(BLACK)
start_button = draw.rect(screen, color2, (190, 180, 120, 60))
display.update()
for i in event.get():
    if i.type() == MOUSEBUTTONDOWN and mouse.get_pos[0] >= 190 and mouse.get_pos[0] <= 310 and mouse.get_pos[1] >= 180 and mouse.get_pos[1] <= 240:
        color2 = (255, 255, 255)
        display.update()

Anyway this is my first try with pygame, if you have any suggestions just tell me.

alternatively you can just fill the screen again and update the display if you want that.

screen = display.set_mode(500, 500)

BLACK = (255, 255, 255)
color2 = (127, 127, 127) #Grey

screen.fill(BLACK)
start_button = draw.rect(screen, color2, (190, 180, 120, 60))
display.update()
for i in event.get():
    if i.type() == MOUSEBUTTONDOWN and mouse.get_pos[0] >= 190 and mouse.get_pos[0] <= 310 and mouse.get_pos[1] >= 180 and mouse.get_pos[1] <= 240:
        screen.fill(BLACK)
        display.update()

I also recomend you make buttons and checking if they are pressed using this http://programarcadegames.com/index.php?lang=en&chapter=array_backed_grids

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM