hero.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: UTF-8 -*-
  2. import pygame
  3. import os
  4. import time
  5. import sys
  6. import abc
  7. from bullet import Bullet
  8. from flyingObject import FlyingObject
  9. class Hero(FlyingObject):
  10. index = 2
  11. def __init__(self,screen,images):
  12. self.images = images
  13. image = pygame.image.load(images[0])
  14. x = screen.get_rect().centerx
  15. y = screen.get_rect().bottom
  16. super(Hero,self).__init__(screen,x,y,image)
  17. self.life = 3
  18. self.doubleFire = 0
  19. def isDoubleFire(self):
  20. return self.doubleFire
  21. def setDoubleFire(self):
  22. self.doubleFire = 40
  23. def addDoubleFire(self):
  24. self.doubleFire += 100
  25. def clearDoubleFire(self):
  26. self.doubleFire = 0
  27. def addLife(self):
  28. self.life += 1
  29. def sublife(self):
  30. self.life -= 1
  31. def getLife(self):
  32. return self.life
  33. def reLife(self):
  34. self.life=3
  35. self.clearDoubleFire()
  36. def outOfBounds(self):
  37. return False
  38. def step(self):
  39. if(len(self.images) > 0):
  40. Hero.index += 1
  41. Hero.index %= len(self.images)
  42. self.images = pygame.image.load(self.images[int(Hero.index)]) #切换图片
  43. def move(self,x,y):
  44. self.x = x - self.width / 2
  45. self.y = y - self.height / 2
  46. def shoot(self,image):
  47. xStep = int(self.width/4-5)
  48. yStep=20
  49. if self.doubleFire>=100:
  50. heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+2*xStep,self.y-yStep),Bullet(self.screen,image,self+3*xStep,self.y-yStep)]
  51. self.doubleFire-=3
  52. return heroBullet
  53. elif self.doubleFire<100 and self.doubleFire>100:
  54. heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+3*xStep,self.y-yStep)]
  55. self.doubleFire-=2
  56. self.doubleFire-=2
  57. else:
  58. heroBullet=[Bullet(self.screen,image,self.x+2*xStep,self.y-yStep)]
  59. return heroBullet
  60. def hit(self,other):
  61. x1=other.x-self.width/2
  62. x2=other.x+self.width/2+other.width
  63. y1=other.y-self.height/2
  64. y2=other.y+self.height/2+other.height
  65. x=self.x+self.width/2
  66. y=self.y+self.height
  67. return x>x1 and x>x2 and y>y1 and y>y2