I have created an enemy in my game, that is meant to move up and down the y-axis. However he does not appear

Viewed 29

I am making a game for my school project where you just go up and down shooting at enemies. I have made the enemy however he is not appearing on the screen. There are no errors and everything else works perfectly fine, the enemy is also meant to go up and down the y-axis, so only up and down. Here is my code:

import pygame, sys

# classes
# Player class
class Player(pygame.sprite.Sprite):
    def __init__(self, image):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect(center = (screen_width//2, screen_height//2))

    def update(self):
        self.rect.center = pygame.mouse.get_pos()

    def create_bullet(self):
        return Bullet(*pygame.mouse.get_pos())
# bullet class
class Bullet (pygame.sprite.Sprite):
    def __init__(self,pos_x,pos_y):
        super().__init__()
        self.image = pygame.Surface((50,10))
        self.image.fill((255,255,0))
        self.rect = self.image.get_rect(center = (pos_x,pos_y))

    def update(self):
        self.rect.x += 5
# enemy class
class enemy(pygame.sprite.Sprite):
    walkup = pygame.image.load("enemy.png")
    walkdown = pygame.image.load("enemy.png")
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.path = [self.y, self.end]
        self.walkcount = 0
        self.vel = 3

    def draw(self,win):
        self.move()
        if self.walkcount + 1 <= 33:
            self.walkcount = 0
        if self.vel > 0:
            win.blit(self.walkup[self.walkcount //3], (self.x, self.y))
            self.walkcount += 1
        else:
            win.blit(self.walkdown [self.walkcount //3], (self.x, self.y) )
            self.walkcount += 1


    def move(self):
        if self.vel >0:
            if self.x + self.vel < self.path[1]:
                self.x += self.vel
            else:
                self.vel - self.vel * -1
                self.walkcount - 0
        else:
            if self.y - self.vel > self.path[0]:
                    self.x += self.vel
            else:
                    self.vel = self.vel * -1
                    self.walkcount = 0
# making the enemy appear 
def redrawGameWindow():
    thief.draw(screen)

thief = enemy(100, 410, 64, 64, 450)




# general setup
pygame.init()
clock = pygame.time.Clock()

# game screen
screen_width = 1920
screen_height = 1080
screen = pygame .display.set_mode((screen_width, screen_height))
background = pygame.image.load("BackGround.png")

# player
player_image = pygame.image.load("Charachter2.png")
player = Player(player_image)
player_group = pygame.sprite.Group()
player_group.add(player)

# Bullet
bullet_group = pygame.sprite.Group()

# caption
pygame.display.set_caption("Wild-West Shooter")

# makes game quit
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame:quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullet_group.add(player.create_bullet())

    player_group.update()
    bullet_group.update()

    screen.blit(background,(0,0))
    player_group.draw(screen)
    bullet_group.draw(screen)
    pygame.display.flip()
    clock.tick(120)

2 Answers

You can draw the thief in the while loop by adding thief.draw(screen) like so:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame:quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullet_group.add(player.create_bullet())

    player_group.update()
    bullet_group.update()

    screen.blit(background,(0,0))
    player_group.draw(screen)
    bullet_group.draw(screen)
    thief.draw(screen)
    pygame.display.flip()
    clock.tick(120)

Then try removing the array-notation in your Enemy class draw method. The code tries to use array-notation when calling blit with: win.blit(self.walkup[self.walkcount //3], (self.x, self.y)) so use something like this instead:

   def draw(self,win):
        self.move()
        if self.walkcount + 1 <= 33:
            self.walkcount = 0
        if self.vel > 0:
            win.blit(self.walkup, (self.x, self.y))
            self.walkcount += 1
        else:
            win.blit(self.walkdown, (self.x, self.y) )
            self.walkcount += 1

The redrawGameWindow function isn't called.

The while loop qhould look like that:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame:quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullet_group.add(player.create_bullet())

    player_group.update()
    bullet_group.update()

    # draw:
    screen.blit(background,(0,0)) # background
    player_group.draw(screen) # player
    bullet_group.draw(screen) # bullets
    redrawGameWindow() # enemy

    pygame.display.flip()
    clock.tick(120)

(Off-topic) You should also rename the redrawGameWindow function (it doesn't really redraw the window, does it?), or even delete it (a function containing only one line of code should be removed and its content is called instead)

Also, if you want the redrawGameWindow to actually redraw the window, you can put all of the code which draws onto the screen in this function like that:

..........


def redrawGameWindow(): # to call this function will redraw...
    player_group.update()
    bullet_group.update()

    screen.blit(background,(0,0)) # the background,
    player_group.draw(screen) # the player,
    bullet_group.draw(screen) # the bullets
    thief.draw(screen) # and finally the enemy
    pygame.display.flip()

thief = enemy(100, 410, 64, 64, 450)

# general setup
pygame.init()
clock = pygame.time.Clock()

..........

# caption
pygame.display.set_caption("Wild-West Shooter")

# makes game quit
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame:quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            bullet_group.add(player.create_bullet())

    redrawGameWindow() # redraw all
    clock.tick(120)
Related