Rectangle disappearing when off the screen Pygame

I was trying to create a simple game but i’ve came to a problem once i added ennemies. The ennemies disappeared once they get off the screen and when i come back..Once i start the game and after I came back.

So, i tried to use the debugger and i realised that it was the height of the ennemy (red rectangle) that was changing from 20 to 0. However i dont know why it does that beacuse never in my code im changing the height of this rectangle.

I’ve recreated a small program that shows the issue:

import pygame
WIDTH, HEIGHT = 800, 600
PLAYER_WIDTH, PLAYER_HEIGHT = 10, 20
FPS = 60
VELOCITY = 1
GRAVITY = 1
PLAYER_SPEED = 5
ENEMY_SPEED = 1
JUMPING_HEIGHT = 15
window = pygame.display.set_mode((WIDTH, HEIGHT))
class Player:
    def __init__(self, platforms, enemy):
        self.player_pos_x = 200
        self.player_pos_y = 350
        self.draw_player()
        self.platforms = platforms
        self.jumping = False
        self.velocity = 0
        self.falling = False
        self.enemy = enemy
    def gravity(self):
        if self.jumping:
            self.jump() 

        if self.falling:
            self.fall()

        for platform in self.platforms:
            if self.player.y > platform.y + platform.height and self.player.y < platform.y + platform.height + 20:
                if self.player_pos_x + self.player.width >= platform.x and self.player_pos_x <= platform.x + platform.width:
                    self.jumping = False
                    self.falling = False
                    self.fall()
            elif self.player_pos_y + self.player.height > platform.y and self.player_pos_y < platform.y:
                if self.player_pos_x + self.player.width >= platform.x and self.player_pos_x <= platform.x + platform.width:
                    self.player_pos_y = platform.y - self.player.height
                    self.jumping = False
                    self.falling = False
                    self.velocity = JUMPING_HEIGHT
                    break

            elif self.player.bottom <= platform.y - 1:
                if not self.jumping and not self.falling:
                    self.fall()
            
    def jump(self):
        if not self.jumping:
            self.jumping = True
            self.velocity = JUMPING_HEIGHT

        else:
            self.player_pos_y -= self.velocity
            self.velocity -= GRAVITY
            if self.velocity < -JUMPING_HEIGHT:
                self.jumping = False
            
    def fall(self):
        if not self.falling:
            if self.velocity > 0:
                self.velocity = 0
            self.falling = True

        else:
            self.player_pos_y -= self.velocity
            self.velocity -= GRAVITY
            if self.velocity < -99999:
                self.falling = False
                self.velocity = JUMPING_HEIGHT

    def draw_player(self):
        self.player = pygame.draw.rect(window, (0, 0, 255), (self.player_pos_x, self.player_pos_y, PLAYER_WIDTH, PLAYER_HEIGHT))

    def right(self):
        for platform in self.platforms:
            if (self.player.y > platform.y and self.player.y < platform.bottom) or (self.player.bottom < platform.bottom and self.player.bottom > platform.y):
                if self.player.right > platform.x - PLAYER_SPEED and self.player.right < platform.right - PLAYER_SPEED:
                    return
        
        self.enemy.enemy_pos_x -= PLAYER_SPEED
        for platform in self.platforms:
            platform.x -= PLAYER_SPEED
    
    def left(self):
        for platform in self.platforms:
            if (self.player.y > platform.y and self.player.y < platform.bottom) or (self.player.bottom < platform.bottom and self.player.bottom > platform.y):
                if self.player.x < platform.right + PLAYER_SPEED and self.player.x > platform.x + PLAYER_SPEED:
                    return
        
        self.enemy.enemy_pos_x += PLAYER_SPEED
        for platform in self.platforms:
            platform.x += PLAYER_SPEED

    def player_movement(self):
        self.gravity()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_d]:
            self.right()
        if keys[pygame.K_q]:
            self.left()
        if keys[pygame.K_SPACE]:
            if not self.jumping and not self.falling:
                self.jump()

