0
\$\begingroup\$

I have blitted 4 zombies to the screen in pygame. First, the zombies will wander in a specific region (let us say a 10 x 10 area). And when the player enters any one of the zombie's areas, the zombie should target the player.

import pygame, random, os
from pygame.locals import *

pygame.init()

scr_width = 1020
scr_height = 510
screen = pygame.display.set_mode((scr_width, scr_height))

clock = pygame.time.Clock()

images = {}
path = 'Desktop/Files/Dungeon Minigame/'
filenames = [f for f in os.listdir(path) if f.endswith('.png')]
for name in filenames:
    imagename = os.path.splitext(name)[0]
    images[imagename] = pygame.image.load(os.path.join(path, name))

pygame.display.set_caption('Dungeon Minigame')

font = pygame.font.SysFont('Times_New_Roman', 27)

white = [240, 240, 240]

fps = 60
lives = 3
score = 0

playerX = 510
playerY = 220
playerxchange = 0
playerychange = 0

def player(x, y):
    screen.blit(images['r_knight'], (playerX, playerY))

class Enemy:
    def __init__(self):
        self.x = random.randint(8, 800)
        self.y = random.randint(8, 440)
        self.moveX = 0
        self.moveY = 0

    def move(self):
        self.speed = 3
    def draw(self):
        screen.blit(images['r_zombie'], (self.x, self.y))

def enemy(x, y):
    screen.blit(images['r_zombie'], (x, y))

enemy_list = []
for i in range(4):
    new_enemy = Enemy()
    enemy_list.append(new_enemy)

while True:

    screen.blit(images['background'], (0, 0))

    score_text = font.render('Score: ' + str(score), True, white)
    lives_text = font.render('Lives: ', True, white)

    screen.blit(score_text, (20, 20))
    screen.blit(lives_text, (840, 20))

    onpress = pygame.key.get_pressed()

    screen.blit(images['r_knight'], (playerX, playerY))

    if lives == 3:
        screen.blit(images['triple_heart'], (920, 0))

    if lives == 2:
        screen.blit(images['double_heart'], (920, 0))

    if lives == 1:
        screen.blit(images['single_heart'], (920, 0))

    if lives <= 0:
        screen.blit(images['triple_empty_heart'], (920, 0))
        if lives < 0:
            lives = 0

    if onpress[pygame.K_a]:
        playerX -= 3
        screen.blit(images['l_knight'], (playerX, playerY))

    if onpress[pygame.K_w]:
        playerY -= 3
        screen.blit(images['l_knight'], (playerX, playerY))

    if onpress[pygame.K_d]:
        playerX += 3
        screen.blit(images['r_knight'], (playerX, playerY))

    if onpress[pygame.K_s]:
        playerY += 3
        screen.blit(images['r_knight'], (playerX, playerY))

    if onpress[pygame.K_w] and onpress[pygame.K_a]:
        screen.blit(images['l_knight'], (playerX, playerY))

    if onpress[pygame.K_s] and onpress[pygame.K_a]:
        screen.blit(images['l_knight'], (playerX, playerY))

    if onpress[pygame.K_w] and onpress[pygame.K_d]:
        screen.blit(images['r_knight'], (playerX, playerY))

    if onpress[pygame.K_s] and onpress[pygame.K_d]:
        screen.blit(images['r_knight'], (playerX, playerY))

    if playerX <= -8:
        playerX = -8
    elif playerX >= 965:
        playerX = 965

    if playerY <= 5:
        playerY = 5
    elif playerY >= 440:
        playerY = 440
    
    for enemy in enemy_list:
        enemy.move()

    for enemy in enemy_list:
        enemy.draw()

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

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

Output

\$\endgroup\$
8
  • \$\begingroup\$ Are you asking on how you should code this, or what should the logic be? \$\endgroup\$
    – Vaillancourt
    Apr 21 '21 at 13:37
  • \$\begingroup\$ @Vaillancourt How to code it. \$\endgroup\$
    – Lokes
    Apr 21 '21 at 13:53
  • \$\begingroup\$ What have you tried? \$\endgroup\$
    – user35344
    Apr 21 '21 at 15:41
  • \$\begingroup\$ I have tried using a range of x and y values but it teleported all of the zombies to that range and it didn't work with their respective locations. \$\endgroup\$
    – Lokes
    Apr 21 '21 at 15:48
  • 2
    \$\begingroup\$ Well then maybe that should be in your question, rather than asking us to recode some of what you already did. \$\endgroup\$
    – Vaillancourt
    Apr 21 '21 at 16:53

You must log in to answer this question.

Browse other questions tagged .