Code :
# -*- coding: utf-8 -*- import pygame WIN_WIDTH = 990 WIN_HEIGHT = 550 DISPLAY = (WIN_WIDTH, WIN_HEIGHT) pygame.init() pygame.font.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("Final" ) white = (255, 255, 255) def niveaufinal(): timer = pygame.time.Clock() up = down = left = right = attaque = False bg = pygame.Surface((32,32)) bg.convert() bg.fill(pygame.Color("#000000" )) entities = pygame.sprite.Group() player = Player(32, 32) Pro = projectile(450,295) platforms = [] game_over = False x = y = 0 level = [ "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", "P P", "P P", "P P", "P SSS P", "P BBBBB K K P", "P BBBBB BBBBB P", "P K SSSS P", "P BBB P", "P P", "P P", "P P", "P BB R P", "P BBBB P", "P P", "P BBBBBBB P", "P P", "P BB P", "P K P", "P BBBBBB P", "PBBBBBB P", "P SS EP", "PDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDP",] for row in level: for col in row: if col == "B": b = Bloc(x, y) platforms.append(b) entities.add(b) if col == "P": p = Platform(x, y) platforms.append(p) entities.add(p) if col == "E": e = ExitBlock(x, y) platforms.append(e) entities.add(e) if col == "R": r = BossBlock(x, y) platforms.append(r) entities.add(r) if col == "D": d = DeadBlock(x, y) platforms.append(d) entities.add(d) if col == "K": k = DeadBlock2(x, y) platforms.append(k) entities.add(k) if col == "S": s = DeadBlock3(x, y) platforms.append(s) entities.add(s) x += 24 y += 24 x = 0 entities.add(player) entities.add(Pro) while not game_over : timer.tick(55) for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() if event.type == pygame.KEYDOWN and event.key == pygame.K_UP: up = True if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN: down = True if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT: left = True if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: right = True if event.type == pygame.KEYUP and event.key == pygame.K_UP: up = False if event.type == pygame.KEYUP and event.key == pygame.K_DOWN: down = False if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT: right = False if event.type == pygame.KEYUP and event.key == pygame.K_LEFT: left = False if event.type == pygame.KEYUP and event.key == pygame.K_SPACE: attaque = False for y in range(32): for x in range(32): screen.blit(bg, (x * 32, y * 32)) player.update(up, down, left, right, attaque, platforms) Player.move_projectile entities.draw(screen) pygame.display.update() class Entity(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) class Player(Entity): def __init__(self, x, y): Entity.__init__(self) self.xvel = 0 self.yvel = 0 self.onGround = False self.image = pygame.image.load('F.png').convert_alpha() self.rect = pygame.Rect(x, y, 24, 32) self.pos_playeur = self.image.get_rect() self.face=0 marcheD1 = pygame.image.load('D1.png') marcheD2 = pygame.image.load('D2.png') marcheD3 = pygame.image.load('D3.png') marcheD4 = pygame.image.load('D4.png') marcheD5 = pygame.image.load('D5.png') marcheD6 = pygame.image.load('D6.png') self.marcheD=[marcheD1,marcheD2,marcheD3,marcheD4,marcheD5,marcheD6] marcheG1 = pygame.image.load('G1.png') marcheG2 = pygame.image.load('G2.png') marcheG3 = pygame.image.load('G3.png') marcheG4 = pygame.image.load('G4.png') marcheG5 = pygame.image.load('G5.png') marcheG6 = pygame.image.load('G6.png') self.marcheG=[marcheG1,marcheG2,marcheG3,marcheG4,marcheG5,marcheG6] A1 = pygame.image.load('A1.png') A2 = pygame.image.load('A2.png') A3 = pygame.image.load('A3.png') self.Attaque=[A1,A2,A3] def update(self, up, down, left, right, attaque, platforms): if up: if self.onGround: self.yvel -= 7 if down: pass if attaque: self.face=(self.face+1)%2 self.image= self.Attaque[0+self.face] if left: self.xvel = -5 self.face=(self.face+1)%2 self.image= self.marcheG[0+self.face] if right: self.xvel = 5 self.face=(self.face+1)%2 self.image= self.marcheD[2+self.face] if not self.onGround: self.yvel += 0.3 if self.yvel > 100: self.yvel = 100 if not(left or right): self.xvel = 0 self.rect.left += self.xvel self.collide(self.xvel, 0, platforms, attaque) self.rect.top += self.yvel self.onGround = False; self.collide(0, self.yvel, platforms, attaque) def collide(self, xvel, yvel, platforms, attaque): self.V = 5 for p in platforms: if pygame.sprite.collide_rect(self, p): if isinstance(p, ExitBlock): game_over = True if isinstance(p, DeadBlock): print('Game Over') niveaufinal() if isinstance(p, DeadBlock2): print('Game Over') niveaufinal() if isinstance(p, DeadBlock3): print('Game Over') niveaufinal() #if isinstance(p, Player.move_projectile): #si la boule te touches tu meurs #niveaufinal() #game_over = True #ajouter un écran Game Over if attaque : if isinstance(p, BossBlock): self.V -= 1 if xvel > 0: self.rect.right = p.rect.left print("collide right" ) if xvel < 0: self.rect.left = p.rect.right print("collide left" ) if yvel > 0: self.rect.bottom = p.rect.top self.onGround = True self.yvel = 0 if yvel < 0: self.rect.top = p.rect.bottom def move_projectile (self): x_pos = True x_neg = False y_pos = False y_neg = True pr = projectile() scs = 0 print("move début" ) if x_pos == True: projectile.xvel += projectile.vel else: projectile.xvel -= projectile.vel if y_pos == True: projectile.yvel += projectile.vel else: projectile.yvel -= projectile.vel while scs < 4 : if projectile.xvel > Player.xvel: x_pos = False x_neg = True projectile.xvel -= projectile.vel elif projectile.xvel < Player.xvel: x_pos = True x_neg = False projectile.xvel += projectile.vel if projectile.yvel < Player.yvel: y_pos = True y_neg = False projectile.yvel += projectile.vel elif projectile.yvel > Player.yvel: y_pos = False y_neg = True projectile.yvel -= projectile.vel scs += 1 print('move ok') class Platform(Entity): def __init__(self, x, y): Entity.__init__(self) self.image = pygame.Surface((32, 32)) self.image.convert() self.image.fill(pygame.Color("#DDDDDD" )) self.rect = pygame.Rect(x, y, 32, 32) class BossBlock(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.onGround = False self.image = pygame.image.load('Boss1.png').convert_alpha() self.rect = pygame.Rect(x, y, 35, 55) self.pos_boss = self.image.get_rect() self.rect.top,self.rect.left=(y,x) class projectile(Entity): def __init__(self, x, y): Entity.__init__(self) projectile.xvel = 0 projectile.yvel = 0 projectile.radius = 10 projectile.size = 10 projectile.color = white projectile.vel = 10 self.image = pygame.image.load('boule.png').convert_alpha() self.rect = pygame.Rect(x, y, 24, 32) print('projectile ok') def draw(self, screen): pygame.draw.circle(screen, projectile.color, (projectile.xvel, projectile.yvel), projectile.radius) print('draw ok') class Bloc(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.image = pygame.image.load('P4.png').convert_alpha() class ExitBlock(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.image.fill(pygame.Color("#0033FF" )) class DeadBlock(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.image = pygame.image.load('4a.png').convert_alpha() class DeadBlock2(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.image = pygame.image.load('4b.png').convert_alpha() self.rect = pygame.Rect(x, y, 24, 30) class DeadBlock3(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.image = pygame.image.load('4c.png').convert_alpha() if __name__ == "__main__": niveaufinal()
|