Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.30 KB | None | 0 0
  1. """ chase.py
  2. Chasing a sprite
  3. """
  4. import pygame
  5. import random
  6. import math
  7. pygame.init()
  8. global distance
  9.  
  10. class ControlData:
  11. def __init__(self):
  12. self.moveX = 0
  13. self.moveY = 0
  14.  
  15.  
  16. class StateDefine():
  17. def __init__(self):
  18. self.currentState=0
  19. self.currentState2=0
  20. self.health = 100
  21. global fire
  22.  
  23. class Missle(pygame.sprite.Sprite):
  24. def __init__(self,screen, (x, y), playerShip):
  25. pygame.sprite.Sprite.__init__(self)
  26. self.image = pygame.image.load("missle.png").convert()
  27. self.image = self.image.convert()
  28. tranColor = self.image.get_at((1, 1))
  29. self.image.set_colorkey(tranColor)
  30. self.rect = self.image.get_rect()
  31. self.missleX = float(x)
  32. self.missleY = float(y)
  33. self.ship = playerShip
  34. self.count = 0
  35. self.currentState2 = 0
  36. self.screen = screen
  37.  
  38.  
  39.  
  40.  
  41. def update(self):
  42. ##PatternMovement----------------
  43. keys = pygame.key.get_pressed()
  44. if keys[pygame.K_SPACE]:
  45. print 'fire'
  46. fire3=True
  47.  
  48. self.count += 1
  49.  
  50. if self.count < 600:
  51. self.caculateNextPosition()
  52. self.rect.center = (missleX, missleY)
  53. elif self.count < 600:
  54. self.caculateNextPositionEvade()
  55. self.rect.center = (missleX, missleY)
  56. else:
  57. self.count = 0
  58.  
  59. def caculateNextPosition(self): ##Fastest Spaceship
  60. missleX = self.ship.rect.centerx
  61. missleY = self.ship.rect.centery
  62. keys = pygame.key.get_pressed()
  63.  
  64. if missleY < self.missleY:
  65. missleY += 3
  66. elif missleY > self.missleY:
  67. missleY -= 3
  68.  
  69. if missleX < self.missleX:
  70. missleX += 3
  71. elif missleX > self.missleX :
  72. missleX -= 3
  73.  
  74.  
  75.  
  76. class Box(pygame.sprite.Sprite):
  77. def __init__(self,screen, (x, y), playerShip):
  78. pygame.sprite.Sprite.__init__(self)
  79. self.image = pygame.image.load("heart.png").convert()
  80. self.image = self.image.convert()
  81. tranColor = self.image.get_at((1, 1))
  82. self.image.set_colorkey(tranColor)
  83. self.rect = self.image.get_rect()
  84. self.boxX = float(x)
  85. self.boxY = float(y)
  86. self.ship = playerShip
  87. self.count = 0
  88. self.currentState2 = 0
  89. self.screen = screen
  90.  
  91.  
  92.  
  93.  
  94. ptu = ControlData()
  95. ptu.moveX = 1
  96. ptu.moveY = 1
  97.  
  98. ptd = ControlData()
  99. ptd.moveX = 1
  100. ptd.moveY = -1
  101.  
  102. self.pattern = [ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu,
  103. ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd,]
  104.  
  105. self.patternIndex = 0
  106.  
  107. def update(self):
  108. ##PatternMovement----------------
  109.  
  110. posX=self.boxX
  111. posY=self.boxY
  112. cd=self.pattern[self.patternIndex]
  113. self.boxX+=cd.moveX
  114. self.boxY+=cd.moveY
  115. self.patternIndex += 1
  116. if self.patternIndex >= len(self.pattern):
  117. self.patternIndex = 0
  118.  
  119.  
  120. self.count += 1
  121.  
  122.  
  123. #So the spaceship won't go outside the screen
  124. if self.boxX > self.screen.get_width():
  125. self.boxX = 0
  126. if self.boxY < 0:
  127. self.boxY = self.screen.get_height()
  128. ##PatternMovement----------------
  129.  
  130. if self.count < 600:
  131. self.caculateNextPosition()
  132. self.rect.center = (self.boxX, self.boxY)
  133. elif self.count < 600:
  134. self.caculateNextPositionEvade()
  135. self.rect.center = (self.boxX, self.boxY)
  136. else:
  137. self.count = 0
  138.  
  139. def caculateNextPosition(self):
  140. boxX = self.ship.rect.centerx
  141. boxY = self.ship.rect.centery
  142.  
  143.  
  144.  
  145.  
  146.  
  147. class EnemyShip(pygame.sprite.Sprite):
  148. def __init__(self,screen, (x, y), playerShip):
  149. pygame.sprite.Sprite.__init__(self)
  150. self.image = pygame.image.load("enemy.png").convert()
  151. self.image = self.image.convert()
  152. tranColor = self.image.get_at((1, 1))
  153. self.image.set_colorkey(tranColor)
  154. self.rect = self.image.get_rect()
  155. self.enemyX = float(x)
  156. self.enemyY = float(y)
  157. self.ship = playerShip
  158. self.count = 0
  159. self.currentState2 = 0
  160. self.screen = screen
  161.  
  162.  
  163. ptu = ControlData()
  164. ptu.moveX = 5
  165. #ptu.moveY = 5
  166.  
  167. ptd = ControlData()
  168. ptd.moveX = 5
  169. # ptd.moveY = -5
  170.  
  171. self.pattern = [ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu, ptu,
  172. ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd, ptd,]
  173.  
  174. self.patternIndex = 0
  175.  
  176. def update(self):
  177. ##PatternMovement----------------
  178.  
  179. posX=self.enemyX
  180. posY=self.enemyY
  181. cd=self.pattern[self.patternIndex]
  182. self.enemyX+=cd.moveX
  183. self.enemyY+=cd.moveY
  184. self.patternIndex += 1
  185. if self.patternIndex >= len(self.pattern):
  186. self.patternIndex = 0
  187.  
  188.  
  189. self.count += 1
  190. x_component = self.ship.rect.centerx-self.enemyX
  191. y_component = self.ship.rect.centery-self.enemyY
  192. distance = math.hypot(x_component, y_component)##If player comes within radius of 100,intiate chase state
  193.  
  194. #So the spaceship won't go outside the screen
  195. if self.enemyX > self.screen.get_width():
  196. self.enemyX = 0
  197. if self.enemyY < 0:
  198. self.enemyY = self.screen.get_height()
  199. ##PatternMovement----------------
  200. if distance > 200 and self.enemyY !=68:
  201. self.enemyY+=1
  202. if self.enemyY>self.screen.get_height():
  203. self.enemyY=68
  204.  
  205. if distance < 200:
  206. self.currentState2 = 1
  207.  
  208. elif distance > 200:
  209. self.currentState2 = 0
  210.  
  211. if self.count < 600:
  212. self.caculateNextPosition()
  213. self.rect.center = (self.enemyX, self.enemyY)
  214. elif self.count < 600:
  215. self.caculateNextPositionEvade()
  216. self.rect.center = (self.enemyX, self.enemyY)
  217. else:
  218. self.count = 0
  219.  
  220. def caculateNextPosition(self): ##Fastest Spaceship
  221. shipX = self.ship.rect.centerx
  222. shipY = self.ship.rect.centery
  223. if self.currentState2==1:
  224. if self.enemyY < shipY:
  225. self.enemyY += 3
  226. elif self.enemyY > shipY:
  227. self.enemyY -= 3
  228.  
  229. if self.enemyX < shipX:
  230. self.enemyX += 3
  231. elif self.enemyX > shipX:
  232. self.enemyX -= 3
  233. elif self.currentState2==0:
  234. if self.enemyY < shipY:
  235. self.enemyY += 0
  236. elif self.enemyY > shipY:
  237. self.enemyY -= 0
  238.  
  239. if self.enemyX < shipX:
  240. self.enemyX += 0
  241. elif self.enemyX > shipX:
  242. self.enemyX -= 0
  243.  
  244. class EnemyShip2(pygame.sprite.Sprite):
  245. def __init__(self,screen, (x, y), playerShip):
  246. pygame.sprite.Sprite.__init__(self)
  247. self.image = pygame.image.load("enemy.png")
  248. self.image = self.image.convert()
  249. tranColor = self.image.get_at((1, 1))
  250. self.image.set_colorkey(tranColor)
  251. self.rect = self.image.get_rect()
  252. self.enemy2X = float(x)
  253. self.enemy2Y = float(y)
  254. self.ship = playerShip
  255. self.count = 0
  256. self.currentState = 0
  257.  
  258.  
  259.  
  260.  
  261. def states(self):
  262. 'Defining States'
  263. ## if self.currentState = 1
  264.  
  265. ## elif self.currentState = 2
  266.  
  267. # if self.EN2State == self.idle
  268.  
  269.  
  270.  
  271.  
  272.  
  273. def update(self):
  274. test = False
  275. self.count += 1
  276. x_component = self.ship.rect.centerx-self.enemy2X
  277. y_component = self.ship.rect.centery-self.enemy2Y
  278. distance = math.hypot(x_component, y_component)
  279. if distance < 200:
  280. self.currentState = 1
  281.  
  282. elif distance > 200:
  283. self.currentState = 0
  284.  
  285. if self.count < 600:
  286. self.caculateNextPosition()
  287. self.rect.center = (self.enemy2X, self.enemy2Y)
  288. elif self.count < 600:
  289. self.caculateNextPositionEvade()
  290. self.rect.center = (self.enemy2X, self.enemy2Y)
  291. else:
  292. self.count = 0
  293.  
  294. def caculateNextPosition(self):
  295. shipX = self.ship.rect.centerx
  296. shipY = self.ship.rect.centery
  297.  
  298. if self.currentState==1:
  299. if self.enemy2Y < shipY:
  300. self.enemy2Y += 2
  301. elif self.enemy2Y > shipY:
  302. self.enemy2Y -= 2
  303.  
  304. if self.enemy2X < shipX:
  305. self.enemy2X += 2
  306. elif self.enemy2X > shipX:
  307. self.enemy2X -= 2
  308. elif self.currentState==0:
  309. if self.enemy2Y < shipY:
  310. self.enemy2Y += 0
  311. elif self.enemy2Y > shipY:
  312. self.enemy2Y -= 0
  313.  
  314. if self.enemy2X < shipX:
  315. self.enemy2X += 0
  316. elif self.enemy2X > shipX:
  317. self.enemy2X -= 0
  318.  
  319. class PlayerShip(pygame.sprite.Sprite):
  320. def __init__(self, screen, (x, y)):
  321. pygame.sprite.Sprite.__init__(self)
  322. self.screen = screen
  323. self.image = pygame.image.load("player.png")
  324. self.image = self.image.convert()
  325. tranColor = self.image.get_at((1, 1))
  326. self.image.set_colorkey(tranColor)
  327. self.rect = self.image.get_rect()
  328. self.rect.center = (x, y)
  329. self.health = 100
  330.  
  331.  
  332. def update(self):
  333. x_pos =self.rect.centerx
  334. y_pos =self.rect.centery
  335. fire = False
  336.  
  337. keys = pygame.key.get_pressed()
  338. if keys[pygame.K_LEFT]:
  339. self.rect.centerx -= 4
  340. if keys[pygame.K_RIGHT]:
  341. self.rect.centerx += 4
  342. if keys[pygame.K_UP]:
  343. self.rect.centery -= 4
  344. if keys[pygame.K_DOWN]:
  345. self.rect.centery += 4
  346. #if keys[pygame.K_SPACE]:
  347. # print 'fire'
  348.  
  349. if self.rect.centerx > self.screen.get_width():
  350. self.rect.centerx = 0
  351. if self.rect.centerx < 0:
  352. self.rect.centerx = self.screen.get_width()
  353. if self.rect.centery > self.screen.get_height():
  354. self.rect.centery = 0
  355. if self.rect.centery < 0:
  356. self.rect.centery = self.screen.get_height()
  357.  
  358.  
  359.  
  360. def main():
  361.  
  362.  
  363.  
  364.  
  365. screen = pygame.display.set_mode((1000, 620))
  366. pygame.display.set_caption("Space blaster")
  367.  
  368. background = pygame.Surface(screen.get_size())
  369. background.fill((0, 0, 0))
  370. screen.blit(background, (0, 0))
  371.  
  372. playerShip = PlayerShip(screen, (800,600))
  373. enemyShip1 = EnemyShip(screen,(100, 68), playerShip)
  374. enemyShip2 = EnemyShip2(screen,(300, 30),playerShip)
  375. Box1 = Box(screen,(100,40),playerShip)
  376. Missle1 = Missle(screen,(800,100),playerShip)
  377.  
  378.  
  379.  
  380. enemySprites = pygame.sprite.Group(enemyShip1, enemyShip2)
  381. allSprites = pygame.sprite.Group(playerShip, enemyShip1, enemyShip2,Box1,Missle1)
  382.  
  383.  
  384. clock = pygame.time.Clock()
  385. b1 = "space_background.gif"
  386. back = pygame.image.load(b1).convert()
  387.  
  388. screenHeight = back.get_rect().height
  389. h = 0
  390. keepGoing = True
  391.  
  392. while keepGoing:
  393. clock.tick(60)
  394. if h >= screenHeight:
  395. h = 0
  396. screen.blit(back, (0,h))
  397. screen.blit(back, (0,h-screenHeight))
  398. h = h + 5 # faster move
  399.  
  400. # Handle Events
  401. for event in pygame.event.get():
  402. if event.type == pygame.QUIT:
  403. keepGoing = False
  404.  
  405. # Update the sprite
  406. # Clear the 'screen' of all of the sprites using the background surface
  407. allSprites.clear(screen, background)
  408. # Call all the sprites update methods
  409. allSprites.update()
  410. # Draw the sprites to the screen
  411. allSprites.draw(screen)
  412. # Update the full display Surface to the screen
  413. pygame.display.flip()
  414.  
  415. if __name__ == "__main__":
  416. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement