0

Like the title said i cant figure out how to make my player die when an enemy touches him. I've looked it up but i still cant figure this one out, any ideas?

skip this next part!

Wanted me to put more stuff in here and apparently not get to the point. ;ulahtluihliukhrefluijaheuflhawuleifhuahfuljhyuileawhyruihyauiljwyruihaui

Here is my code:

import pygame, math

pygame.init()

SCREEN_WIDTH = 750
SCREEN_HEIGHT = 500
SCREEN_COLOR = (160, 160, 160)

START_BUTTON_SIZE = .5
START_BUTTON_X = 280
START_BUTTON_Y = 320

EXIT_BUTTON_SIZE = .1
EXIT_BUTTON_X = 695
EXIT_BUTTON_Y = 5

player_x = 450
player_y = 250
player_width = 35
player_height = 40
player_vel = 4
player_color = (1, 40, 100)

Enemy_width = 30
Enemy_height = 30
enemy_positions = [[0, 0], [750, 500], [0, 500], [750, 0], [0, 250], [750, 250], [375, 0], [375, 500]]
Enemy_Vel = 2

FPS = 1000

Mouse_Pos = 0

Start = False

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

Start_Menu = pygame.image.load('Start_Menu.bmp').convert_alpha()
Start_Menu = pygame.transform.scale(Start_Menu, (SCREEN_WIDTH, SCREEN_HEIGHT))

Background = pygame.image.load('Background.bmp').convert_alpha()
Background = pygame.transform.scale(Background, (SCREEN_WIDTH, SCREEN_HEIGHT+10))

pygame.display.set_caption('Outlaw')

Enemy = pygame.transform.smoothscale(pygame.image.load("Start_Enemy.bmp").convert_alpha(), (Enemy_width, Enemy_height))

player = pygame.transform.smoothscale(pygame.image.load("Player.bmp").convert_alpha(), (player_width, player_height))

start_image = pygame.image.load('Start_btn.bmp').convert_alpha()
exit_image = pygame.image.load('Exit_btn.bmp').convert_alpha()

correction_angle = 90




class Button():
    
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.clicked = False
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)

    
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.clicked = False
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        
    def draw(self):
        action = False
        
        pos = pygame.mouse.get_pos()
        
        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True
                
            if pygame.mouse.get_pressed()[0] == 0:
                self.clicked = False
                
        
        screen.blit(self.image,(self.rect.x, self.rect.y))
        
        return action
    
clock = pygame.time.Clock()
    
start_button = Button(START_BUTTON_X, START_BUTTON_Y, start_image, START_BUTTON_SIZE)
exit_button = Button(EXIT_BUTTON_X, EXIT_BUTTON_Y, exit_image, EXIT_BUTTON_SIZE)

run = True
while run:
    
    clock.tick(FPS)
    
    
    
    
    if Start == False:
        screen.fill((SCREEN_COLOR))
    
        screen.blit(Start_Menu,(0, 0))
    
    if start_button.draw() == True and Start == False:
        print('START')
        Start = True
        player_x = 225
        player_y = 400
        
        #where you draw stuff
    if Start == True:
        screen.fill((SCREEN_COLOR))
        screen.blit(Background,(0, 0))
        
        player_pos  = player_x, player_y
        player_rect = player.get_rect(center = player_pos)

        mx, my = pygame.mouse.get_pos()
        dx, dy = mx - player_rect.centerx, my - player_rect.centery
        angle = math.degrees(math.atan2(-dy, dx)) - correction_angle

        rot_image      = pygame.transform.rotate(player, angle)
        rot_image_rect = rot_image.get_rect(center = player_rect.center)
        
        screen.blit(rot_image, rot_image_rect.topleft)

        
        for enemy_pos in enemy_positions:
            enemy_rect = Enemy.get_rect(center = enemy_pos)
            dx, dy = player_x - enemy_rect.centerx, player_y - enemy_rect.centery
            angle = math.degrees(math.atan2(-dy, dx)) - correction_angle
            rot_image      = pygame.transform.rotate(Enemy, angle)
            rot_image_rect = rot_image.get_rect(center = enemy_rect.center)
            screen.blit(rot_image, rot_image_rect.topleft)

        pygame.display.flip()
    
    
    for enemy_pos in enemy_positions:
        if enemy_pos[0] < player_x:
            enemy_pos[0] += Enemy_Vel
        if enemy_pos[0] > player_x:
            enemy_pos[0] -= Enemy_Vel
        if enemy_pos[1] < player_y:
            enemy_pos[1] += Enemy_Vel
        if enemy_pos[1] > player_y:
            enemy_pos[1] -= Enemy_Vel
            
    keys = pygame.key.get_pressed()
      
    # if left arrow key is pressed
    if keys[pygame.K_a] and player_x>0:
          
        # decrement in x co-ordinate
        player_x -= player_vel
          
    # if left arrow key is pressed
    if keys[pygame.K_d] and player_x<SCREEN_WIDTH:
          
        # increment in x co-ordinate
        player_x += player_vel
         
    # if left arrow key is pressed   
    if keys[pygame.K_w] and player_y>0:        
        # decrement in y co-ordinate
        player_y -= player_vel
          
    # if left arrow key is pressed   
    if keys[pygame.K_s] and player_y<SCREEN_HEIGHT:
        # increment in y co-ordinate
        player_y += player_vel
        
        
        
    if exit_button.draw() == True:
        print('EXIT')
        run = False
    
    
    pygame.display.update()
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            run = False
          

    
pygame.quit()
  • please reduce your code be providing [a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – coder Jun 22 '22 at 21:48
  • i dont know what to reduce, im new to pygame stuff so i dont know what will mess this up. – LegoSuperBall Jun 22 '22 at 21:49
  • In your question, take away bits of code that we do not need to see (code that is irrelevant) – coder Jun 23 '22 at 00:59

1 Answers1

1

See How do I detect collision in pygame?. Use pygame.Rect.colliderect to detect the collision between an enemy and a player rectangle. remove the enemy from the list when it hits the player (see How to remove items from a list while iterating?):

while run:
     # [...] your code

     player_rect = player.get_rect(center = player_pos) 
     for enemy_pos in enemy_positions[:]:
         enemy_rect = Enemy.get_rect(center = enemy_pos)
        
         if player_rect.colliderect(enemy_rect): 
             print('hit')
             enemy_positions.remove(enemy_pos)

    # [...] your code
Rabbid76
  • 202,892
  • 27
  • 131
  • 174