def __init__(self, x, y, width=0, height=0): super(Player, self).__init__(x, y, width, height) # Player's boolean conditions self.isAlive = True # Because he can die self.isWalking = False # Obvious self.isCrouching = False # Obvious self.isFiring = False self.reloadTrigger = False self.position = {'direction': 'LEFT', 'mouse': 0} # Player inputs self.up = self.down = self.left = self.right = False self.mouseLeft = self.mouseMid = self.mouseRight = False # The rotating arm self.arm = Arm(self.rect.centerx, self.rect.centery, "Rifle") # Animations try: self.addAnimation('charge', 0.1, [("sprites/pixel.%s.png" % x) for x in range(1,4)]) except: try: self.addAnimation('charge', 0.1, [("sprites\pixel.%s.png" % x) for x in range(1,4)]) except Exception: try: print("File name error") except Exception: print "File name error"
class Player(entity.Movable): # Class Player # Contains input, status, movement, animation # Initialization method def __init__(self, x, y, width=0, height=0): super(Player, self).__init__(x, y, width, height) # Player's boolean conditions self.isAlive = True # Because he can die self.isWalking = False # Obvious self.isCrouching = False # Obvious self.isFiring = False self.reloadTrigger = False self.position = {'direction': 'LEFT', 'mouse': 0} # Player inputs self.up = self.down = self.left = self.right = False self.mouseLeft = self.mouseMid = self.mouseRight = False # The rotating arm self.arm = Arm(self.rect.centerx, self.rect.centery, "Rifle") # Animations try: self.addAnimation('charge', 0.1, [("sprites/pixel.%s.png" % x) for x in range(1,4)]) except: try: self.addAnimation('charge', 0.1, [("sprites\pixel.%s.png" % x) for x in range(1,4)]) except Exception: try: print("File name error") except Exception: print "File name error" #===================== INPUTING ======================================# def input(self, e): if e.type == KEYDOWN: if e.key == K_LEFT: self.left, self.right = True, False if e.key == K_RIGHT: self.left, self.right = False, True if e.key == K_UP and self.onGround: self.jumpForce = 11.4 if e.type == KEYUP: if e.key == K_LEFT: self.left = False if e.key == K_RIGHT: self.right = False (mouse_x, mouse_y) = mouse.get_pos() (self.mouseLeft, self.mouseMid, self.mouseRight) = mouse.get_pressed() if e.type == MOUSEBUTTONDOWN and self.mouseLeft: self.isFiring = True if e.type == MOUSEBUTTONUP and not self.mouseLeft: self.isFiring = False if e.type == KEYDOWN and e.key: self.reloadTrigger = True if e.type == MOUSEMOTION: #self.arm.onMouseAction((mouse_x, mouse_y), self.rect) self.position['mouse'] = mouse_x self.arm.onMouseAction(mouse.get_pos(), self.rect, self.mouseLeft, self.mouseRight) return mouse.get_focused() #===================== UPDATING ======================================# # Updates player's movement def update(self, floorGroup, bulletGroup, granadeGroup): self._horizontalMotion() self._verticalMotion() super(Player, self).motion(floorGroup, self.hVelocity) self.arm.fire(self.isFiring, self.reloadTrigger, bulletGroup, granadeGroup) self.reloadTrigger = False def _horizontalMotion(self): maxVelocity = 5 maxAcceleration = 1.1 # Horizontal concern if self.onGround and not self.hitWall: if self.left or self.right: if self.left: if self.acceleration > -maxAcceleration: self.acceleration -= 0.2 elif self.right: if self.acceleration < maxAcceleration: self.acceleration += 0.2 if self.hVelocity < maxVelocity and self.hVelocity > -maxVelocity: self.hVelocity += self.acceleration else: if self.hVelocity > 0: self.hVelocity -= 0.7 if self.hVelocity < 0: self.hVelocity += 0.7 else: self.acceleration = 0 if self.hVelocity > -1 or self.hVelocity < 1: self.hVelocity = 0 else: if self.hVelocity > 0: self.hVelocity -= 0.3 else: self.hVelocity += 0.3 elif self.hitWall: self.hVelocity = 0 self.acceleration = 0 elif not self.onGround: if self.hVelocity < 1 and self.hVelocity > -1: if self.left: self.hVelocity -= 2 self.acceleration -= 1.0 if self.right: self.hVelocity += 2 self.acceleration += 1.0 def _verticalMotion(self): if not self.hitCeiling: if self.jumpForce > 0: self.jumpForce -= 2 self.vVelocity -= self.jumpForce if self.jumpForce <= 0: self.jumpForce = 0 else: self.jumpForce = 0 self.vVelocity = 0 # Change direction of the player according to actual # direction and mouse position on screen if self.position['direction']== 'LEFT' \ and self.position['mouse'] > self.rect.centerx: # self.flip() self.position['direction']='RIGHT' if self.position['direction']== 'RIGHT' \ and self.position['mouse'] <= self.rect.centerx: # self.flip() self.position['direction']='LEFT' #===================== DRAWING =======================================# def draw(self, surface): self.arm.draw(surface, self.rect.centerx, self.rect.centery) # self._animatingFire('charge', surface) draw.rect(surface, (255, 0, 0), self.rect) draw.rect(surface, (50, 50, 50), self.arm.rect)