bob
bob

Reputation: 47

How to get enemy to appear?

When the game first starts both the player and the enemy appear but when the player moves the enemy disappears.

import pygame
import os 
import random 
from pygame.locals import * # Constants
import math
import sys
import random

pygame.init()  

screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))     # fill the background white 
White = (255,255,255)

screen.blit(background, (0, 0))

class Player(pygame.sprite.Sprite):

def __init__(self):

    self.rect = pygame.draw.rect(screen, (0,0,128), (50,560,50,25)) #(colour)(x-position,y-position,width,height)
    self.dist = 100

def draw_rect(self,x,y):    # This is my code which should make the player move
    screen.blit(background, (0, 0)) #If this isn't included then when the rectangle moves it's old positon will still be on the screen
    self.rect = self.rect.move(x*self.dist, y*self.dist); pygame.draw.rect(screen, (0, 0, 128), self.rect)
    pygame.display.update()

def handle_keys(self): # code to make the character move when the arrow keys are pressed
    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        self.draw_rect(-0.05,0)
    elif keys[K_RIGHT]:
        self.draw_rect(0.05,0)
    elif keys[K_UP]:
        self.draw_rect(0,-0.05)
    elif keys[K_DOWN]:
        self.draw_rect(0,0.05)
    elif keys[K_SPACE]:
        self.draw_rect(0.4,-0.9)
    if self.rect.right > 1280:   # These are to stop the player from going out of the boundary
        self.rect.right = 1280
    if self.rect.left < 0:
        self.rect.left = 0
    if self.rect.bottom > 720:
        self.rect.bottom = 720
    if self.rect.top < 0:
        self.rect.top = 0

The movement of the player works perfectly fine.

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
def __init__(self):
    x = random.randint(50,450)
    self.rect = pygame.draw.rect(screen, (128,0,0), (300,x,50,25))

The enemy just spawns at a random place and should still appear when the player moves around. I tried to do something but it didn't work.

clock = pygame.time.Clock()  # A clock to limit the frame rate.
player = Player()
enemy = Enemy()

def main():  #my main loop 
    running = True
    while running:
        player.handle_keys()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        clock.tick(60)  # Limit the frame rate to 60 FPS.
        pygame.display.flip()   #updates the whole screen

    if __name__ == '__main__': main()

Upvotes: 1

Views: 91

Answers (1)

sloth
sloth

Reputation: 101062

It's because you draw the Rect of the enemy only once: in the __init__ function of Enemy. Also, you do a lot of drawing in different places of the Player class, like clearing the screen. Stop doing that.

You already subclass Sprite, so use this class as intended. Sprites have an image and rect that defines how they look like and where they are, and you add them to a Group that takes care of calling their update function and drawing them to the screen.

Your code should look like this:

import pygame
import random 
from pygame.locals import * # Constants

pygame.init()  

screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))     # fill the background white 

class Player(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((50,25))
        self.image.fill((0,0,128))
        self.rect = self.image.get_rect(topleft=(50,560))
        self.dist = 100

    def update(self): # code to make the character move when the arrow keys are pressed

        keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            self.rect.move_ip(-1,0)
        elif keys[K_RIGHT]:
            self.rect.move_ip(1,0)
        elif keys[K_UP]:
            self.rect.move_ip(0,-1)
        elif keys[K_DOWN]:
            self.rect.move_ip(0,1)
        self.rect.clamp_ip(screen_rect)

class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
    def __init__(self):
        super().__init__()
        x = random.randint(50,450)
        self.image = pygame.Surface((50,25))
        self.image.fill((128,0,0))
        self.rect = self.image.get_rect(topleft=(300, x))

clock = pygame.time.Clock()  # A clock to limit the frame rate.
player = Player()
enemy = Enemy()
sprites = pygame.sprite.Group(player, enemy)

def main():  #my main loop 
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        sprites.update()
        screen.blit(background, (0, 0))
        sprites.draw(screen)
        clock.tick(60)  # Limit the frame rate to 60 FPS.
        pygame.display.flip()   #updates the whole screen

if __name__ == '__main__': 
    main()    

Upvotes: 1

Related Questions