0

I got this code and the only thing i am missing is how to check for the collision between bullet and zombie. I tried using the center of the circle created in the PlayerBullet class but i couldnt use the variables from the class. How can I use those variables and what happens if there are multiple zombies and bullets at the same time?

import pygame
import sys
import math
import random
import time

pygame.init()

display = pygame.display.set_mode((1500, 1000))
clock = pygame.time.Clock()


zombie_image = pygame.image.load("Zombie.png")
zombie_image_copy = pygame.transform.rotozoom(zombie_image, 0, 2)
image = pygame.image.load("player.png")
player_image = pygame.transform.rotozoom(image, 0, 2)
wall = pygame.image.load("wall.png")
wall_image2 = pygame.transform.flip(wall,True,True)
wall_image = pygame.transform.flip(wall,False,True)
zombie_speed = 1
current_zombie_count = 0
max_zombie_count = 0
enemy_group = pygame.sprite.Group()

class Player:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def rotate_player(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) - 85
        player_image_copy = pygame.transform.rotate(player_image, angle)
        display.blit(player_image_copy, (self.x+7-int(player_image_copy.get_width()/2), self.y+14-int(player_image_copy.get_height()/2)))

    def main(self, display):
        self.rotate_player(display)
        #display.blit(player_image_copy, (self.x, self.y))

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 = 20
        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
        circlepoint = self.x-int(self.x_vel)+8, self.y-int(self.y_vel)+12


    def main(self, display):
        self.x -= int(self.x_vel)
        self.y -= int(self.y_vel)
        pygame.draw.circle(display, (255,0,0), (self.x+8, self.y+12), 15)
        global circlepoint
        circlepoint = self.x+8, self.y+12

class Zombie(pygame.sprite.Sprite):
    def __init__(self,playerbullet, x, y, speed):
        self.x = x
        self.y = y
        self.speed = zombie_speed
        #zombie_rect = pygame.Rect(self.x-10, self.y-10, 58, 56)
        self.image = pygame.image.load("Zombie.png").convert()
        self.rect = self.image.get_rect

    def update(self):
        self.y += 1






    def main(self, display):
        self.update()

        display.blit(zombie_image_copy, (self.x, self.y))
        zombie_rect = pygame.Rect(self.x-10, self.y-10, 58, 56)





f = 750
g = 500

player_bullets = []
list_zombies = []


while True:

    display.fill((24,164,86))
    display.blit(wall_image2, (0,950))
    display.blit(wall_image, (969, 950))


    display_rect = pygame.Rect(0,0,1500,950)

    player = Player(f, g, 100, 50)

    point1 = (f-30, g)
    point2 = (f, g-35)
    point3 = (f+35, g)
    point4 = (f, g+35)




    mouse_x, mouse_y = pygame.mouse.get_pos()

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

            pygame.display.quit()
            pygame.QUIT
            sys.exit()

        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()

    pygame.draw.rect(display, (255,255,255), (44, 44, 16, 16))

    if keys[pygame.K_a]:
        if pygame.Rect.collidepoint(display_rect,point1):
            f -= 5


    if keys[pygame.K_d]:
        if pygame.Rect.collidepoint(display_rect,point3):
            f += 5


    if keys[pygame.K_w]:
        if pygame.Rect.collidepoint(display_rect,point2):
            g -= 5


    if keys[pygame.K_s]:
        if pygame.Rect.collidepoint(display_rect,point4):
            g += 5



    if list_zombies == []:
        max_zombie_count += 1
        zombie_speed += 0.5
        while current_zombie_count < max_zombie_count:
            spawnpoint_x = random.randint(50,1400)
            spawnpoint_y = 0
            list_zombies.append(Zombie(spawnpoint_x, spawnpoint_y, zombie_speed,player_bullets ))
            current_zombie_count += 1

    player.main(display)


    for bullet in player_bullets:
        bullet.main(display)

    for zombie in list_zombies:
        zombie.main(display)

   

    clock.tick(60)
    pygame.display.update()
Roni
  • 597
  • 5
  • 21
  • Why can't you use the variables in the class? There must be a list of zombies. Every time a bullet moves, you check its location against the bounding region of each zombie in turn. – Tim Roberts Jan 27 '22 at 23:18
  • So each time I shoot I check if the bullet hits any zombie in zombie list right? – Eric Blacker Jan 28 '22 at 11:20
  • Yes, every time a bullet moves, you need to compare its position against every zombie. There's no "automatic" method of checking them all at once. – Tim Roberts Jan 29 '22 at 00:28

0 Answers0