-1

I have absolutely no idea how to go about this, I'm planning on just making the sword a square that when space is pressed the sprite will show and if the enemy is touching it, it will die. any recommendations?

import pygame
import random
import math

user_width = 20
user_height = 20
enemy_width = 20
enemy_height = 20
enemy_x = 0
enemy_y = 0
x = 250
y = 250
vel = 150
enemy_vel = 1
fps = 120
player = pygame.image.load('sprite.png')
player = pygame.transform.scale(player, (user_width, user_height))
enemy = pygame.image.load('enemy.png')
enemy = pygame.transform.scale(enemy, (enemy_width, enemy_height))

top_left_corner = False
top_right_corner = False
bottom_left_corner = False
bottom_right_corner = False
player_middle_x = (x + (user_width/2))
player_middle_y = (y + (user_height/2))
player_middle = player_middle_x, player_middle_y
enemy_middle_x = (enemy_x + (enemy_width/2))
enemy_middle_y = (enemy_y + (enemy_height/2))
enemy_middle = enemy_middle_x, enemy_middle_y
print(enemy_middle)
user_left = x
user_right = x + user_width
user_top = y
user_bottom = y + user_height
enemy_left = enemy_x
enemy_right = enemy_x + enemy_width
enemy_top = enemy_y
enemy_bottom = enemy_y + enemy_height
window = pygame.display.set_mode((500, 500))
w = True
a = True
s = True
d = True
agro = False
window.fill(0)
quit = False
FPS_clock = pygame.time.Clock()

#updates user location and enemy location
def update_location():
    global user_right
    global user_left
    global user_top
    global user_bottom
    global enemy_top
    global enemy_right
    global enemy_left
    global enemy_bottom
    global player_middle_x
    global player_middle_y
    global player_middle
    global enemy_middle_x
    global enemy_middle_y
    global enemy_middle
#==================================================user
    user_left = x
    user_right = x + user_width
    user_top = y
    user_bottom = y + user_height
    player_middle_x = (x + (user_width / 2))
    player_middle_y = (y + (user_height / 2))
    player_middle = player_middle_x, player_middle_y
#================================================enemy
    enemy_left = enemy_x
    enemy_right = enemy_x + enemy_width
    enemy_top = enemy_y
    enemy_bottom = enemy_y + enemy_height
    enemy_middle_x = (enemy_x + (enemy_width / 2))
    enemy_middle_y = (enemy_y + (enemy_height / 2))
    enemy_middle = enemy_middle_x, enemy_middle_y
#==========================================update
    window.blit(player, (x, y))
    window.blit(enemy, (enemy_x, enemy_y))

def death():
    global quit
    if (enemy_middle_y + 20) >= player_middle_y and (enemy_middle_y - 20) <= player_middle_y and\
            (enemy_middle_x + 20) >= player_middle_x and (enemy_middle_x - 20) <= player_middle_x:
        quit = True

#when you touch the edge of the window
def boundary_collide():
    global top_left_corner
    global top_right_corner
    global bottom_right_corner
    global bottom_left_corner
    global w
    global a
    global s
    global d
    if user_top <= 0:
        if user_left <= 0 :
            top_left_corner = True
        else:
            top_left_corner = False
        if user_right >= 500:
            top_right_corner = True
        else:
            top_right_corner = False
        w = False
        if not keys[pygame.K_w]:
            w = True
        elif keys[pygame.K_a] and not top_left_corner:
            a = True
        elif keys[pygame.K_d] and not top_right_corner:
            d = True
    else:
        w = True


    if user_left <= 0:
        if user_top <= 0:
            top_left_corner = True
        else:
            top_left_corner = False
        if user_bottom >= 500:
            bottom_left_corner = True
        else:
            bottom_left_corner = False
        a = False
        if not keys[pygame.K_a]:
            a = True
        elif keys[pygame.K_w] and not top_left_corner:
            w = True
        elif keys[pygame.K_s] and not bottom_left_corner:
            s = True
    else:
        a = True

    if user_bottom >= 500:
        if user_left <= 0 :
            bottom_left_corner = True
        else:
            bottom_left_corner = False
        if user_right >= 500:
            bottom_right_corner = True
        else:
            bottom_right_corner = False
        s = False
        if not keys[pygame.K_s]:
            s = True
        elif keys[pygame.K_a] and not bottom_left_corner:
            a = True
        elif keys[pygame.K_d] and not bottom_right_corner:
            d = True
    else:
        s = True

    if user_right >= 500:
        if user_top <= 0:
            top_right_corner = True
        else:
            top_right_corner = False
        if user_bottom >= 500:
            bottom_right_corner = True
        else:
            bottom_right_corner = False
        d = False
        if not keys[pygame.K_d]:
            d = True
        elif keys[pygame.K_w] and not top_right_corner:
            w = True
        elif keys[pygame.K_s] and not bottom_right_corner:
            s = True
    else:
        d = True

def enemy_movement():
    global enemy_x
    global enemy_y
    global agro
    if agro == True:
        dx, dy = player_middle_x - enemy_middle_x, player_middle_y - enemy_middle_y
        dist = math.hypot(dx, dy)
        dx, dy = dx / dist, dy / dist  # Normalize.
        # Move along this normalized vector towards the player at current speed.
        enemy_x += dx * enemy_vel
        enemy_y += dy * enemy_vel


#movement
def movement():
    global keys
    global x
    global y
    boundary_collide()
    keys = pygame.key.get_pressed()
    if w or a or s or d:
        if keys[pygame.K_w] and w:
            y -= vel * dt
        if keys[pygame.K_a] and a:
            x -= vel * dt
        if keys[pygame.K_s] and s:
            y += vel * dt
        if keys[pygame.K_d] and d:
            x += vel * dt

"""
=====================|MAIN GAME LOOP|===============================
"""
while not quit:
    dt = FPS_clock.tick(fps) / 1000
    FPS_clock.tick(fps)
    for event in pygame.event.get():
        keys = pygame.key.get_pressed()
        if event.type == pygame.QUIT or keys[pygame.K_ESCAPE]:
            quit = True
    if (enemy_middle_y + 200) >= player_middle_y and (enemy_middle_y - 200) <= player_middle_y and\
            (enemy_middle_x + 200) >= player_middle_x and (enemy_middle_x - 200) <= player_middle_x:
        agro = True
    else:
        agro = False
    enemy_movement()
    movement()
    window.fill(0)
    update_location()
    death()
    pygame.display.update()

i have not tried anything as i have no idea on any way to go about this xd (if theres a rile about having to try things first then please excuse my stupidness xd)

jmarkmurphy
  • 11,030
  • 31
  • 59

1 Answers1

-1

Make it so when you press the space bar to swing the sword, play the swinging animation, and make it so when an enemy collides with the player's hitbox it dies. Don't bother creating a new hitbox for the sword. To get a hitbox you can do:

player_hitbox = player.get_rect()
enemy_hitbox = enemy.get_rect()

Then you can use pygame's colliderect function to see if the collide then kill the enemy (or the player if their not swinging their sword)

if pygame.Rect.colliderect(player_hitbox, enemy_hitbox):
    (kill the enemy)
Jml_
  • 1
  • 1
  • This does not work as [`get_rect`](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.get_rect) returns a rectangle that always starts at (0, 0). – Rabbid76 Aug 27 '23 at 09:45