[pygame] Platformer 2D - Probè

Platformer 2D - Probè [pygame] - Python - Programmation

Marsh Posté le 28-04-2018 à 01:51:59    

Bonjour,  
Je suis une grande débutante sur Python et dans la programmation en général. Dans le cadre de mes études j'ai choisi une option programmation et je dois rendre un devoir. J'ai choisi de faire un platformer 2D et je bloque totalement au niveau du Boss, il est censé envoyer un projectile sur le player mais je n'arrive pas à lui donner correctement cette directive.  
J'ai essayé des dizaines et des dizaines de fois, j'ai retourné mon script dans tous les sens, je l'ai modifié du tout au tout, j'ai fait pleins de recherches sur internet mais rien n'y fait :(  
Est ce que vous avez une idée de ce que je pourrais faire ?
Ps : je sais qu'il doit être rempli d'erreurs, désolée pour vos yeux ^^'
 
Voici le script du Boss :  
 

Code :
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3.  
  4. WIN_WIDTH = 990
  5. WIN_HEIGHT = 550
  6.  
  7. DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
  8. pygame.init()
  9. pygame.font.init()
  10. screen = pygame.display.set_mode(DISPLAY)
  11. pygame.display.set_caption("Final" )
  12. white = (255, 255, 255)
  13.  
  14. def niveaufinal():
  15.    timer = pygame.time.Clock()
  16.    up = down = left = right = attaque = False
  17.    bg = pygame.Surface((32,32))
  18.    bg.convert()
  19.    bg.fill(pygame.Color("#000000" ))
  20.    entities = pygame.sprite.Group()
  21.    player = Player(32, 32)
  22.    Pro = projectile(450,295)
  23.    platforms = []
  24.    game_over = False
  25.    
  26.    x = y = 0
  27.    level = [
  28.        "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
  29.        "P                                       P",
  30.        "P                                       P",
  31.        "P                                       P",
  32.        "P    SSS                                P",
  33.        "P    BBBBB       K         K            P",
  34.        "P               BBBBB    BBBBB          P",
  35.        "P        K           SSSS               P",
  36.        "P       BBB                             P",
  37.        "P                                       P",
  38.        "P                                       P",
  39.        "P                                       P",
  40.        "P            BB    R                    P",
  41.        "P                      BBBB             P",      
  42.        "P                                       P",
  43.        "P               BBBBBBB                 P",
  44.        "P                                       P",
  45.        "P         BB                            P",
  46.        "P             K                         P",
  47.        "P             BBBBBB                    P",
  48.        "PBBBBBB                                 P",
  49.        "P                    SS                EP",
  50.        "PDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDP",]
  51.  
  52.    for row in level:
  53.        for col in row:
  54.            if col == "B":
  55.                b = Bloc(x, y)
  56.                platforms.append(b)
  57.                entities.add(b)
  58.            if col == "P":
  59.                p = Platform(x, y)
  60.                platforms.append(p)
  61.                entities.add(p)
  62.            if col == "E":
  63.                e = ExitBlock(x, y)
  64.                platforms.append(e)
  65.                entities.add(e)
  66.            if col == "R":
  67.                r = BossBlock(x, y)
  68.                platforms.append(r)
  69.                entities.add(r)
  70.            if col == "D":
  71.                d = DeadBlock(x, y)
  72.                platforms.append(d)
  73.                entities.add(d)
  74.            if col == "K":
  75.                k = DeadBlock2(x, y)
  76.                platforms.append(k)
  77.                entities.add(k)
  78.            if col == "S":
  79.                s = DeadBlock3(x, y)
  80.                platforms.append(s)
  81.                entities.add(s)
  82.            x += 24
  83.        y += 24
  84.        x = 0
  85.  
  86.    entities.add(player)
  87.    entities.add(Pro)
  88.    
  89.    
  90.    while not game_over :
  91.        timer.tick(55)
  92.        for event in pygame.event.get():
  93.            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
  94.                pygame.quit()
  95.            if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
  96.                up = True
  97.            if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
  98.                down = True
  99.            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
  100.                left = True
  101.            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
  102.                right = True
  103.  
  104.            if event.type == pygame.KEYUP and event.key == pygame.K_UP:
  105.                up = False
  106.            if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
  107.                down = False
  108.            if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
  109.                right = False
  110.            if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
  111.                left = False
  112.            if event.type == pygame.KEYUP and event.key == pygame.K_SPACE:
  113.                attaque = False
  114.                
  115.                
  116.        for y in range(32):
  117.            for x in range(32):
  118.                screen.blit(bg, (x * 32, y * 32))
  119.        
  120.        player.update(up, down, left, right, attaque, platforms)
  121.        Player.move_projectile
  122.  
  123.        
  124.        entities.draw(screen)
  125.        pygame.display.update()
  126.        
  127.  
  128. class Entity(pygame.sprite.Sprite):
  129.    def __init__(self):
  130.        pygame.sprite.Sprite.__init__(self)
  131.  
  132. class Player(Entity):
  133.    def __init__(self, x, y):
  134.        Entity.__init__(self)
  135.        self.xvel = 0
  136.        self.yvel = 0
  137.        self.onGround = False
  138.        
  139.        self.image = pygame.image.load('F.png').convert_alpha()
  140.        self.rect = pygame.Rect(x, y, 24, 32)
  141.        self.pos_playeur = self.image.get_rect()
  142.        self.face=0    
  143.        
  144.        marcheD1 = pygame.image.load('D1.png')
  145.        marcheD2 = pygame.image.load('D2.png')
  146.        marcheD3 = pygame.image.load('D3.png')
  147.        marcheD4 = pygame.image.load('D4.png')
  148.        marcheD5 = pygame.image.load('D5.png')
  149.        marcheD6 = pygame.image.load('D6.png')
  150.        self.marcheD=[marcheD1,marcheD2,marcheD3,marcheD4,marcheD5,marcheD6]
  151.        
  152.        marcheG1 = pygame.image.load('G1.png')
  153.        marcheG2 = pygame.image.load('G2.png')
  154.        marcheG3 = pygame.image.load('G3.png')
  155.        marcheG4 = pygame.image.load('G4.png')
  156.        marcheG5 = pygame.image.load('G5.png')
  157.        marcheG6 = pygame.image.load('G6.png')
  158.        self.marcheG=[marcheG1,marcheG2,marcheG3,marcheG4,marcheG5,marcheG6]
  159.        
  160.        A1 = pygame.image.load('A1.png')
  161.        A2 = pygame.image.load('A2.png')
  162.        A3 = pygame.image.load('A3.png')
  163.        self.Attaque=[A1,A2,A3]
  164.  
  165.    def update(self, up, down, left, right, attaque, platforms):
  166.        if up:
  167.            if self.onGround: self.yvel -= 7
  168.        if down:
  169.            pass
  170.        if attaque:
  171.            self.face=(self.face+1)%2
  172.            self.image= self.Attaque[0+self.face]
  173.        if left:
  174.            self.xvel = -5
  175.            self.face=(self.face+1)%2
  176.            self.image= self.marcheG[0+self.face]
  177.        if right:
  178.            self.xvel = 5
  179.            self.face=(self.face+1)%2
  180.            self.image= self.marcheD[2+self.face]
  181.        if not self.onGround:
  182.            self.yvel += 0.3
  183.            if self.yvel > 100: self.yvel = 100
  184.        if not(left or right):
  185.            self.xvel = 0
  186.        
  187.        self.rect.left += self.xvel
  188.        self.collide(self.xvel, 0, platforms, attaque)
  189.        self.rect.top += self.yvel
  190.        self.onGround = False;
  191.        self.collide(0, self.yvel, platforms, attaque)
  192.    
  193.    def collide(self, xvel, yvel, platforms, attaque):
  194.        self.V = 5
  195.        for p in platforms:
  196.            if pygame.sprite.collide_rect(self, p):
  197.                if isinstance(p, ExitBlock):
  198.                    game_over = True
  199.                if isinstance(p, DeadBlock):
  200.                    print('Game Over')
  201.                    niveaufinal()
  202.                if isinstance(p, DeadBlock2):
  203.                    print('Game Over')
  204.                    niveaufinal()
  205.                if isinstance(p, DeadBlock3):
  206.                    print('Game Over')
  207.                    niveaufinal()
  208.                #if isinstance(p, Player.move_projectile): #si la boule te touches tu meurs
  209.                    #niveaufinal()
  210.                    #game_over = True #ajouter un écran Game Over
  211.                if attaque :
  212.                    if isinstance(p, BossBlock):
  213.                        self.V -= 1
  214.                if xvel > 0:
  215.                    self.rect.right = p.rect.left
  216.                    print("collide right" )
  217.                if xvel < 0:
  218.                    self.rect.left = p.rect.right
  219.                    print("collide left" )
  220.                if yvel > 0:
  221.                    self.rect.bottom = p.rect.top
  222.                    self.onGround = True
  223.                    self.yvel = 0
  224.                if yvel < 0:
  225.                    self.rect.top = p.rect.bottom
  226.  
  227.    def move_projectile (self):
  228.        x_pos = True
  229.        x_neg = False
  230.        y_pos = False
  231.        y_neg = True
  232.        pr = projectile()
  233.        scs = 0
  234.        print("move début" )
  235.        if x_pos == True:
  236.            projectile.xvel += projectile.vel
  237.        else:
  238.            projectile.xvel -= projectile.vel
  239.        if y_pos == True:
  240.            projectile.yvel += projectile.vel
  241.        else:
  242.            projectile.yvel -= projectile.vel
  243.        while scs < 4 :
  244.            if projectile.xvel > Player.xvel:
  245.                x_pos = False
  246.                x_neg = True
  247.                projectile.xvel -= projectile.vel
  248.            elif projectile.xvel < Player.xvel:
  249.                x_pos = True
  250.                x_neg = False
  251.                projectile.xvel += projectile.vel
  252.            if projectile.yvel < Player.yvel:
  253.                y_pos = True
  254.                y_neg = False
  255.                projectile.yvel += projectile.vel
  256.            elif projectile.yvel > Player.yvel:
  257.                y_pos = False
  258.                y_neg = True
  259.                projectile.yvel -= projectile.vel
  260.        scs += 1
  261.        print('move ok')
  262.        
  263.        
  264. class Platform(Entity):
  265.    def __init__(self, x, y):
  266.        Entity.__init__(self)
  267.        self.image = pygame.Surface((32, 32))
  268.        self.image.convert()
  269.        self.image.fill(pygame.Color("#DDDDDD" ))
  270.        self.rect = pygame.Rect(x, y, 32, 32)
  271.  
  272.  
  273. class BossBlock(Platform):
  274.    def __init__(self, x, y):
  275.        Platform.__init__(self, x, y)
  276.        self.onGround = False
  277.        self.image = pygame.image.load('Boss1.png').convert_alpha()
  278.        self.rect = pygame.Rect(x, y, 35, 55)
  279.        self.pos_boss = self.image.get_rect()
  280.        self.rect.top,self.rect.left=(y,x)
  281.        
  282.  
  283. class projectile(Entity):
  284.    def __init__(self, x, y):
  285.        Entity.__init__(self)
  286.        projectile.xvel = 0
  287.        projectile.yvel = 0
  288.        projectile.radius = 10
  289.        projectile.size = 10
  290.        projectile.color = white
  291.        projectile.vel = 10
  292.        self.image = pygame.image.load('boule.png').convert_alpha()
  293.        self.rect = pygame.Rect(x, y, 24, 32)
  294.        print('projectile ok')
  295.  
  296.    
  297.    def draw(self, screen):
  298.        pygame.draw.circle(screen, projectile.color, (projectile.xvel, projectile.yvel), projectile.radius)
  299.        print('draw ok')
  300.    
  301.  
  302. class Bloc(Platform):
  303.    def __init__(self, x, y):
  304.        Platform.__init__(self, x, y)
  305.        self.image = pygame.image.load('P4.png').convert_alpha()
  306.  
  307. class ExitBlock(Platform):
  308.    def __init__(self, x, y):
  309.        Platform.__init__(self, x, y)
  310.        self.image.fill(pygame.Color("#0033FF" ))
  311.  
  312. class DeadBlock(Platform):
  313.    def __init__(self, x, y):
  314.        Platform.__init__(self, x, y)
  315.        self.image = pygame.image.load('4a.png').convert_alpha()
  316.        
  317. class DeadBlock2(Platform):
  318.    def __init__(self, x, y):
  319.        Platform.__init__(self, x, y)
  320.        self.image = pygame.image.load('4b.png').convert_alpha()
  321.        self.rect = pygame.Rect(x, y, 24, 30)
  322.  
  323. class DeadBlock3(Platform):
  324.    def __init__(self, x, y):
  325.        Platform.__init__(self, x, y)
  326.        self.image = pygame.image.load('4c.png').convert_alpha()
  327.  
  328. if __name__ == "__main__":
  329.    niveaufinal()


 
 
Ps2 : je vois que mon level = [] s'est mal copié malheureusement, et si besoin je peux vous transmettre les images :)  
 
Merci d'avaaaaaannceee  !! :D


Message édité par gilou le 30-04-2018 à 10:13:47
Reply

Marsh Posté le 28-04-2018 à 01:51:59   

Reply

Marsh Posté le 30-04-2018 à 10:01:51    

Que veux tu dire par "je n'arrive pas à lui envoyer correctement cette directive" ?
Sois un peu plus précise dans ta question stp, tu augmenteras tes chances d'avoir une réponse.
Et encadre ton code avec la balise [code], ça facilitera la lecture de ton programme.

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed