0
\$\begingroup\$

Every time I try to operate player one and two or try to use the pickup key it stops the players. here is my code:

import pygame, sys, math
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((700,700))
screen.fill((255,255,255))
pygame.key.set_repeat(5,5)
import random
p1="james_u"
p2="billy_u"
p1_face= "r"
p2_face= "r"
p1h="health p1 4 HP"
p2h="health p2 4 HP"
player = 1
BG=pygame.image.load("Bgg.png")
H1=4
H2=4
G1X=random.randint(10,500)
G1Y=random.randint(10,500)

x_collide=[]
y_collide=[]

class Dude:
    char_width = 0
    char_height = 0

    def __init__(self, newX, newY):
        self.x = newX
        self.y = newY
        self.img = pygame.image.load(p2+".png")
        myRec = self.img.get_rect()
        Dude.char_width = myRec[2]
        Dude.char_height = myRec[3]
        
    def draw(self, window):
        window.blit(self.img, (self.x,self.y))

    
    def moveLeft(self):
        self.x = self.x - 5   
        p2_face = "l"

    def moveRight(self):
        self.x = self.x + 5
        p2_face = "r"    
       
    def moveUp(self):
        self.y = self.y -5
        p2_face = "u"    
    
    def moveDown(self):
        self.y = self.y +5
        p2_face = "d"        
        
    def getRec(self):
        return (self.x, self.y, myRec[2], myRec[3])    

class Broski:
    char_height= 0
    char_width= 0
    def __init__(self, newX, newY):
        self.x = newX
        self.y = newY
        self.x_spd = 0
        self.y_spd = 0
        self.img = pygame.image.load(p1+".png")
        myRec = self.img.get_rect()
        Broski.char_width = myRec[2]
        Broski.char_height = myRec[3]
        
    def draw(self, window):
        window.blit(self.img, (self.x,self.y))
    
    def move(self):
        self.x += self.x_spd
        self.y += self.y_spd
        self.x_spd = 0
        self.y_spd = 0
        
        
    def getRec(self):
        print(myRec)
        return (self.x, self.y, myRec[2], myRec[3]) 


class BG:
    def __init__(self, newX, newY):
        self.x = newX
        self.y = newY
        self.img = pygame.image.load("Bgg.png")
    
    def draw(self, window):
        window.blit(self.img, (self.x,self.y))

    def getRec(self):
        myRec = self.img.get_rect()
        print(myRec)
        return (self.x, self.y, myRec[2], myRec[3])

class Health:
    def __init__(self, newX, newY, newH):
        self.x = newX
        self.y = newY
        self.img = pygame.image.load(newH +".png")  
    def draw(self, window):
        window.blit(self.img, (self.x,self.y))

class Weapon:
    gun_list=[]
    gun_list_A=[]
    gun_count=0
    Player = "None"
    def __init__(self, newX, newY, idle_Image, Speed, Damage, Arc_Angle, R_Speed, active_Image):
        self.x = newX
        self.y = newY
        self.active_Image = active_Image
        self.idle_Image = idle_Image
        self.img = pygame.image.load(idle_Image +".png")  
        myRec = self.img.get_rect()
        Weapon.gun_count+=1
        Weapon.gun_list.append(Weapon.gun_count)
        Weapon.gun_list.append(newX)
        Weapon.gun_list.append(newY)
        Weapon.gun_list.append(myRec[2])
        Weapon.gun_list.append(myRec[3])
        Weapon.gun_list.append(Speed)
        Weapon.gun_list.append(Damage)
        Weapon.gun_list.append(Arc_Angle)
        Weapon.gun_list.append(R_Speed)
        Weapon.gun_list_A.append(Weapon.gun_list)
        Weapon.gun_list=[]
    def playerlock(self, player):#once picked up it locks onto the player
        if math.sqrt((player.x-self.x)**2 + (player.y-self.y)**2) <= 150:
            self.img=pygame.image.load(self.active_Image+".png")
            Weapon.Player = player
        #positianal argument for weapons placement on player

    def draw(self, window):
        if Weapon.Player == bro:
            self.active_Image = self.idle_Image + p1_face
            self.x = bro.x -10
            self.y = bro.y -10
        if Weapon.Player == person:
            self.active_Image = self.idle_Image + p2_face
            self.x = person.x - 10
            self.y = person.y - 10
        window.blit(self.img, (self.x,self.y))
        #idle image must be gun name and nothing else while active is directional
    def getRec(self):
        print(myRec)
        return (self.x, self.y, myRec[2], myRec[3])

class Bullet:
    bul_width=0
    bul_height=0
    x=0
    y=0
    def __init__(self, newX, newY, Image, Player):
        self.x = newX
        self.y = newY
        x=self.x
        y=self.y
        self.player = Player
        self.img = pygame.image.load(Image +".png")
        myRec = self.img.get_rect()
        Bullet.bul_width= myRec[2]
        Bullet.bulsd_height= myRec[3]
    def shoot():
        print("shoot")
    
    def draw(self, window):
        window.blit(self.img, (self.x,self.y))

    def getRec(self):
        print(myRec)
        return (self.x, self.y, myRec[2], myRec[3])



