Pygame hitboxes blinking

118 views Asked by At

I am trying to make a game using pygame, I am almost done but I want to make the boxes that are being drawn in-wall object to not blink, these red boxes blink throughout the game which I don't want them to and further, In the end, I am calling playercollide function in an if condition, here whenever I make new collider I have to add the function in if condition every time, what I want is that collider object automatically calls this function, without me calling it in if statement for every instance of collider object. Please guide me on how to do so.

def redrawGameWindow():
    win.blit(bg, (-50,-200))
    man.draw(win)
    #man.drawhitbox(win)
    pygame.display.update()


#mainloop

man = player(200, 410, 64,64)
run = True
while run:
    collider1 = wall(500, 400, 200, 200, True)
    collider2 = wall(200,100,50, 50, False)
    collider3 = wall(700,100,50, 50, False)
    collider4 = wall(900,100,50,50, True)
    clock.tick(60)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    x, y = man.x, man.y

    if keys[pygame.K_a] and man.x > man.vel:
        x -= man.vel
        man.left = True
        man.right = False
    elif keys[pygame.K_d] and man.x < scrWidth - man.width - man.vel:
        x += man.vel
        man.right = True
        man.left = False
    elif keys[pygame.K_w] and man.y > man.vel:
        y -= man.vel
        man.left = False
        man.right = False
    elif keys[pygame.K_s] and man.y < scrHeight - man.height - man.vel:
        y+= man.vel
        man.left = False
        man.right = False
    else:
        man.right = False
        man.left = False
        man.walkCount = 0

    man.hitbox(15, 0, 31, 17)
    player_rect = pygame.Rect(x, y, 50, 55)
    if collider2.playerCollide(player_rect) == False and collider1.playerCollide(player_rect) == False and collider3.playerCollide(player_rect) == False and collider4.playerCollide(player_rect) == False:
        man.x, man.y = x, y
    redrawGameWindow()
pygame.quit()
1

There are 1 answers

3
Rabbid76 On BEST ANSWER

The issue is cause by the multiple calls to pygame.display.update(). Remove the call to pygame.display.update() from the class wall and do just 1 pygame.display.update() at the end of redrawGameWindow.
However, you must draw the background before you draw the boxes, otherwise the background will cover the boxes:

def redrawGameWindow():
    # win.blit(bg, (-50,-200)) <--- DELETE
    man.draw(win)
    #man.drawhitbox(win)
    pygame.display.update()
run = True
while run:
    win.blit(bg, (-50,-200)) # <--- ADD

    collider1 = wall(500, 400, 200, 200, True)
    collider2 = wall(200,100,50, 50, False)
    collider3 = wall(700,100,50, 50, False)
    collider4 = wall(900,100,50,50, True)

    # [...]

    redrawGameWindow()

Simplify the code by creating a list of colliders:

colliders = [
   wall(500, 400, 200, 200, True),
   wall(200,100,50, 50, False),
   wall(700,100,50, 50, False),
   wall(900,100,50,50, True)] 
player_rect = pygame.Rect(x, y, 50, 55)
if not any(c.playerCollide(player_rect) for c in colliders):
    man.x, man.y = x, y