0

I have been trying to make collisions in a game i'm working on and am trying to blit dialogue whenever two rects collide and the player presses e but cant figure it out

whenever i test it it shows me the dialogue once in 2 presses or sometimes never but when i put a print statement in the if part it works just fine but still doesnt blit the dialogue

import pygame
from sys import exit

pygame.init()

WIDTH = 800
HEIGHT = 800

x = 0
y = 0

my_font = pygame.font.Font(None, 30)

screen = pygame.display.set_mode((WIDTH, HEIGHT))

test_rect = pygame.Rect((x,y),(40,40))
dialogue_rect = pygame.Rect((150,150),(150,150))
dialogue = my_font.render('Hello mate', False, 'black')

running = True
while running:

    screen.fill((255,255,255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False
            exit()

    def move():
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            test_rect.move_ip(0,-1)
        elif keys[pygame.K_DOWN]:
            test_rect.move_ip(0,1)
        elif keys[pygame.K_RIGHT]:
            test_rect.move_ip(1,0)
        elif keys[pygame.K_LEFT]:
            test_rect.move_ip(-1,0)

    move()

    if test_rect.colliderect(dialogue_rect): 
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.type == pygame.K_e:
                    screen.blit(dialogue,(400,100))

    pygame.draw.rect(screen, 'blue', test_rect)
    pygame.draw.rect(screen, 'green', dialogue_rect)

    pygame.display.update()
  • i tried putting the collision code into the for loop but that didnt work either – Randommcguy25 Jul 24 '22 at 10:53
  • 1
    The problem are the multiple calls of pygame.event.get(). See [Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?](https://stackoverflow.com/questions/58086113/faster-version-of-pygame-event-get-why-are-events-being-missed-and-why-are/58087070#58087070) – Rabbid76 Jul 24 '22 at 11:11
  • Also, `if event.key == pygame.K_e:` instead of `if event.type == pygame.K_e:` – The_spider Jul 24 '22 at 11:13
  • @The_spider i tried that but it didnt work either...anything else? – Randommcguy25 Jul 24 '22 at 12:14

0 Answers0