1

Here is the snippet of code which is supposed to detect when the SPACE key is pressed.

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
             part = 3

It worked fine in other projects, but it is barely detecting the key press now. It sometimes detects it, but it is rare. I know that I could use pygame.key.get_pressed() but it will return True as long as the space key is held, which is not something that I want.

Full code: please excuse the lack of comments

import pygame,sys,math,random
from pygame.locals import *

from pygame.mixer import *


def main():

    display = pygame.display.set_mode((1600,900),FULLSCREEN)
    tv = pygame.image.load("tv.png")
    tv = pygame.transform.scale(tv,(1600,900))
    bg1 = pygame.image.load("bg.png")
    bg2 = pygame.image.load("bg2.png")


    swiperPos = False
    pygame.mixer.init()
    startmusic = pygame.mixer.Sound("introsong.mp3")
    pygame.mixer.Channel(0).play(startmusic,loops = 1000)
    part = 0
    pygame.font.init()
    arial = pygame.font.SysFont("arial",41)
    georgia = pygame.font.SysFont("georgia",41)
    arialsmall = pygame.font.SysFont("arial", 20)
    georgiasmall = pygame.font.SysFont("georgia", 20)
    vhsEffect = True
    oldMousePressed = [False,False,False]

    while True:
        display.fill((0,0,251))
        mousePos = pygame.mouse.get_pos()
        mousePressed = [False,False,False]
        rawMousePressed = [pygame.mouse.get_pressed(3)[0],pygame.mouse.get_pressed(3)[1],pygame.mouse.get_pressed(3)[2]]
        if rawMousePressed[0] and not oldMousePressed[0]: mousePressed[0] = True
        if rawMousePressed[1] and not oldMousePressed[1]: mousePressed[1] = True
        if rawMousePressed[2] and not oldMousePressed[2]: mousePressed[2] = True
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

                sys.exit()
        if part == 0:
            display.blit(bg1,(0,0))

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    quit()
                    sys.exit()
            if mousePressed[0]:
                if mousePos[0] > 406 and mousePos[0] < 580 and mousePos[1] > 540 and mousePos[1] < 564:

                    part = 2
                elif mousePos[0] > 951 and mousePos[0] < 1131 and mousePos[1] > 540 and mousePos[1] < 564:

                    part = 1
        if part == 1:
            display.blit(bg2,(0,0))
            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    quit()
                    sys.exit()

            if mousePressed[0]:

                if mousePos[0] > 557 and mousePos[0] < 940 and mousePos[1] > 207 and mousePos[1] < 240:
                    if vhsEffect:
                        vhsEffect = False
                    else:
                        vhsEffect = True
                elif mousePos[0] > 700 and mousePos[0] < 754 and mousePos[1] > 651 and mousePos[1] < 670:
                    part = 0
            text = georgia.render(f"VHS EFFECTS: {vhsEffect}",True,(251,251,251))
            display.blit(text,(561,201))
        if part == 2:
            text = georgia.render("MIKE: Jake, do you want to go camping?",False,(251,251,251))
            display.blit(text,(401,201))
            text = georgiasmall.render("PRESS SPACE TO CONTINUE", False, (251, 251, 251))
            display.blit(text, (601, 701))
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                     if event.key == pygame.K_SPACE:
                           part = 3
        if part == 3:
            text = georgia.render("MIKE: Jake, do you want to go camping?",False,(251,251,251))
            display.blit(text,(401,201))
            text = georgia.render("JAKE: Sure dude, what park?", False, (251, 251, 251))
            display.blit(text, (401, 301))
            text = georgiasmall.render("PRESS SPACE TO CONTINUE", False, (251, 251, 251))
            display.blit(text, (601, 701))
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                     if event.key == pygame.K_SPACE:
                          part = 4
        text = arial.render(f"MOUSEPOS:{mousePos}",True,(251,251,251))

        display.blit(text,(541,51))






        # AFTER EFFECTS
        if vhsEffect:
            integer = random.randrange(0,200)
            if integer == 1 and not swiperPos:

                swiperPos = 1
            if swiperPos != False:
                for x in range(1600):
                    colour = display.get_at((x,swiperPos))
                    pygame.draw.line(display,colour,(x,swiperPos),(x,swiperPos + 80),1)
                swiperPos += 8
                if swiperPos > 890:
                    swiperPos = False
            pick = False
            for i in range(501):
                surface = pygame.Surface([1600, 2], pygame.SRCALPHA)
                integer = random.randrange(0,8000)
                if integer == 1:

                    surface = pygame.Surface([1600,2])
                    surface.fill((251,251,251))
                else:
                    if not pick:
                        surface.set_alpha(70)
                        surface.fill((251,251,251))
                        pick = True
                    else:
                        surface.set_alpha(70)
                        surface.fill((0,0,0))
                        pick = False
                display.blit(surface,(0,i * 2))
            display.blit(tv,(0,0))
        oldMousePressed = rawMousePressed

        pygame.display.update()
main()
saalty
  • 13
  • 4

1 Answers1

1

pygame.event.get() get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. [...]

If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.

Get the events once per frame and use them in multiple loops or pass the list of events to functions and methods where they are handled:

while True:
    # [...]        

    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:

    # [...]

    if part == 0:
        # [...]

        for event in event_list:
            # [...]

    if part == 1:
        # [...]

        for event in event_list:
            # [...]

    if part == 2:
        # [...]

        for event in event_list:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    part = 3

    if part == 3:
        # [...]

        for event in event_list:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    part = 4

            quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174