Have Enemies Shoot at Player Problem? pygame

Viewed 372

I am trying to make my enemy shoot my player but for some reason the bullets arent showing nor they are shooting at the player I don't even know why, I even draw the bullets on my window VIDEO

bulls = []
runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runninggame = False

    for bull in bulls:
        if bull.x < 500 and bull.x > 0:
            bull.x += bull.vel  # Moves the bullet by its vel
        else:
            for enemys in enemying:
                if playerman.x - 100 <= enemys.x <= playerman.x + 100 and playerman.y - 100 <= enemys.y <= playerman.y + 100:
                    bulls.pop(bulls.index(bull))
                    bulls.append(enemybullets(round(enemys.x+enemys.width//2), round(enemys.y + enemys.height//2), 6, (0,0,0), facing)) 


then I called it below

    for bull in bulls:
        bull.draw(window)

my enemy projectile class

# enemys bullets
class enemybullets(object):
    def __init__(self,x,y,radius,color,facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self,win):
        pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)



1 Answers

This is the code of a very similar game I made with the help of a tutorial some time ago.

My first suggestion is try reducing the velocity of the bullets.That way you can make sure that you are not missing anything that's on the screen.

Next make sure that you are updating the screens contents properly.

It is difficult to give a comprehensive answer without having a look at your full code.I have included my code here so that you could reference and fix the issue.

Hope this helps.

import pygame

pygame.init()

win = pygame.display.set_mode((800, 480))

clock = pygame.time.Clock()

char = pygame.image.load('standing.png')

bulletSound = pygame.mixer.Sound('gshot.wav')
shotHit = pygame.mixer.Sound('bounce.wav')
backSound = pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play(-1)


walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
            pygame.image.load('L4.png'), pygame.image.load('L5.png'),
            pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'),
            pygame.image.load('L9.png')]

walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
             pygame.image.load('R4.png'), pygame.image.load('R5.png'),
             pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'),
             pygame.image.load('R9.png')]

b_walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),
            pygame.image.load('L4E.png'), pygame.image.load('L5E.png'),
            pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'),
            pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
b_walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'),
             pygame.image.load('R4E.png'), pygame.image.load('R5E.png'),
             pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'),
             pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]

pygame.display.set_caption("My Samurai(v1.0)")

x, y = 50, 50
bx, by = 250, 250
p_width, p_height = 40, 50
b_width, b_height = 40, 50

jumpCount = 5
walkCount = 0


class bullets:

    def __init__(self, x, y, surface, mov_speed):
        self.x = x
        self.y = y
        self.surface = surface
        self.mov_speed = mov_speed

    def draw(self):
        pygame.draw.circle(self.surface, (200, 20, 20), (self.x, self.y), 3, 1)
        self.x += self.mov_speed






