Advertisement
Guest User

Untitled

a guest
Jul 13th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. import pygame, random, sys
  2. from pygame.locals import *
  3.  
  4. WINDOWWIDTH = 600
  5. WINDOWHEIGHT = 600
  6. TEXTCOLOR = (255, 255, 255)
  7. WHITE = (0, 0, 255)
  8. BGC = (0, 0, 0)
  9. FPS = 80
  10. PLAYERMOVERATE = 5
  11. countBy = 1
  12. ddd = random.randint(2, 4)
  13. dddd = random.randint(5, 7)
  14. plip = random.randint(ddd, dddd)
  15. ddddd = random.randint(-4, -2)
  16. dddddd = random.randint(-7, -5)
  17. pliping = random.randint(dddddd, ddddd)
  18.  
  19.  
  20. def terminate():
  21. pygame.quit()
  22. sys.exit()
  23.  
  24. def waitForPlayerToPressKey():
  25. while True:
  26. for event in pygame.event.get():
  27. if event.type == QUIT:
  28. terminate()
  29. if event.type == KEYDOWN:
  30. if event.key == K_ESCAPE:
  31. terminate()
  32. return
  33.  
  34. def drawText(text, font, surface, x, y):
  35. textobj = font.render(text, 1, TEXTCOLOR)
  36. textrect = textobj.get_rect()
  37. textrect.topleft = (x, y)
  38. surface.blit(textobj, textrect)
  39.  
  40. pygame.init()
  41. pygame.font.init()
  42. font = pygame.font.SysFont(None,48)
  43. mainClock = pygame.time.Clock()
  44. background = pygame.image.load('sb.png')
  45. backgroundRect = background.get_rect()
  46. size = (width, height) = background.get_size()
  47. screen = pygame.display.set_mode(size)
  48. screen.blit(background, backgroundRect)
  49. pygame.display.set_caption('Mothers Day')
  50. pygame.mouse.set_visible(True)
  51. pygame.display.set_icon(pygame.image.load('gameicon.gif'))
  52. icon = pygame.image.load('icon.bmp').convert_alpha()
  53. pygame.display.set_icon(icon)
  54. gameStartSound = pygame.mixer.Sound('gamestart.wav')
  55. gameOverSound = pygame.mixer.Sound('gamestart.wav')
  56. pygame.mixer.music.load('background.wav')
  57.  
  58. playerImage = pygame.image.load('starship.bmp')
  59. playerRect = playerImage.get_rect()
  60. baddieImage = pygame.image.load('enemy.bmp')
  61. baddieRect = baddieImage.get_rect()
  62.  
  63. drawText('Star Trek', font, screen, (WINDOWWIDTH / 2.5), (WINDOWHEIGHT / 3))
  64. drawText('Press a key to start.', font, screen, (WINDOWWIDTH / 3.75), (WINDOWHEIGHT / 3) + 50)
  65. pygame.display.update()
  66. waitForPlayerToPressKey()
  67. MESH = ((playerRect.topright[1] + 1), (playerRect.topright[1] + 1), (playerRect.centery - 10), (playerRect.centery + 10))
  68. shootRect = pygame.draw.rect(screen, WHITE, MESH)
  69.  
  70.  
  71. def baddieHasHitShoot(baddieRect, shootRect):
  72. if baddieRect.colliderect(shootRect):
  73. return True
  74. return False
  75.  
  76. topScore = 0
  77. while True:
  78.  
  79. score = 10000
  80. playerRect.topleft = (0, 0)
  81. baddieRect.topright = (600, 0)
  82. moveUp = moveDown = False
  83. pygame.mixer.music.play(-1, 0.0)
  84.  
  85. UPDOWN = False
  86. while True:
  87. if UPDOWN:
  88. baddieRect.move_ip(0, pliping)
  89. elif not UPDOWN:
  90. baddieRect.move_ip(0, plip)
  91. if baddieRect.bottom > WINDOWHEIGHT:
  92. UPDOWN = True
  93. elif baddieRect.top < 0:
  94. UPDOWN = False
  95.  
  96. score -= countBy
  97.  
  98. for event in pygame.event.get():
  99. if event.type == QUIT:
  100. terminate()
  101.  
  102. if event.type == KEYDOWN:
  103. if event.key == K_UP:
  104. moveDown = False
  105. moveUp = True
  106. if event.key == ord(' '):
  107. pygame.draw(shootRect)
  108. move.ip[5, 0]
  109. if event.key == K_DOWN:
  110. moveUp = False
  111. moveDown = True
  112.  
  113. if event.type == KEYUP:
  114. if event.key == K_ESCAPE:
  115. terminate()
  116. if event.key == K_UP or event.key == ord('w'):
  117. moveUp = False
  118. if event.key == K_DOWN or event.key == ord('s'):
  119. moveDown = False
  120.  
  121. if moveUp and playerRect.top > 0:
  122. playerRect.move_ip(0, -1 * PLAYERMOVERATE)
  123. if moveDown and playerRect.bottom < WINDOWHEIGHT:
  124. playerRect.move_ip(0, PLAYERMOVERATE)
  125.  
  126. screen.blit(background, backgroundRect)
  127. screen.blit(playerImage, playerRect)
  128. screen.blit(baddieImage, baddieRect)
  129. drawText('Score: %s' % (score), font, screen, 200, 0)
  130. drawText('Top Score: %s' % (topScore), font, screen, 195, 40)
  131. pygame.display.update()
  132.  
  133. if baddieHasHitShoot(baddieRect, shootRect):
  134. if score > topScore:
  135. score = topscore
  136. break
  137. mainClock.tick(FPS)
  138.  
  139. pygame.mixer.music.stop()
  140. gameOverSound.play()
  141. drawText('GAME OVER', font, screen, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
  142. drawText('Press a key to play again.', font, screen, (WINDOWWIDTH / 3) - 100, (WINDOWHEIGHT / 3) + 50)
  143. pygame.display.update()
  144. waitForPlayerToPressKey()
  145. gameOverSound.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement