def aim(self, _p2DMouse, _vec): if(self.bCanAim): l_vecCenterImage = Vector2D(_p2DMouse + _vec, self.image.p2DCenter) l_scalarProduct = l_vecCenterImage.scalarProduct(Vector2D(1,0)) if(self.bone.advImage.bFlipX): if(l_scalarProduct < 0): self.flipX() else: if(l_scalarProduct > 0): self.flipX() if(self.down): if(self.bone.advImage.bFlipX): add = 45 else: add = 25 else: if(self.bone.advImage.bFlipX): add = 85 else: add = 93 l_vecRotGun = Vector2D(_p2DMouse + _vec, self.bone.bone[1].advImage.p2DRotation) if(self.bone.advImage.bFlipX): l_vecRotGun = - l_vecRotGun l_fTheta = l_vecRotGun.getAngle(Vector2D(1,0)) + self.bone.advImage.fTheta - add l_vec2 = self.bone.getVec() self.bone.bone[1].setThetaBone(l_fTheta, l_vec2) l_vec2 = self.bone.bone[1].getVec() self.bone.bone[1].bone[0].setThetaBone(0, l_vec2) l_vec2 = self.bone.bone[1].bone[0].getVec() self.bone.bone[1].bone[0].bone[0].setThetaBone(0, l_vec2)
def __init__(self, mass = 0.0, velocity = Vector2D(), acceleration = Vector2D(), life = 0.0, image = None) : self.mass = mass self.velocity = velocity self.acceleration = acceleration self.life = life self.image = image self.last_position = self.image.p2DCenter.copy() self.down = False self.jump = False
def update(self, dt): self.setPosition(self.image.p2DCenter) Entity.update(self, dt) self.updateAnim() if(self.bone.advImage.bFlipX): self.bone.update(dt, Vector2D(Point2D(), Point2D(-1,0))) else: self.bone.update(dt, Vector2D(Point2D(), Point2D(1,0))) self.updateRectangle()
def __init__(self, _advImage, _velocity = Vector2D(), _accelerat = Vector2D(), _mass = 0.0, _life = 0.0, _iVersion = 0): Entity.__init__(self, _mass, _velocity, _accelerat, _life, _advImage) self.iVersion = _iVersion self.bone = None self.AnimationFrames = [] self.create() self.updateRectangle() self.actualAnim = 0 self.bCanAim = True self.bSword = False
def __init__(self): self.width = 1280 self.height = 720 self.window = pygame.display.set_mode((self.width, self.height)) self.window_position = Vector2D(0, 0) ''' DECORS ''' self.background = None self.ground = None self.platforms = [] ''' MENUS ''' self.boutons = [] self.boutons_text = [] self.boutons_main = ['Start', 'HighScore', 'Settings', 'Quit'] self.boutons_pause = ['Resume', 'Restart', 'Menu', 'Settings', 'Quit'] self.boutons_end = ['Restart', 'HighScore', 'Menu', 'Quit'] self.boutons_settings = ['Soon'] self.boutons_scores = ['No Score'] self.bouton_width = 200 self.bouton_height = 80 self.bouton_score_w = 300 self.bouton_score_h = 50 self.state = 'main' self.setting = False self.highscore = False self.myFont = pygame.font.Font('Others/Fonts/myFont.ttf', 36)
def display(self, _screen, _vec=Vector2D(), _bImage=True, _bRec=False, _bLast=False, _bFirst=False): if (not _bFirst): """On affiche les autres membres""" for i in range(len(self.bone) - 1, -1, -1): self.bone[i].display(_screen, _vec, _bImage, _bRec, _bLast) if (_bLast): if (len(self.bone) == 0): """l arbre est vide, on est au dernier membre, on peut l afficher""" if (_bImage): self.advImage.display(_screen, _vec) if (_bRec): pygame.draw.line(_screen, [255, 0, 0], (self.advImage.p2DRotation.x - 4 + _vec.x, self.advImage.p2DRotation.y + _vec.y), (self.advImage.p2DRotation + _vec).getList()) self.advImage.drawRectangle(_screen, _vec) else: if (_bImage): self.advImage.display(_screen, _vec) if (_bRec): pygame.draw.line(_screen, [255, 0, 0], (self.advImage.p2DRotation.x - 4 + _vec.x, self.advImage.p2DRotation.y + _vec.y), (self.advImage.p2DRotation + _vec).getList()) self.advImage.drawRectangle(_screen, _vec)
def update(self, dt, _vec): """Si des cles pour l animation sont presentes, on peut mettre a jour l animation""" if (self.aKeyFrames != None): if (self.iActualKey == -1 and self.initAnimation(_vec)): pass elif (self.iActualKey < len(self.aKeyFrames.afEndT)): self.time += dt if (self.time >= self.aKeyFrames.afEndT[self.iActualKey]): self.setThetaBone(self.aKeyFrames.afTheta[self.iActualKey], _vec) self.initAnimation(_vec) elif (self.iActualKey < len(self.aKeyFrames.afTheta)): if (not self.advImage.bFlipX): self.setThetaBone( self.lastTheta + self.time * self.velocityTheta, _vec) else: self.setThetaBone( (self.lastTheta + self.time * self.velocityTheta), _vec) """On met a jour les sous membres""" for i in range(0, len(self.bone)): self.bone[i].update( dt, Vector2D(self.advImage.aPoints[0], self.advImage.aPoints[3]))
def shoot(self, _p2DMouse, _vec): l_p2DRotGun = self.bone.bone[1].bone[0].bone[0].bone[0].advImage.p2DRotation.copy() l_p2DCent = self.bone.bone[1].bone[0].bone[0].bone[0].advImage.aPoints[3]*0.82 + self.bone.bone[1].bone[0].bone[0].bone[0].advImage.aPoints[0]*0.18 l_vecMousGun = Vector2D(l_p2DCent, l_p2DRotGun) l_vecMousGun.normalize() return (-l_vecMousGun, l_p2DRotGun)
def addBullet(self, entity): if self.dt_weapon > self.dt_gun: if self.current_bullets < self.max_bullets: (vec, position) = self.player.shoot( Point2D(self.input.mouse[1][0], self.input.mouse[1][1]), -self.display.window_position) l_entityBullet = Entity( 0, -vec * 1000.0, Vector2D(0, 0), 0, AdvImage([pygame.image.load('Others/Images/laser.png')], position + vec * -50.0, _fTheta=vec.getAngle(Vector2D(1, 0)))) l_entityBullet.image.setPosition(position + vec * -50.0) self.bullets.append(l_entityBullet) self.dt_weapon = 0 self.current_bullets += 1 else: self.current_bullets = 0 self.dt_weapon = -1
def rotate(self, _fTheta, _p2DcentRot=None): l_p2DcentRot = self.p2DCenter if (_p2DcentRot != None): l_p2DcentRot = _p2DcentRot for i in range(0, 4): self.aPoints[i].rotate(_fTheta, l_p2DcentRot) if (_fTheta != 0): True self.p2DCenter.rotate(_fTheta, l_p2DcentRot) l_vec2 = Vector2D(Point2D(0, 0), Point2D(1, 0)) self.fTheta = self.getAngle(l_vec2)
def createSkeleton(self): """ Le buste: * """ self.bone = Bone(AdvImage([self.image[12]], Point2D(140,180), Point2D(140,250), Point2D(100,150))) """ La tete: o """ if(self.iVersion == 0): self.bone.addBone(AdvImage([self.image[11]], Point2D(150,85), Point2D(150,106), Point2D(65,80))) elif(self.iVersion == 1): self.bone.addBone(AdvImage([self.image[3]], Point2D(150,85), Point2D(150,106), Point2D(65,80))) """ Bras Droit: + """ self.bone.addBone(AdvImage([self.image[2]], Point2D(130,180), Point2D(130,150), Point2D(60,90))) self.bone.bone[1].addBone(AdvImage([self.image[1]], Point2D(130,250), Point2D(130,222), Point2D(40,90))) self.bone.bone[1].bone[0].addBone(AdvImage([self.image[8]], Point2D(130,250), Point2D(135,275), Point2D(40,90))) self.bone.bone[1].bone[0].bone[0].addBone(AdvImage([self.image[10]], Point2D(145,300), Point2D(165,330), Point2D(50,100))) """ Bras Gauche: + """ self.bone.addBone(AdvImage([self.image[2]], Point2D(130,180), Point2D(130,150), Point2D(60,90))) self.bone.bone[2].addBone(AdvImage([self.image[0]], Point2D(130,250), Point2D(130,222), Point2D(40,90))) self.bone.bone[2].bone[0].addBone(AdvImage([self.image[8]], Point2D(130,250), Point2D(135,275), Point2D(40,90))) if(self.iVersion == 0): self.bone.bone[2].bone[0].bone[0].addBone(AdvImage([self.image[14]], Point2D(190,275), Point2D(190,275), Point2D(180,60))) """ Jambe Droite: / """ self.bone.addBone(AdvImage([self.image[5]], Point2D(135,280), Point2D(135,240), Point2D(60,120))) self.bone.bone[3].addBone(AdvImage([self.image[4]], Point2D(155,380), Point2D(150,330), Point2D(50,120))) self.bone.bone[3].bone[0].addBone(AdvImage([self.image[9]], Point2D(160,445), Point2D(150,430), Point2D(65,50))) """ Jambe Gauche: / """ self.bone.addBone(AdvImage([self.image[6]], Point2D(135,280), Point2D(135,240), Point2D(60,120))) self.bone.bone[4].addBone(AdvImage([self.image[4]], Point2D(155,380), Point2D(150,330), Point2D(50,120))) self.bone.bone[4].bone[0].addBone(AdvImage([self.image[9]], Point2D(160,445), Point2D(150,430), Point2D(65,50))) if(self.iVersion == 0): self.bone.addBone(AdvImage([self.image[13]], Point2D(85,190), Point2D(150,106), Point2D(65,80))) """On initialise la taille, rotation, position""" self.bone.scaleBone(1.0) self.bone.setThetaBone(0, Vector2D(Point2D(), Point2D(1,0))) self.translate(Vector2D(100,0))
def updateRectangle(self): l_fLower1 = self.bone.bone[3].bone[0].bone[0].getLowerY() l_fLower2 = self.bone.bone[4].bone[0].bone[0].getLowerY() l_fLower = l_fLower1 if(l_fLower2 > l_fLower1): l_fLower = l_fLower2 l_fHigher = self.bone.bone[0].getHigherY() l_p2DCenter = Point2D(self.image.p2DCenter.x, l_fLower/2 + l_fHigher/2) l_p2DDist = Point2D(self.image.p2DLength.x, l_fLower - l_fHigher) self.image.define(self.image.p2DCenter, l_p2DDist, _fTheta = self.image.fTheta) self.bone.translateBone(Vector2D(l_p2DCenter, self.image.p2DCenter))
def manageCollision(self, _entity): x = False y = False vx = 0 vy = 0 if self.image.collision(_entity.image) : actual_pos = self.image.p2DCenter.copy() self.image.setPosition(Point2D(self.last_position.x, actual_pos.y)) if self.image.collision(_entity.image) : y = True self.velocity.y = 0 self.image.setPosition(Point2D(actual_pos.x, self.last_position.y)) if self.image.collision(_entity.image) : x = True self.velocity.x = 0 self.image.setPosition(actual_pos) while self.image.collision(_entity.image) : if x : if actual_pos.x - self.last_position.x > 0 : vx = -1 else : vx = 1 if y : if actual_pos.y - self.last_position.y > 0 : vy = -1 else : vy = 1 self.velocity.y = 1 if not x and not y : if actual_pos.x - self.last_position.x > 0 : vx = -1 else : vx = 1 if actual_pos.y - self.last_position.y > 0 : vy = -1 else : vy = 1 self.image.translate(Vector2D(vx, vy))
def getVectors(self): l_aVec = [] for i in range(0, 4): l_aVec.append(Vector2D(self.aPoints[i], self.aPoints[(i + 1) % 4])) return l_aVec
def collision(self, _rectangle, _withEdges=True, _insideOnly=False): """withEdges = avec ou sans les bords des rectangles insideOnly permet de detecter si un rectangle est dans un autre""" l_vector1 = Vector2D() l_vector2 = Vector2D() l_point = Point2D() l_compare = MinMax() for c in range(0, 2): if (c == 0): l_vector1.define(self.aPoints[0], self.aPoints[3]) l_vector1.normalize() l_vector2.define(l_point, self.aPoints[0]) l_compare.xMin = l_compare.xMax = l_vector2.scalarProduct( l_vector1) l_vector2.define(l_point, self.aPoints[3]) if (l_vector2.scalarProduct(l_vector1) < l_compare.xMin): l_compare.xMin = l_vector2.scalarProduct(l_vector1) elif (l_vector2.scalarProduct(l_vector1) > l_compare.xMax): l_compare.xMax = l_vector2.scalarProduct(l_vector1) else: l_vector1.define(self.aPoints[0], self.aPoints[1]) l_vector1.normalize() l_vector2.define(l_point, self.aPoints[0]) l_compare.xMin = l_compare.xMax = l_vector2.scalarProduct( l_vector1) l_vector2.define(l_point, self.aPoints[1]) if (l_vector2.scalarProduct(l_vector1) < l_compare.xMin): l_compare.xMin = l_vector2.scalarProduct(l_vector1) elif (l_vector2.scalarProduct(l_vector1) > l_compare.xMax): l_compare.xMax = l_vector2.scalarProduct(l_vector1) l_vector2.define(l_point, _rectangle.aPoints[0]) l_compare.yMin = l_compare.yMax = l_vector2.scalarProduct( l_vector1) for i in range(0, 4): l_vector2.define(l_point, _rectangle.aPoints[i]) if (l_vector2.scalarProduct(l_vector1) < l_compare.yMin): l_compare.yMin = l_vector2.scalarProduct(l_vector1) elif (l_vector2.scalarProduct(l_vector1) > l_compare.yMax): l_compare.yMax = l_vector2.scalarProduct(l_vector1) if (_insideOnly): if (_withEdges): if ((l_compare.xMax < l_compare.yMin) or (l_compare.xMin > l_compare.yMax) or (l_compare.xMax > l_compare.yMax) or (l_compare.xMin < l_compare.yMin)): return False else: if ((l_compare.xMax <= l_compare.yMin) or (l_compare.xMin >= l_compare.yMax) or (l_compare.xMax >= l_compare.yMax) or (l_compare.xMin <= l_compare.yMin)): return False else: if (_withEdges): if ((l_compare.xMax < l_compare.yMin) or (l_compare.xMin > l_compare.yMax)): return False else: if ((l_compare.xMax <= l_compare.yMin) or (l_compare.xMin >= l_compare.yMax)): return False return True
def getVec(self): return Vector2D(self.advImage.aPoints[0], self.advImage.aPoints[3])
def getAngle(self, _vec): l_vec1 = Vector2D(self.aPoints[0], self.aPoints[3]) l_fTheta = l_vec1.getAngle(_vec) return l_fTheta
def loadLevel(self): images = SpriteSheet("Others/Images/sprite512.png", Point2D(42, 42)) buste = SpriteSheet("Others/Images/sprite1024.png", Point2D(84, 84)) seriesOfImages = images.loadFrames(0, 12) seriesOfImages2 = buste.loadFrames(0, 3) for i in range(0, len(seriesOfImages2)): seriesOfImages.append(seriesOfImages2[i]) sprites = AdvImage(seriesOfImages, Point2D(100, 200)) self.player = Skeleton(sprites, Vector2D(0, 1), Vector2D(0, 10), 300, 1) self.player.scale(_fLength=140, _bRectOnly=True) #self.player = Entity(200, Point2D(700, 200), Vector2D(0, 0), Vector2D(0, 10), 1, AdvImage([pygame.image.load('Others/Images/player.jpg').convert_alpha()])) self.player.image.scaleImage(_fLength=140) self.display.background = pygame.image.load( 'Others/Images/background.jpg') #self.display.ground = AdvImage([pygame.image.load('Others/Images/ground.jpg')], Point2D(0, 700)) #self.display.ground.scaleImage(_fLength = 20) #self.platforms.append(AdvImage([pygame.image.load('Others/Images/cube.jpg')], Point2D(1000, 485))) #self.platforms[0].scaleImage(_fLength = 280) #self.platforms.append(AdvImage([pygame.image.load('Others/Images/cube.jpg')], Point2D(2000, 0))) self.platforms.append( Entity( image=AdvImage([pygame.image.load('Others/Images/sol1.jpg')], Point2D(0, 429)))) self.platforms.append( Entity( image=AdvImage([pygame.image.load('Others/Images/sol1.jpg')], Point2D(276, 429)))) self.platforms.append( Entity( image=AdvImage([pygame.image.load('Others/Images/sol3.jpg')], Point2D(552, 429)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/palissade.jpg')], Point2D(1050, 200)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment1.jpg')], Point2D(552, 250)))) self.platforms.append( Entity(image=AdvImage([pygame.image.load('Others/Images/sol.jpg')], Point2D(1050, 660)))) self.platforms.append( Entity( image=AdvImage([pygame.image.load('Others/Images/sol2.jpg')], Point2D(4911, 660)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment2.jpg')], Point2D(1300, 460)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment3.jpg')], Point2D(1510, 335)))) self.platforms.append( Entity( image=AdvImage([pygame.image.load('Others/Images/pont.jpg')], Point2D(2034, 350)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment4.jpg')], Point2D(2450, 330)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/escalier1.jpg')], Point2D(3500, 540)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/escalier2.jpg')], Point2D(3620, 420)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/escalier3.jpg')], Point2D(3740, 300)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/escalier4.jpg')], Point2D(3860, 180)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment8.jpg')], Point2D(4400, 540)))) self.platforms.append( Entity( image=AdvImage([pygame.image.load('Others/Images/pont1.jpg')], Point2D(4600, 540)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment9.jpg')], Point2D(5000, 420)))) self.platforms.append( Entity( image=AdvImage([pygame.image.load('Others/Images/pont2.jpg')], Point2D(5200, 420)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment10.jpg')], Point2D(6400, 360)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment10.jpg')], Point2D(6400, 240)))) self.platforms.append( Entity(image=AdvImage([pygame.image.load('Others/Images/sol.jpg')], Point2D(6600, 660)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment12.jpg')], Point2D(6400, 240)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment13.jpg')], Point2D(6600, 580)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment14.jpg')], Point2D(7300, 0)))) self.platforms.append( Entity(image=AdvImage( [pygame.image.load('Others/Images/batiment15.jpg')], Point2D(6950, 420)))) #self.platforms.append(Entity(image = AdvImage([pygame.image.load('Others/Images/end.jpg')], Point2D(7800, 150)))) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(300, 300)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(1125, 600)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(1065, 150)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(1550, 290)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(2250, 310)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(3795, 260)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(3915, 140)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(5500, 80)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(5600, 350)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(5700, 80)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(5800, 350)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(5900, 80)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(6000, 350)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(6100, 80)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(6200, 350)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(7200, 80)), life=1)) self.targets.append( Entity(image=AdvImage([ pygame.image.load('Others/Images/target.png').convert_alpha() ], Point2D(7500, 80)), life=1)) for i in range(len(self.platforms)): self.platforms[i].image.translateImage( Vector2D(Point2D(), self.platforms[i].image.p2DLength * 0.5))
def init(self): self.window_position = Vector2D(0, 0)
def setPosition(self, _p2DPos): self.translateImage(Vector2D(self.p2DCenter, _p2DPos))
def setPosition(self, _p2DPos): l_vec = Vector2D(self.bone.getCenter(), _p2DPos) self.bone.translateBone(l_vec)
def clearAnimation(self): if(self.bone.advImage.bFlipX): self.bone.clearAnimation(Vector2D(Point2D(), Point2D(-1,0))) else: self.bone.clearAnimation(Vector2D(Point2D(), Point2D(1,0)))