class figure:

    def __init__(self, x, y, width, height, surface):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.surface = surface
        self.jumpCount = 5
        self.jump = False
        self.walkCount = 0
        self.move = False
        self.maxCount = 0
        self.hitboxWidth = 55
        self.hitboxHeight = 55
        self.hitbox = [self.x, self.y, self.hitboxWidth, self.hitboxHeight]

    def p_jump(self):
        if self.jump:
            print("i came here")
            if self.jumpCount > 0:
                self.y -= int((self.jumpCount ** 2) / 2) * 6
                self.jumpCount -= 1
                print("im here 1", self.jumpCount, "y pos", self.y)
            else:
                self.y += int((self.jumpCount ** 2) / 2) * 6
                self.jumpCount -= 1
                print("i came here 2", self.jumpCount)

            if self.jumpCount == -6:
                self.jumpCount = 5
                self.jump = False
                print(jump)

    def draw(self, dirL, dirR, surface, arrayL, arrayR):
        #pygame.draw.rect(self.surface, (255, 0, 0), (self.hitbox[0], self.hitbox[1], self.hitboxWidth, self.hitboxHeight), 1)
        if dirL:
            if self.move:
                surface.blit(arrayL[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            else:
                surface.blit(arrayL[self.walkCount // 3], (self.x, self.y))

        if dirR:
            if self.move:
                surface.blit(arrayR[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            else:
                surface.blit(arrayR[self.walkCount // 3], (self.x, self.y))

        if self.walkCount > self.maxCount:
            self.walkCount = 0


speed = 5
jump = False
run = True
left = False
right = True
player = figure(x, y, p_width, p_height, win)
beast = figure(bx, by, b_width, b_height, win)
player.maxCount = 24
beast.maxCount = 30
beast.move = True
player.hitboxWidth = 42
beast.hitboxWidth = 42
body = []
i = 0
bSpeed = 8
score = 0
score_font = pygame.font.SysFont('arial', 20, 1, 1)
hit_font = pygame.font.SysFont('calibri', 80, 1, 1)
while run:
    pygame.time.delay(50)
    pygame.display.update()
    clock.tick(27)
    bg = pygame.image.load('bg.jpg')
    win.blit(bg, (0, 0))
    score_display = score_font.render("Score: {}".format(score), 1, (0, 0, 0))
    win.blit(score_display, (700, 10))
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player.x > 0:
        player.x -= speed
        left = True
        right = False
        player.move = True
        bSpeed = -8
    elif keys[pygame.K_RIGHT] and player.x + p_width + speed + 5 < 800:
        player.x += speed
        left = False
        right = True
        player.move = True
        bSpeed = 8
    else:
        player.move = False
        player.walkCount = 0

    if keys[pygame.K_UP] and player.y > 0 and not jump:
        player.y -= speed
    if keys[pygame.K_DOWN] and player.y + p_height + speed + 5 < 480 and not jump:
        player.y += speed
    if keys[pygame.K_SPACE]:
        player.jump = True
    if keys[pygame.K_s] and i <= 0:
        body.append(bullets(player.x + 32, player.y + 40, win, bSpeed))
        i = 4
        bulletSound.play()
    for bullet in body:
        bullet.draw()
        if beast.x + 25 < bullet.x < beast.x + beast.hitboxWidth:
            if beast.y < bullet.y < beast.y + beast.hitboxHeight:
                body.pop(body.index(bullet))
                score += 1
                shotHit.play()


    # beast position cal
    if beast.x - player.x > 0:
        b_right = False
        b_left = True
        beast.x -= int((beast.x - player.x) * 0.025)
    else:
        b_right = True
        b_left = False
        beast.x -= int((beast.x - player.x) * 0.025)

    if beast.y - player.y > 0:
        beast.y -= int((beast.y - player.y) * 0.025)
    else:
        beast.y -= int((beast.y - player.y) * 0.025)

    if i > 0:
        i -= 1

    player.p_jump()
    player.draw(left, right, win, walkLeft, walkRight)
    beast.draw(b_left, b_right, win, b_walkLeft, b_walkRight)

    player.hitbox = [player.x + 10, player.y + 8, player.hitboxWidth, player.hitboxHeight]
    beast.hitbox = [beast.x + 10, beast.y + 5, beast.hitboxWidth, beast.hitboxHeight]

    if (player.hitbox[0] < beast.hitbox[0] < player.hitbox[0] + player.hitbox[2] - 20) or\
            (player.hitbox[0] + 15 < beast.hitbox[0] + beast.hitbox[2] <= player.hitbox[0] + player.hitbox[2]):
        if (player.hitbox[1] < beast.hitbox[1] < player.hitbox[1] + player.hitbox[3] - 10) or\
                (player.hitbox[1] + 10 < beast.hitbox[1] + beast.hitbox[3] < player.hitbox[1] + player.hitbox[3]):
            score -= 5
            hit_display = hit_font.render("-5", 1, (255, 0, 0))
            win.blit(hit_display, (400, 200))
            pygame.display.update()
            pygame.time.delay(1000)
            player.x, player.y = 100, 100
            beast.x, beast.y = 600, 300
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False



pygame.quit()
Related