class Enemy:
    def __init__(self, x, y, platforms):
        self.enemy_pos_x = x
        self.enemy_pos_y = y
        self.draw_enemy()
        self.platforms = platforms
        self.falling = False
        self.velocity = 0

        self.Times = 0

        #Direction: Positive = Right ; Negative = Left
        self.direction = ENEMY_SPEED
    def draw_enemy(self):
        self.enemy = pygame.draw.rect(window, (255, 0, 0), (self.enemy_pos_x, self.enemy_pos_y, PLAYER_WIDTH, PLAYER_HEIGHT))
        if self.enemy_pos_x <= 0:
            return
        if self.enemy.h == 0:
            self.enemy.h = 25
            self.enemy.y = 100

    def gravity(self):
        if self.falling:
            self.fall()
        platform_y = 9999

        for platform in self.platforms:
            if (self.enemy.right > platform.x and self.enemy.right < platform.right) or (self.enemy.x < platform.right and self.enemy.x > platform.x):
                if self.enemy.y < platform.y and platform.y < platform_y:
                    platform_y = platform.y

        for platform in self.platforms:
            if self.enemy.y > platform.y + platform.height and self.enemy.y < platform.y + platform.height + 20:
                if self.enemy_pos_x + self.enemy.width >= platform.x and self.enemy_pos_x <= platform.x + platform.width:
                    self.falling = False
                    self.fall()
            elif self.enemy_pos_y + self.enemy.height > platform.y and self.enemy_pos_y < platform.y:
                if self.enemy_pos_x + self.enemy.width >= platform.x and self.enemy_pos_x <= platform.x + platform.width:
                    self.enemy_pos_y = platform_y - self.enemy.height
                    self.falling = False
                    self.velocity = JUMPING_HEIGHT
                    break

            elif self.enemy.bottom < platform_y - 1:
                if not self.falling:
                    self.fall()
    def fall(self):
        if not self.falling:
            if self.velocity > 0:
                self.velocity = 0
            self.falling = True

        else:
            self.enemy_pos_y -= self.velocity
            self.velocity -= GRAVITY
            if self.velocity < -99999:
                self.falling = False
                self.velocity = JUMPING_HEIGHT
    def enemy_movement(self):
        self.gravity()
        self.moving()
    def moving(self):
        for platform in self.platforms:
            if self.direction > 0:
                if (self.enemy.y > platform.y and self.enemy.y < platform.bottom) or (self.enemy.bottom < platform.bottom and self.enemy.bottom > platform.y):
                    if self.enemy.right > platform.x - ENEMY_SPEED and self.enemy.right < platform.right - ENEMY_SPEED:
                        self.direction *= -1
            else:
                if (self.enemy.y > platform.y and self.enemy.y < platform.bottom) or (self.enemy.bottom < platform.bottom and self.enemy.bottom > platform.y):
                    if self.enemy.x < platform.right + ENEMY_SPEED and self.enemy.x > platform.x + ENEMY_SPEED:
                        self.direction *= -1

        self.enemy_pos_x += self.direction

clock = pygame.time.Clock()
platforms = []
platforms.append(pygame.Rect(0, 400, 10000, 200))
platforms.append(pygame.Rect(100, 320, 100, 20))
platforms.append(pygame.Rect(100, 290, 10, 30))
platforms.append(pygame.Rect(190, 290, 10, 30))
def main():
    run = True
    enemy = Enemy(150, 300, platforms)
    player = Player(platforms, enemy)
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False  
        window.fill((20, 150, 200))
        player.draw_player()
        enemy.draw_enemy()
        player.player_movement()
        enemy.enemy_movement()
        for platform in platforms:
            pygame.draw.rect(window, (123, 123, 123), platform)
        pygame.display.update()
main()

Thank you in advance.

New contributor

NotABotIstg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật