Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. import pygame
  2. import level
  3. import platform
  4. ##import enemies
  5. import math
  6. pygame.init()
  7.  
  8. ## sets up colors that need to be used in every part of the program
  9. black=(0,0,0)
  10. white=(255,255,255)
  11. red=(255,0,0)
  12. green=(0,255,0)
  13. blue=(0,0,255)
  14.  
  15.  
  16. class Player(pygame.sprite.Sprite):
  17. ## This class represents the player. It is inhariting from the Sprite class in Pygame
  18. window=None
  19. screen_width=0
  20. screen_height=0
  21. width=None
  22. height=None
  23. x_velocity=0
  24. y_velocity=0
  25. chrono_level=500
  26. ball_damage=0
  27. bomb_damage=0
  28. blast_radius=0
  29. gravity=0
  30. isjumping=False
  31. isducking=False
  32. level=None
  33. direction=None
  34.  
  35. def __init__(self,argwindow,argsheight,argswidth,argcolor=white,argwidth=40,argheight=60,argx=0,argy=0,argball_damage=5,argbomb_damage=15,argbomb_radius=10,argchrono_level=500):
  36. ## Constructor. Pass in the color, width, and height
  37. pygame.sprite.Sprite.__init__(self)
  38. self.image = pygame.Surface([argwidth,argheight])
  39. self.image.fill(argcolor)
  40. self.rect=self.image.get_rect()
  41. ## sets up the variables inital variables
  42. self.window=argwindow
  43. self.width=argwidth
  44. self.height=argheight
  45. self.screen_height=argsheight
  46. self.screen_width=argswidth
  47. self.rect.x=argx
  48. self.rect.y=argy
  49. self.x_velocity=0
  50. self.y_velocity=0
  51. self.ball_damage=argball_damage
  52. self.bomb_damage=argbomb_damage
  53. self.bomb_radius=argbomb_radius
  54. self.chrono_level=argchrono_level
  55. self.isjumping=False
  56. self.isducking=False
  57.  
  58. def update(self):
  59. ## check gravity
  60. self.calc_grav()
  61. ## move left or right
  62. self.rect.x+=self.x_velocity
  63. ## check for any collisions
  64. platform_hit_list=pygame.sprite.spritecollide(self,self.level.platform_list,False)## this is the pygame generatred collision detection builtin to the sprite class
  65. for platform in platform_hit_list:
  66. if self.x_velocity > 0:##i.e sprite was moving right
  67. self.rect.right = platform.rect.left ##puts the right side of the sprite flush with the left side of the platform
  68. elif self.x_velocity < 0:
  69. self.rect.left = platform.rect.right
  70. self.x_velocity=0
  71. ## move sprite up or down
  72. self.rect.y+=self.y_velocity
  73. ## check for any collisions
  74. platform_hit_list=pygame.sprite.spritecollide(self,self.level.platform_list,False)## this is the pygame generatred collision detection builtin to the sprite class
  75. for platform in platform_hit_list:
  76. if self.y_velocity > 0:## i.e. sprite is falling
  77. self.rect.bottom = platform.rect.top ## puts bottom of player flush with top of platform
  78. self.isjumping=False
  79. elif self.y_velocity < 0:
  80. self.rect.top = platform.rect.bottom
  81. self.y_velocity=0
  82. ## check direction
  83. pos = pygame.mouse.get_pos()
  84. if pos[0] > (self.rect.x+(self.width/2)): ##i.e. cursor is farther to the right then the middle of the sprite
  85. self.direction="Right"
  86. else: ##pos[0] < (self.rect.x+(self.width/2))
  87. self.direction="Left"
  88. def jump(self):
  89. if not self.isjumping:
  90. self.y_velocity=-15
  91. self.isjumping=True
  92.  
  93. def calc_grav(self):
  94. if self.y_velocity ==0:
  95. self.y_velocity=1
  96. else:
  97. self.y_velocity+=self.gravity
  98.  
  99. if self.rect.y >= self.screen_height - self.rect.height and self.y_velocity >= 0:
  100. self.y_velocity = 0
  101. self.rect.y = self.screen_height - self.rect.height
  102. self.isjumping=False
  103.  
  104. def move_left(self):## called if the player hits the left arrow key or the a key
  105. self.x_velocity=-5
  106.  
  107. def move_right(self):## called is the player hits the right arrow key or the d key
  108. self.x_velocity=5
  109.  
  110. def stop(self):## called if the player lets up on either arrow key or the a or d key
  111. self.x_velocity=0
  112.  
  113. def shoot(self,argmouse_position):
  114. if self.direction=="Left":
  115. bullet_start_x=self.rect.x
  116. bullet_start_y=(self.rect.y+(self.height/2))
  117. elif self.direction=="Right":
  118. bullet_start_x=(self.rect.x+self.width)
  119. bullet_start_y=(self.rect.y+(self.height/2))
  120. bullet=player_bullet(bullet_start_x,bullet_start_y,argmouse_position)
  121. return (bullet)
  122.  
  123.  
  124. class player_bullet(pygame.sprite.Sprite):
  125.  
  126. bullet_x=None
  127. bullet_y=None
  128. bullet_x_velocity=None
  129. bullet_y_velocity=None
  130. target_x=None
  131. target_y=None
  132. speed=10
  133.  
  134. def __init__(self,argx,argy,argmouse_positon):
  135. pygame.sprite.Sprite.__init__(self)
  136. print "it inited"
  137. self.image = pygame.Surface([4, 10])
  138. self.image.fill(black)
  139. self.rect=self.image.get_rect()
  140. self.rect.x=argx
  141. self.rect.y=argy
  142. self.bullet_x=argx
  143. self.bullet_y=argy
  144. self.target_x=argmouse_positon[0]
  145. self.target_y=argmouse_positon[1]
  146. dx=self.target_x-self.bullet_x
  147. dy=self.target_y-self.bullet_y
  148. angle=math.atan2(dy,dx)
  149. print angle
  150. self.bullet_x_velocity=self.speed*math.cos(angle)
  151. self.bullet_y_velocity=self.speed*math.sin(angle)
  152. def update(self):
  153. print self.rect.x
  154. print self.bullet_x_velocity
  155. self.rect.x+=self.bullet_x_velocity
  156. print self.rect.x
  157. self.rect.y+=self.bullet_y_velocity
  158. def collide(self,argdisplay_width,argdisplay_height,argplatform_list):
  159. Platform_hit_list=pygame.sprite.spritecollide(self, argplatform_list, False)
  160. if len(Platform_hit_list) > 0:
  161. return True
  162. elif self.rect.x > argdisplay_width or self.rect.x < 0:
  163. return True
  164. elif self.rect.y > argdisplay_height or self.rect.y < 0:
  165. return True
  166. else:
  167. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement