简体   繁体   中英

pygame: drawing a selection rectangle with the mouse

I have written a simple breakout game in pygame and am writing a level editor. Everything was working until I tried to add a selecting rectangle that has the transparent look (like the one on my desktop background). I can get a rectangle (sorta) but everything else vanishes, and it isn't semi-transparent.

code:

pygame.init()
screen = pygame.display.set_mode(size)
mousescreen = pygame.Surface((screen.get_size())).convert_alpha()

...

in the designing loop:

global xpos, ypos, theBricks, clock, mousedrag, mouseRect
global designing, titles
global theLevels, level, cur_max_level, max_level

mousedrag = False
mouseRect = None
mouseDown = False

while designing:

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

        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouseDown = True
            mpos = pygame.mouse.get_pos()
            x_position = mpos[0]
            y_position = mpos[1]

            xpos = ((x_position-left)/BRW) * BRW + left
            ypos = ((y_position-top)/BRH) * BRH + top                   


        elif event.type == MOUSEMOTION:
            if mouseDown:
                newx_pos = mpos[0]
                newy_pos = mpos[1]
                mousedrag = True

                if mousedrag:
                    mouseRect = Rect(newx_pos, newy_pos, xpos, ypos)

        elif event.type == MOUSEBUTTONUP:
            if mousedrag:
                mousedrag = False
            else:
                if is_a_brick(xpos, ypos):
                    del_brick(xpos, ypos)
                else:
                    make_brick(xpos, ypos)

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_q:
                designing = False
                titles = True

...

in the update screen function:

for bricks in theBricks:
    pygame.draw.rect(screen, GREEN, bricks.rect)

if mousedrag:
    pygame.draw.rect(mousescreen, RED, mouseRect, 50)
    screen.blit(mousescreen, (0,0))

pygame.draw.rect(screen, WHITE, (xpos, ypos, BRW, BRH))

pygame.display.update()

The rectangle is not transparent and everything else vanishes off the screen? Where am I going wrong?

I'm not sure if .convert_alpha() is creating a transparent screen like you think. Try setting the alpha level on mousescreen explicitly:

mousescreen = pygame.Surface((screen.get_size()))
mousescreen.set_alpha(100)  # this value doesn't have to be 100 

Another way to achieve the same effect is to draw your rect as 4 lines directly onto the screen which means you don't have to have mousescreen at all. In your update screen function:

if mousedrag:
    mouseRectCorners = [mouseRect.topleft, 
                        mouseRect.topright, 
                        mouseRect.bottomright, 
                        mouseRect.bottomleft]
    pygame.draw.lines(screen, RED, True, mouseRectCorners, 50)

Just make sure you draws these lines after any other objects or they might get hidden. I'm not sure if this option is really considered best practice but it's always good to have options.

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