0

I am currently making a top-down shooter game in Pygame and I want the Enemy to be removed when you hit it 4 times with the player_bullets. I am unsure how to do this as I am new to coding. I have watched a few tutorials but many seem to be too complicated or don't solve my problem. I hope someone can help.

import pygame
import sys
import math
import random

pygame.init()

displayWidth = 100
displayHeight = 200

LENGTH = 5
WIDTH = 5

loopcount = 1000
MINPOS = 0
MAXPOS = 3000

surface = pygame.display.set_mode((displayWidth, displayHeight))

#caption
pygame.display.set_caption('ArcadeGame')

Earth = pygame.image.load("Earth.png")

#ImageBackground = pygame.image.load("Space.png")
#Display name
displayImage = pygame.image.load('Name.png')
Earth = pygame.image.load('earth.png')

display = pygame.display.set_mode((900, 700))
clock = pygame.time.Clock()

player_walk_images = [pygame.image.load("player_walk_0.png"), pygame.image.load("player_walk_1.png"),
pygame.image.load("player_walk_2.png"), pygame.image.load("player_walk_3.png")]


player_weapon = pygame.image.load("gun.png").convert()
player_weapon.set_colorkey((255, 255, 255))




#Player
class Player:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.animation_count = 0
        self.moving_right = False
        self.moving_left = False
    def handle_weapons(self, display):
        mouse_x, mouse_y = pygame.mouse.get_pos()

        rel_x, rel_y = mouse_x - player.x, mouse_y - player.y
        angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)

        player_weapon_copy = pygame.transform.rotate(player_weapon, angle)

        display.blit(player_weapon_copy, (self.x + 15 - int(player_weapon.get_width()/2), (self.y+25-int(player_weapon_copy.get_height()/2))))

    def main(self, display):
        if self.animation_count +1 >= 16:
            self.animation_count = 0

        self.animation_count += 1



        if self.moving_right:
            display.blit(pygame.transform.scale(player_walk_images[self.animation_count//4], (100, 100)), (self.x, self.y))
        elif self.moving_left:
            display.blit(pygame.transform.scale(pygame.transform.flip(player_walk_images[self.animation_count//4], True, False), (100, 100)), (self.x, self.y))
        else:
            display.blit(pygame.transform.scale(player_walk_images[0], (100, 100)), (self.x, self.y))

        self.handle_weapons(display)
        self.moving_right = False
        self.moving_left = False

#Bullets
class PlayerBullet:
    def __init__(self, x, y, mouse_x, mouse_y):
        self.x = x
        self.y = y
        self.mouse_x = mouse_x
        self.mouse_y = mouse_y
        self.speed = 15
        self.angle = math.atan2(y-mouse_y, x-mouse_x)
        self.x_vel = math.cos(self.angle) * self.speed
        self.y_vel = math.sin(self.angle) * self.speed
    def main(self, display):
        self.x -= int(self.x_vel)
        self.y -= int(self.y_vel)


        PlayerBulletRect = pygame.draw.circle(display, (255,0,0), (self.x, self.y), 5)

#Enemy's
class Enemy1(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.hit_box = (self.x-10, self.y -10, 70, 70)
        self.animation_images = [pygame.image.load("Enemy1_animation_0.png"), pygame.image.load("Enemy1_animation_1.png"),
        pygame.image.load("Enemy1_animation_2.png"), pygame.image.load("Enemy1_animation_3.png")]
        self.animation_count = 0
        self.reset_offset = 0
        self.offset_x = random.randrange(-150, 150)
        self.offset_y = random.randrange(-150, 150)
        self.health = 4


    def main(self, display):
        if self.animation_count + 1 == 16:
            self.animation_count = 0
        self.animation_count += 1

        if self.reset_offset == 0:
            self.offset_x = random.randrange(-150, 150)
            self.offset_y = random.randrange(-150, 150)
            self.reset_offset = random.randrange(120, 150)
        else:
            self.reset_offset -= 1

        if player.x + self.offset_x > self.x-display_scroll[0]:
            self.x += 1
        elif player.x + self.offset_x < self.x-display_scroll[0]:
            self.x -= 1

        if player.y + self.offset_y > self.y-display_scroll[1]:
            self.y += 1
        elif player.y + self.offset_y < self.y-display_scroll[1]:
            self.y -= 1

        display.blit(pygame.transform.scale(self.animation_images[self.animation_count//4], (50, 50)), (self.x-display_scroll[0], self.y-display_scroll[1]))

        self.hit_box = (self.x-display_scroll[0]-10, self.y-display_scroll[1]-10, 70, 70)
        pygame.draw.rect(display, (255, 0, 0), self.hit_box, 2)

class Enemy2(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.hit_box = (self.x+5, self.y +10, 70, 70)
        self.animation_images = [pygame.image.load("Enemy1_animation_0 copy.png"), pygame.image.load("Enemy1_animation_1 copy.png"),
        pygame.image.load("Enemy1_animation_2 copy.png"), pygame.image.load("Enemy1_animation_3 copy.png")]
        self.animation_count = 0
        self.reset_offset = 0
        self.offset_x = random.randrange(-150, 150)
        self.offset_y = random.randrange(-150, 150)


    def main(self, display):
        if self.animation_count + 1 == 16:
            self.animation_count = 0
        self.animation_count += 1

        if self.reset_offset == 0:
            self.offset_x = random.randrange(-150, 150)
            self.offset_y = random.randrange(-150, 150)
            self.reset_offset = random.randrange(120, 150)
        else:
            self.reset_offset -= 1

        if player.x + self.offset_x > self.x-display_scroll[0]:
            self.x += 1
        elif player.x + self.offset_x < self.x-display_scroll[0]:
            self.x -= 1

        if player.y + self.offset_y > self.y-display_scroll[1]:
            self.y += 1
        elif player.y + self.offset_y < self.y-display_scroll[1]:
            self.y -= 1

        display.blit(pygame.transform.scale(self.animation_images[self.animation_count//4], (80, 80)), (self.x-display_scroll[0], self.y-display_scroll[1]))

        self.hit_box = (self.x-display_scroll[0]+5, self.y-display_scroll[1]+10, 70, 70)
        pygame.draw.rect(display, (0, 0, 0), self.hit_box, 2)




enemies = [Enemy1(600, 400), Enemy2(800, -200)]


player = Player(400, 300, 32, 32)

display_scroll = [0,0]

player_bullets = []

while True:
    display.fill((0, 0, 0))
    display.blit(displayImage, (0, 0))
    #display.blit(ImageBackground, (0, 0))
    display.blit(Earth, (700, 100))


    mouse_x, mouse_y = pygame.mouse.get_pos()

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

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                player_bullets.append(PlayerBullet(player.x, player.y, mouse_x, mouse_y))

    keys = pygame.key.get_pressed()

    #stars
    pygame.draw.rect(display, (255, 255, 255), (100-display_scroll[0], 100-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (150-display_scroll[0], 150-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (200-display_scroll[0], 700-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (10-display_scroll[0], 300-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (500-display_scroll[0], 300-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (200-display_scroll[0], 200-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (500-display_scroll[0], 500-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (600-display_scroll[0], 300-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (1-display_scroll[0], 23-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (1000-display_scroll[0], 1500-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (5000-display_scroll[0], 5000-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (-400-display_scroll[0], -100-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (-2000-display_scroll[0], -2000-display_scroll[1], 5, 5))
    pygame.draw.rect(display, (255, 255, 255), (-1000-display_scroll[0], -1000-display_scroll[1], 5, 5))

    #Movement
    if keys[pygame.K_a]:
        display_scroll[0] -=5

        player.moving_left = True

        for bullet in player_bullets:
            bullet.x -= 5

    if keys[pygame.K_d]:
        display_scroll[0] +=5

        player.moving_right = True

        for bullet in player_bullets:
            bullet.x += 5

    if keys[pygame.K_w]:
        display_scroll[1] -=5

        for bullet in player_bullets:
            bullet.x -= 5

    if keys[pygame.K_s]:
        display_scroll[1] +=5

        for bullet in player_bullets:
            bullet.x += 5


    player.main(display)

    for bullet in player_bullets:
        bullet.main(display)

#Bullet collision with enemy

    for enemy in enemies:
        for bullet in player_bullets:
            if pygame.Rect(enemy.hit_box).collidepoint(bullet.x, bullet.y):
                player_bullets.remove(bullet)
        enemy.main(display)


    clock.tick(60)
    pygame.display.update()

1 Answers1

2

Pygame has an easy way for collisions. You can use the pygame.Rect.

pygame.Rect(x, y, width, height).collidepoint(x, y) returns a True when the point is in the rectangle.

for enemy in enemies:
    for bullet in player_bullets:
        if pygame.Rect(enemy.hit_box).collidepoint(bullet.x, bullet.y):
            do something...
Jerry
  • 401
  • 4
  • 11
  • 1
    @Maddox, there are more methods on the Rect object, which may help you with collision of different objects in collections. The docs are good help if you want to implement something specific – Cpreet Jul 29 '22 at 07:40