Contenu | Rechercher | Menus

Annonce

DVD, clés USB et t-shirts Ubuntu-fr disponibles sur la boutique En Vente Libre

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 21/06/2022, à 06:43

chris7522

Est il possible de rendre un argument d'une classe python optionnel ?

Bonjour a toutes et a tous,
Est il possible de rendre un argument d'une classe python optionnel ? Voici mes 2 classes , la seconde héritant de la première .

class Ship:
    COOLDOWN = 30

    def __init__(self, x, y, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0

    def draw(self, window):
        window.blit(self.ship_img, (self.x, self.y))
        for laser in self.lasers:
            laser.draw(window)

    def move_lasers(self, vel, obj):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(HEIGHT):
                self.lasers.remove(laser)
            elif laser.collision(obj):
                obj.health -= 5
                self.lasers.remove(laser)

    def cooldown(self):
        if self.cool_down_counter >= self.COOLDOWN:
            self.cool_down_counter = 0
        elif self.cool_down_counter > 0:
            self.cool_down_counter += 1

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Laser(self.x, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1

    def get_width(self):
        return self.ship_img.get_width()

    def get_height(self):
        return self.ship_img.get_height()


class Player(Ship):
    def __init__(self,x ,y, health=100):
        super().__init__(x,y,health)
       
        self.ship_img = YELLOW_SPACE_SHIP
        self.laser_img = YELLOW_LASER
        self.mask = pygame.mask.from_surface(self.ship_img)
        self.max_health = health

    def move_lasers(self, vel, objs):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(WIDTH):
                self.lasers.remove(laser)
            else:
                for obj in objs:
                    if laser.collision(obj):
                        objs.remove(obj)
                        if laser in self.lasers:
                            self.lasers.remove(laser)

    def draw(self, window):
        super().draw(window)
        self.healthbar(window)
        
    def draw2(self, window):
        super().draw(window)

    def healthbar(self, window):
        pygame.draw.rect(window, (255,0,0), (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width(), 10))
        pygame.draw.rect(window, (0,255,0), (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width() * (self.health/self.max_health), 10))

Je souhaiterai instancier Player sans envoyer d'arguments : player = Player()
Est ce possible d'ecrire un truc de ce genre :

class Player(Ship):
    def __init__(self,x=None , y=None health=100):
        super().__init__(x,y,health)
        ...

Merci de votre aide !

Dernière modification par chris7522 (Le 21/06/2022, à 06:44)

Hors ligne

#2 Le 21/06/2022, à 08:08

MPython Alaplancha

Re : Est il possible de rendre un argument d'une classe python optionnel ?

Bonjour,

chris7522 a écrit :

Je souhaiterai instancier Player sans envoyer d'arguments : player = Player()
Est ce possible d'ecrire un truc de ce genre :

class Player(Ship):
    def __init__(self,x=None , y=None health=100):
        super().__init__(x,y,health)
        ...

Oui. Si tu ne fournis pas d'arguments à la classe Player lors de son ititialisation, les valeurs de x , y, health seront celles proposées par défaut. Soit x=None, y=None, health=100, arguments qui seront envoyés à la méthode __init__() de ta classe Ship via super().__init__(x,y,health)
...


# Là pour le plaisir de découvrir ...
# Envie d'apprendre Bash ou Python? Pensez aux MOOC ---> https://forum.ubuntu-fr.org/viewtopic.php?id=2030646

Hors ligne

#3 Le 21/06/2022, à 09:14

kholo

Re : Est il possible de rendre un argument d'une classe python optionnel ?

salut
def __init__(self,x=None , y=None, health=100):
il manque une virgule !

mais, pour le côté optionnel,
il y a aussi les * et ** (arguments non nommés ou nommés => dictionnaire)
... mais c'est plus compliqué à gérer sans bases en python ! wink

Hors ligne

#4 Le 21/06/2022, à 09:23

chris7522

Re : Est il possible de rendre un argument d'une classe python optionnel ?

ok , je te remercie pour ton aide .
Seulement , j'obtiens ce message d'erreur :

chris@chris:~/Bureau$ python3 Space5.py 
pygame 2.1.0 (SDL 2.0.16, Python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "Space5.py", line 279, in <module>
    main_menu()
  File "Space5.py", line 275, in main_menu
    main()
  File "Space5.py", line 203, in main
    redraw_window()	
  File "Space5.py", line 192, in redraw_window
    player2.draw2(WIN)
  File "Space5.py", line 121, in draw2
    super().draw(window)
  File "Space5.py", line 61, in draw
    window.blit(self.ship_img, (self.x, self.y))
TypeError: invalid destination position for blit
chris@chris:~/Bureau$ 

Avec mon code dans sa totalité :

import pygame
import os
import time
import random
pygame.font.init()

WIDTH, HEIGHT = 2000, 1000
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Shooter Tutorial")

# Load images
RED_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_red_small.png"))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_green_small.png"))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_blue_small.png"))

# Player player
YELLOW_SPACE_SHIP = pygame.image.load(os.path.join("assets", "pixel_ship_yellow.png"))

# Lasers
RED_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_red.png"))
GREEN_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_green.png"))
BLUE_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_blue.png"))
YELLOW_LASER = pygame.image.load(os.path.join("assets", "pixel_laser_yellow.png"))

# Background
BG = pygame.transform.scale(pygame.image.load(os.path.join("assets", "background-black.png")), (WIDTH, HEIGHT))

class Laser:
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.mask = pygame.mask.from_surface(self.img)

    def draw(self, window):
        window.blit(self.img, (self.x, self.y - 29))

    def move(self, vel):
        self.x += vel

    def off_screen(self, height):
        return not(self.x <= WIDTH and self.x >= 0)

    def collision(self, obj):
        return collide(self, obj)


class Ship:
    COOLDOWN = 30

    def __init__(self, x, y, health=100):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0

    def draw(self, window):
        window.blit(self.ship_img, (self.x, self.y))
        for laser in self.lasers:
            laser.draw(window)

    def move_lasers(self, vel, obj):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(HEIGHT):
                self.lasers.remove(laser)
            elif laser.collision(obj):
                obj.health -= 5
                self.lasers.remove(laser)

    def cooldown(self):
        if self.cool_down_counter >= self.COOLDOWN:
            self.cool_down_counter = 0
        elif self.cool_down_counter > 0:
            self.cool_down_counter += 1

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Laser(self.x, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1

    def get_width(self):
        return self.ship_img.get_width()

    def get_height(self):
        return self.ship_img.get_height()


class Player(Ship):
    def __init__(self,x=None, y=None, health=100):
        super().__init__(x,y,health)
       
        self.ship_img = YELLOW_SPACE_SHIP
        self.laser_img = YELLOW_LASER
        self.mask = pygame.mask.from_surface(self.ship_img)
        self.max_health = health

    def move_lasers(self, vel, objs):
        self.cooldown()
        for laser in self.lasers:
            laser.move(vel)
            if laser.off_screen(WIDTH):
                self.lasers.remove(laser)
            else:
                for obj in objs:
                    if laser.collision(obj):
                        objs.remove(obj)
                        if laser in self.lasers:
                            self.lasers.remove(laser)

    def draw(self, window):
        super().draw(window)
        self.healthbar(window)
        
    def draw2(self, window):
        super().draw(window)

    def healthbar(self, window):
        pygame.draw.rect(window, (255,0,0), (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width(), 10))
        pygame.draw.rect(window, (0,255,0), (self.x, self.y + self.ship_img.get_height() + 10, self.ship_img.get_width() * (self.health/self.max_health), 10))


class Enemy(Ship):
    COLOR_MAP = {
                "red": (RED_SPACE_SHIP, RED_LASER),
                "green": (GREEN_SPACE_SHIP, GREEN_LASER),
                "blue": (BLUE_SPACE_SHIP, BLUE_LASER)
                }

    def __init__(self, x, y, color, health=100):
        super().__init__(x, y, health)
        self.ship_img, self.laser_img = self.COLOR_MAP[color]
        self.mask = pygame.mask.from_surface(self.ship_img)

    def move(self, vel):
        self.x -= vel

    def shoot(self):
        if self.cool_down_counter == 0:
            laser = Laser(self.x-20, self.y, self.laser_img)
            self.lasers.append(laser)
            self.cool_down_counter = 1
            
#--------------------------------------------------

def collide(obj1, obj2):
    offset_x = obj2.x - obj1.x
    offset_y = obj2.y - obj1.y
    return obj1.mask.overlap(obj2.mask, (offset_x, offset_y)) != None

def main():
    run = True
    FPS = 60
    level = 0
    lives = 10
    main_font = pygame.font.SysFont("comicsans", 50)
    lost_font = pygame.font.SysFont("comicsans", 60)

    enemies = []
    wave_length = 3
    enemy_vel = 5

    player_vel = 10
    laser_vel = 20

    player = Player(50,325)
    player2 = Player()

    clock = pygame.time.Clock()

    lost = False
    lost_count = 0

    def redraw_window():
        WIN.blit(BG, (0,0))
        # draw text
        lives_label = main_font.render(f"Lives: {lives}", 1, (255,255,255))
        level_label = main_font.render(f"Level: {level}", 1, (255,255,255))

        WIN.blit(lives_label, (10, 10))
        WIN.blit(level_label, (WIDTH - level_label.get_width() - 10, 10))

        for enemy in enemies:
            enemy.draw(WIN)

        player.draw(WIN)
        player2.draw2(WIN)
		
        if lost:
            lost_label = lost_font.render("You Lost!!", 1, (255,255,255))
            WIN.blit(lost_label, (WIDTH/2 - lost_label.get_width()/2, 350))

        pygame.display.update()

   
    while run:
        clock.tick(FPS)
        redraw_window()	

        if lives <= 0 or player.health <= 0:
            lost = True
            lost_count += 1

        if lost:
            if lost_count > FPS * 3:
                run = False
            else:
                continue

        if len(enemies) == 0:
            level += 1
            wave_length += 2
            for i in range(wave_length):
                enemy = Enemy(random.randrange(2050,3500), random.randrange(50, HEIGHT-50), random.choice(["red", "blue", "green"]))
                enemies.append(enemy)
                
        if level > 1:
            player2.x = player.x 
            player2.y = player.y - 50
            
            
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player.x - player_vel > 0: 
            player.x -= player_vel
        if keys[pygame.K_RIGHT] and player.x + player_vel + player.get_width() < WIDTH: 
            player.x += player_vel
        if keys[pygame.K_UP] and player.y - player_vel > 0: 
            player.y -= player_vel
        if keys[pygame.K_DOWN] and player.y + player_vel + player.get_height() + 15 < HEIGHT: 
            player.y += player_vel
        if keys[pygame.K_SPACE]:
            player.shoot()
            player2.shoot()

        for enemy in enemies[:]:
            enemy.move(enemy_vel)
            enemy.move_lasers(-laser_vel, player)

            if random.randrange(0, 2*60) == 1:
                enemy.shoot()

            if collide(enemy, player):
                player.health -= 10
                enemies.remove(enemy)
            elif enemy.x + enemy.get_width() < 0:
                lives -= 1
                enemies.remove(enemy)
        
        
        player.move_lasers(laser_vel, enemies)
        player2.move_lasers(laser_vel, enemies)
        
		
def main_menu():
    title_font = pygame.font.SysFont("comicsans", 70)
    run = True
    while run:
        WIN.blit(BG, (0,0))
        title_label = title_font.render("Press the mouse to begin...", 1, (255,255,255))
        WIN.blit(title_label, (WIDTH/2 - title_label.get_width()/2, 350))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                main()
    pygame.quit()


main_menu()

Hors ligne

#5 Le 21/06/2022, à 09:45

kholo

Re : Est il possible de rendre un argument d'une classe python optionnel ?

  File "Space5.py", line 61, in draw
    window.blit(self.ship_img, (self.x, self.y))
TypeError: invalid destination position for blit

c'est ta méthode

    def draw(self, window):
        window.blit(self.ship_img, (self.x, self.y))
        for laser in self.lasers:
            laser.draw(window)

qui reçoit un argument "window" qui pose problème

tu peux déboguer à grand coups de print

    def draw(self, window):
        print("window de type", type(window), window)
        window.blit(self.ship_img, (self.x, self.y))
        for laser in self.lasers:
            laser.draw(window)

et éventuellement mettre ça temporairement dans un try pour faire tourner le code
(la méthode ne fonctionnera pas mais tu pourras regarder le code tourner)

Hors ligne

#6 Le 21/06/2022, à 11:56

MPython Alaplancha

Re : Est il possible de rendre un argument d'une classe python optionnel ?

Salut,
L'exception rencontrée semble plutôt liée aux arguments self.x, self.y fournis à la méthode blit() de pygame.
Aussi, je printerais leur contenu. Cela ne m'étonnerait pas que leurs valeurs soient à None... à voir (code non testable)


# Là pour le plaisir de découvrir ...
# Envie d'apprendre Bash ou Python? Pensez aux MOOC ---> https://forum.ubuntu-fr.org/viewtopic.php?id=2030646

Hors ligne

#7 Le 21/06/2022, à 15:52

chris7522

Re : Est il possible de rendre un argument d'une classe python optionnel ?

Je vous remercie de vos précieux conseils , finalement , j'ai pu trouver une solution à mon problème .
Merci à tous les deux !

Hors ligne