class Object:
    ob_count = 0
    ob_list=[]
    ob_list_alpha=[]
    def __init__(self, newX, newY, Image):
        self.x = newX
        self.y = newY
        self.img = pygame.image.load(Image)
        myRec = self.img.get_rect()
        Object.ob_count += 1
        Object.ob_list.append(Object.ob_count)
        Object.ob_list.append(newX)
        Object.ob_list.append(newY)
        Object.ob_list.append(myRec[2])
        Object.ob_list.append(myRec[3])
        Object.ob_list_alpha.append(Object.ob_list)
        Object.ob_list = []
   
    def draw(self, window):
        window.blit(self.img, (self.x,self.y))

    def getRec(self):
        print(myRec)
        return (self.x, self.y, myRec[2], myRec[3])

# class does_collide:
#     def __init__ (self, item):
#         self.it = item
#         x_collide.append((self.it.img.get_rect(1))+Object.ob_list_alpha[1])
       
crate_1=Object(200,200,"Yellow_Cargo_Closed.png")
crate_2=Object(500,200,"Red_Cargo_Closed_Horz.png")
col_l = False
col_r = False
col_u = False
col_d = False
col_l2 = False
col_r2 = False
col_u2 = False
col_d2 = False
bggt=BG(0,0)
bro=Broski(10,10)
none=Broski(50,10)
person=Dude(665,560)
p1health=Health(35,620,p1h)
p2health=Health(465,620,p2h)
Gun_1=Weapon(G1X,G1Y,"glock",10,2,60,3,"glocku")
B_P1_1=Bullet(20,20, "Bullet_P1_1", "P1")
PL = none
while(True):
    screen.fill((255,255,255))
    bggt.draw(screen)
    person.draw(screen)
    bro.draw(screen) 
    B_P1_1.draw(screen)
    crate_1.draw(screen)
    crate_2.draw(screen)
    p1health.draw(screen)
    p2health.draw(screen)
    Gun_1.draw(screen)
    pygame.display.update()
    key=pygame.key.get_pressed()
    for event in pygame.event.get():
        if (event.type==QUIT):
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            #DEBUG HELP
            if key[pygame.K_LCTRL] and [pygame.K_b]:
                print("deebug")
                print(Gun_1.idle_Image+p1_face)
            for i in range (Object.ob_count):
                if  (person.x + Dude.char_width) >= Object.ob_list_alpha[i][1] and person.x <= (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]) and (person.y + Dude.char_height) >= Object.ob_list_alpha[i][2] and person.y <= (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]):
                    if person.x < Object.ob_list_alpha[i][1] and (person.y + Dude.char_height) > Object.ob_list_alpha[i][2] and person.y < (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]):
                        col_r = True
                        break
                    else:
                        col_r = False
                    if (person.x + person.char_width) > (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]) and (person.y + Dude.char_height) > Object.ob_list_alpha[i][2] and person.y < (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]):
                        col_l = True
                        break
                    else:
                        col_l = False
                    if person.y < Object.ob_list_alpha[i][2] and (person.x + Dude.char_width) > Object.ob_list_alpha[i][1] and person.x < (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]):
                        col_d = True
                        break
                    else:
                        col_d = False
                    if (person.y + person.char_height) > (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]) and (person.x + Dude.char_width) > Object.ob_list_alpha[i][1] and person.x < (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]):
                        col_u = True
                        break
                    else:
                        col_u = False
                else:
                    col_l = False
                    col_r = False
                    col_u = False
                    col_d = False
            for i in range (Object.ob_count):
                if  (bro.x + Broski.char_width) >= Object.ob_list_alpha[i][1] and bro.x <= (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]) and (bro.y + Broski.char_height) >= Object.ob_list_alpha[i][2] and bro.y <= (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]):
                    if bro.x < Object.ob_list_alpha[i][1] and (bro.y + Broski.char_height) > Object.ob_list_alpha[i][2] and bro.y < (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]):
                        col_r2 = True
                        break
                    else:
                        col_r2 = False
                    if (bro.x + bro.char_width) > (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]) and (bro.y + bro.char_height) > Object.ob_list_alpha[i][2] and bro.y < (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]):
                        col_l2 = True
                        break
                    else:
                        col_l2 = False
                    if bro.y < Object.ob_list_alpha[i][2] and (bro.x + bro.char_width) > Object.ob_list_alpha[i][1] and bro.x < (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]):
                        col_d2 = True
                        break
                    else:
                        col_d2 = False
                    if (bro.y + bro.char_height) > (Object.ob_list_alpha[i][2] + Object.ob_list_alpha[i][4]) and (bro.x + bro.char_width) > Object.ob_list_alpha[i][1] and bro.x < (Object.ob_list_alpha[i][1] + Object.ob_list_alpha[i][3]):
                        col_u2 = True
                        break
                    else:
                        col_u2 = False
                else:
                    col_l2 = False
                    col_r2 = False
                    col_u2 = False
                    col_d2 = False

            if key[pygame.K_UP] and col_u == False or (key[pygame.K_UP] and col_d == False and key[pygame.K_o]):
                if person.y > 10:
                    person.moveUp()
                    p2="billy_u"
                    p2_face="u"
                    if key[pygame.K_o]:
                        PL = person

            if key[pygame.K_w]  and col_u2 == False or (key[pygame.K_w] and col_d == False and key[pygame.K_q]):
                if bro.y > 10:
                    bro.y_spd += -5
                    p1_face="u"
                    p1="james_u"
                    if key[pygame.K_q]:
                        PL = bro

            if key[pygame.K_DOWN] and col_d == False or (key[pygame.K_DOWN] and col_d == False and key[pygame.K_o]):
                if person.y < 560:
                    person.moveDown()
                    p2="billy_d"
                    p2_face="d"
                    if key[pygame.K_o]:
                        PL = person
                    

            if key[pygame.K_s] and col_d2 == False or (key[pygame.K_s] and col_d == False and key[pygame.K_q]):
                if bro.y < 560:
                    bro.y_spd += 5
                    p1="james_d"
                    p1_face="d"
                    if key[pygame.K_q]:
                        PL = bro

            if key[pygame.K_LEFT] and col_l == False or (key[pygame.K_LEFT] and col_d == False and key[pygame.K_o]):
                if person.x > 10:
                    person.moveLeft()
                    p2="billy_l"
                    p2_face="l"
                    if key[pygame.K_o]:
                        PL = person
                    

            if key[pygame.K_a] and col_l2 == False or (key[pygame.K_a] and col_d == False and key[pygame.K_q]):
                if bro.x > 10:
                    bro.x_spd += -5
                    p1="james_l"
                    p1_face="l"
                    if key[pygame.K_q]:
                        PL = bro

            if key[pygame.K_RIGHT] and col_r == False or (key[pygame.K_RIGHT] and col_d == False and key[pygame.K_o]):
                if person.x < 665:
                    person.moveRight()
                    p2="billy_r"
                    p2_face="r"
                    if key[pygame.K_o]:
                        PL = person

            if key[pygame.K_d] and col_r2 == False or (key[pygame.K_d] and col_d == False and key[pygame.K_q]):
                if bro.x < 665:
                    bro.x_spd +=5
                    p1="james_r"
                    p1_face="r"
                    if key[pygame.K_q]:
                        PL = bro

                
            if B_P1_1.x == person.x and B_P1_1.y == person.y:
                if H2 > 0:
                    H2 -= 1
                B_P1_1.x = bro.x +10
                B_P1_1.y = bro.y +10

#             p1h ="health p1 "+str(H1)+" HP"
#             p2h ="health p2 "+str(H2)+" HP"
            if H1 == 3:
                p1h="health p1 3 HP"
            if H1 == 2:
                p1h="health p1 2 HP"
            if H1 == 1:
                p1h="health p1 1 HP"
            if H1 == 0:
                p1h="health p1 0 HP"
            if H2 == 3:
                p2h="health p2 3 HP"
            if H2<3 and H1>1:
                p2h="health p2 2 HP"
            if H2<2 and H2>0:
                p2h="health p2 1 HP"
            if H2<1 and H2>-1:
                p2h="health p2 0 HP"
                
            if event.type == pygame.KEYUP:
                if key[pygame.K_UP]:
                    print("hurr")
                if key[pygame.K_w]:
                    print("hurr")
                if key[pygame.K_DOWN]:
                    print("hurr")
                if key[pygame.K_s]:
                    print("hurr")
                if key[pygame.K_LEFT]:
                    print("hurr")
                if key[pygame.K_a]:
                    print("hurr")
                if key[pygame.K_RIGHT]:
                    print("hurr")
                if key[pygame.K_d]:
                    print("hurr")
            Gun_1.playerlock(PL)
            bro.move()
            p2health.img=pygame.image.load(p2h+".png")
            p1health.img=pygame.image.load(p1h+".png")
            bro.img=pygame.image.load(p1+".png")
            person.img=pygame.image.load(p2+".png")
            Gun_1.img=pygame.image.load(Gun_1.active_Image + ".png")
#             Gun_1.draw(screen)
            

I thought it was stopping because it was using a class function, but I did some messing around and figured it was the keys and I couldnt find anything online.

I've thought about switching the if statements to detect when the key isn't pressed but if pygame only lets one key be pressed at a time then the same issue arises.

If anyone has any suggestions on how to get it to detect multiple keys at once please let me know.

\$\endgroup\$
1
  • \$\begingroup\$ I am not familiar with pygame, but should key=pygame.key.get_pressed() be outside the for event in pygame.event.get() loop? Wouldn't that mean only one keypress could be evaluated in the entire loop? \$\endgroup\$ – Allister May 10 '20 at 23:36
0
\$\begingroup\$

I refer to the answer by wyattis in https://stackoverflow.com/a/37126399/13682048. The built-in pygame.key.get_pressed() returns a list and one can check multiple keys with

keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and keys[pygame.K_LEFT]:
    move_fullcube = left
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.