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