1

Why does pygame.event.get().key not recognize any keypresses to control an Img? I've tried debugging and printing all most every line but it doesn't work; I'm not sure if it's pygame fault or my code's. Here's the code:-

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

pygame.init()

width, height = int(2/3*900),int(2/3*800)
win = pygame.display.set_mode((width, height),pygame.RESIZABLE)
pygame.display.set_caption("TITLE SETTABLE")
pygame.display.set_icon(pygame.image.load('assets/gun/pst.png'))

fps = 60
black = (0,0,0)

def imgLoad(a,x,y):
    return pygame.transform.scale(pygame.image.load(a).convert_alpha(),(x,y))

images = {
    "guns" : {
        "assault_rifle" : imgLoad("assets/gun/ar.png",360,145),
        "barrel_gun" : imgLoad("assets/gun/bar.png",120,80),
        "hand_pistol" : imgLoad("assets/gun/pst.png",125,85),
        "shotgun" : imgLoad("assets/gun/shg.png",400,115),
        "submachine_gun" : imgLoad("assets/gun/smg.png",330,120),
        "sniper" : imgLoad("assets/gun/snp.png",445,130),
    },
    "bullets" : {
        "assault_rifle" : imgLoad("assets/gun/arB.png",15,65),
        "barrel_gun" : imgLoad("assets/gun/barB.png",20,60),
        "hand_pistol" : imgLoad("assets/gun/pstB.png",15,40),
        "shotgun" : imgLoad("assets/gun/shgB.png",15,75),
        "submachine_gun" : imgLoad("assets/gun/smgB.png",15,40),
        "sniper" : imgLoad("assets/gun/snpB.png",15,75),
    },
    "stores" : {
        "ammobox" : imgLoad("assets/gun/box.png",180,125),
        "grenade" : imgLoad("assets/gun/bomb.png",65,75),
        #"chest" : imgLoad("assets/gun/~chest.png"),
    },
    "players" : {
        "red" : imgLoad("assets/player/ps0.png",50,50),
        "green" : imgLoad("assets/player/ps1.png",50,50),
        "blue" : imgLoad("assets/player/ps2.png",50,50),
        "yellow" : imgLoad("assets/player/ps3.png",50,50),
        "cyan" : imgLoad("assets/player/ps4.png",50,50),
        "magenta" : imgLoad("assets/player/ps5.png",50,50),
        "white" : imgLoad("assets/player/ps6.png",50,50),
        "brown" : imgLoad("assets/player/ps7.png",50,50),
    }
}



class Player:
    def __init__(self,x,y,color,velocity,img_dict):
        self.color = color
        self.img = img_dict["players"][self.color]
        self.x = x
        self.y = y
        self.vel = velocity

    def input(self):
        for event in pygame.event.get():
            print("forevent/")
            if event.type == KEYDOWN:
                print("keydown/")
                if (event.key == K_w) or (event.key == K_UP):
                    self.y = self.y - self.vel
                    print("w/")
                if (event.key == K_a) or (event.key == K_LEFT):
                    self.x = self.x - self.vel
                    print("a/")
                if (event.key == K_s) or (event.key == K_DOWN):
                    self.y = self.y + self.vel
                    print("s/")
                if (event.key == K_d) or (event.key == K_RIGHT):
                    self.x = self.x + self.vel
                    print("d/")

    def update(self,windowVar):
        self.input()
        windowVar.blit(self.img,(self.x,self.y))

class Gun:
    def __init__(self,x,y,current_gun,img_dict):
        self.player_state = False
        self.current = current_gun
        self.x = x
        self.y = y
        self.original_gun = img_dict["guns"][self.current]
        self.original_bullet = img_dict["bullets"][self.current]
        self.gun = self.original_gun
        self.bullet = self.original_bullet

    def update(self,windowVar):
        windowVar.blit(self.gun,(self.x,self.y))


def draw_all(p,img_dict):
    win.fill(black)
    # for i in img_dict:
    #   j = img_dict[i]
    #   for k in j:
    #       l = j[k]
    #       win.blit(l,(random.randint(0,width),random.randint(0,width)))
    p.update(win)
    pygame.display.update()


def main():
    clock = pygame.time.Clock()
    p = Player(width/2,height/2,"blue",25,images)
    running = True
    while running:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False


        draw_all(p,images)

    pygame.quit()


if __name__ == '__main__':
    main()

I'm using Python v3.8.3

Ninjago77
  • 59
  • 4
  • first of there is no need for those parentheses: `if (event.key == K_w) or (event.key == K_UP)` just type: `if event.key == K_w or event.key = K_UP`, second do You get those print statements that You have placed there? also this is a violation of PEP8: `import pygame, random, math` each module should be imported on a new line, and doing this: `from pygame.locals import *` is bad practice, You should import only what You need or import it as a module – Matiiss Jun 18 '21 at 11:20

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.

class Player:
    # [...]

    def input(self, event_list):
        for event in event_list:
            print("forevent/")
            # [...]

    def update(self, windowVar, event_list):
        self.input(event_list)
        windowVar.blit(self.img,(self.x,self.y))
class Gun:
    # [...]

    def update(self, windowVar, event_list):
        windowVar.blit(self.gun,(self.x,self.y))


def draw_all(p, img_dict, event_list):
    win.fill(black)
    # [...]

    p.update(win, event_list)

    pygame.display.update()
def main():
    clock = pygame.time.Clock()
    p = Player(width/2,height/2,"blue",25,images)
    running = True
    while running:
        clock.tick(fps)

        event_list = pygame.event.get()

        for event in event_list:
            if event.type == pygame.QUIT:
                running = False

        draw_all(p, images, event_list)

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