Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. # Wormy (a Nibbles clone)
  2. # By Al Sweigart al@inventwithpython.com
  3. # http://inventwithpython.com/pygame
  4. # Released under a "Simplified BSD" license
  5.  
  6. #KRT 14/06/2012 modified Start Screen and Game Over screen to cope with mouse events
  7. #KRT 14/06/2012 Added a non-busy wait to Game Over screen to reduce processor loading from near 100%
  8.  
  9. #
  10. # Modified by Grant Clarke
  11. # g.clarke@abertay.ac.uk
  12. #
  13. #
  14.  
  15. import random, sys
  16. import pygame, math
  17. from pygame.locals import *
  18.  
  19. FPS = 60
  20. WINDOWWIDTH = 640
  21. WINDOWHEIGHT = 480
  22.  
  23. # R G B
  24. WHITE = (255, 255, 255)
  25. BLACK = ( 0, 0, 0)
  26. RED = (255, 0, 0)
  27. GREEN = ( 0, 255, 0)
  28. DARKGREEN = ( 0, 155, 0)
  29. DARKGRAY = ( 40, 40, 40)
  30. BGCOLOR = BLACK
  31.  
  32. UP = 'up'
  33. DOWN = 'down'
  34. LEFT = 'left'
  35. RIGHT = 'right'
  36.  
  37. #
  38. # GLOBAL VARIABLES used by multiple functions
  39. #
  40. # Set a random start point.
  41. gStartx = random.randint(0, WINDOWWIDTH)
  42. gStarty = random.randint(5, WINDOWHEIGHT)
  43. gPlayerCoords = {'x': gStartx, 'y': gStarty}
  44. gPlayerVelocity = {'x': 0, 'y': 0}
  45. gPlayerAcceleration = 0.4
  46. gPlayerSpaceResistanceMult = 0.98 #space resistance is like air resistance, but in space
  47. gPlayerAngle = 0;
  48. gPlayerAngleStep = 0.06; #amount to increase angle by each tick the left/right key is held, in degrees
  49. #
  50. #
  51. #
  52. def main():
  53. init()
  54. showStartScreen()
  55. while True:
  56. runGame()
  57. showGameOverScreen()
  58.  
  59. def init():
  60. global FPSCLOCK, DISPLAYSURF, BASICFONT
  61.  
  62. pygame.init()
  63. FPSCLOCK = pygame.time.Clock()
  64. DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
  65. BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
  66. pygame.display.set_caption('Assteroids')
  67.  
  68. def runGame():
  69. game_init()
  70. while True: # main game loop
  71. game_over = game_update()
  72. game_render()
  73. if game_over == True:
  74. return # game over, stop running the game
  75.  
  76.  
  77. def game_init():
  78. # Set a random start point.
  79. gStartx = random.randint(10, WINDOWWIDTH -5)
  80. gStarty = random.randint(10, WINDOWHEIGHT -5)
  81. gPlayerCoords = {'x': gStartx, 'y': gStarty}
  82. gPlayerVelocity = {'x': 0, 'y': 0}
  83.  
  84.  
  85. def game_update():
  86. global gPlayerCoords, gPlayerVelocity, gPlayerAcceleration, gPlayerSpaceResistanceMult, gPlayerAngle, gPlayerAngleStep
  87.  
  88. for event in pygame.event.get(): # event handling loop
  89. if event.type == QUIT:
  90. terminate()
  91. elif event.type == KEYDOWN:
  92. if event.key == K_ESCAPE:
  93. terminate()
  94.  
  95. keys = pygame.key.get_pressed()
  96. if keys[K_LEFT]:
  97. gPlayerAngle -= gPlayerAngleStep
  98. if keys[K_UP]:
  99. gPlayerVelocity['y'] += gPlayerAcceleration*math.sin(gPlayerAngle);
  100. gPlayerVelocity['x'] += gPlayerAcceleration*math.cos(gPlayerAngle);
  101. if keys[K_RIGHT]:
  102. gPlayerAngle += gPlayerAngleStep
  103. if keys[K_DOWN]:
  104. gPlayerVelocity['y'] -= gPlayerAcceleration*math.sin(gPlayerAngle);
  105. gPlayerVelocity['x'] -= gPlayerAcceleration*math.cos(gPlayerAngle);
  106.  
  107. gPlayerVelocity['x'] *= gPlayerSpaceResistanceMult
  108. gPlayerVelocity['y'] *= gPlayerSpaceResistanceMult
  109.  
  110. gPlayerCoords['x'] += gPlayerVelocity['x']
  111. gPlayerCoords['y'] += gPlayerVelocity['y']
  112.  
  113. if gPlayerCoords['x'] > WINDOWWIDTH:
  114. gPlayerCoords['x'] -= WINDOWWIDTH
  115. if gPlayerCoords['x'] < 0:
  116. gPlayerCoords['x'] += WINDOWWIDTH
  117. if gPlayerCoords['y'] > WINDOWHEIGHT:
  118. gPlayerCoords['y'] -= WINDOWHEIGHT
  119. if gPlayerCoords['y'] < 0:
  120. gPlayerCoords['y'] += WINDOWHEIGHT
  121.  
  122. # game is not over, return False
  123. return False
  124.  
  125. def game_render():
  126. DISPLAYSURF.fill(BGCOLOR)
  127. drawPlayer(gPlayerCoords, gPlayerVelocity)
  128. pygame.display.update()
  129. FPSCLOCK.tick(FPS)
  130.  
  131. def drawPressKeyMsg():
  132. pressKeySurf = BASICFONT.render('Press a key to play.', True, DARKGRAY)
  133. pressKeyRect = pressKeySurf.get_rect()
  134. pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
  135. DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
  136.  
  137. # KRT 14/06/2012 rewrite event detection to deal with mouse use
  138. def checkForKeyPress():
  139. for event in pygame.event.get():
  140. if event.type == QUIT: #event is quit
  141. terminate()
  142. elif event.type == KEYDOWN:
  143. if event.key == K_ESCAPE: #event is escape key
  144. terminate()
  145. else:
  146. return event.key #key found return with it
  147. # no quit or key events in queue so return None
  148. return None
  149.  
  150.  
  151. def showStartScreen():
  152. titleFont = pygame.font.Font('freesansbold.ttf', 100)
  153. titleSurf1 = titleFont.render('Assteroids!', True, WHITE, DARKGREEN)
  154. titleSurf2 = titleFont.render('Assteroids!', True, GREEN)
  155.  
  156. degrees1 = 0
  157. degrees2 = 0
  158.  
  159. #KRT 14/06/2012 rewrite event detection to deal with mouse use
  160. pygame.event.get() #clear out event queue
  161.  
  162. while True:
  163. DISPLAYSURF.fill(BGCOLOR)
  164. rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
  165. rotatedRect1 = rotatedSurf1.get_rect()
  166. rotatedRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
  167. DISPLAYSURF.blit(rotatedSurf1, rotatedRect1)
  168.  
  169. rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2)
  170. rotatedRect2 = rotatedSurf2.get_rect()
  171. rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
  172. DISPLAYSURF.blit(rotatedSurf2, rotatedRect2)
  173.  
  174. drawPressKeyMsg()
  175. #KRT 14/06/2012 rewrite event detection to deal with mouse use
  176. if checkForKeyPress():
  177. return
  178. pygame.display.update()
  179. FPSCLOCK.tick(FPS)
  180. degrees1 += 3 # rotate by 3 degrees each frame
  181. degrees2 += 7 # rotate by 7 degrees each frame
  182.  
  183.  
  184. def terminate():
  185. pygame.quit()
  186. sys.exit()
  187.  
  188.  
  189. def getRandomLocation():
  190. return {'x': random.randint(0, CELLWIDTH - 1), 'y': random.randint(0, CELLHEIGHT - 1)}
  191.  
  192.  
  193. def showGameOverScreen():
  194. gameOverFont = pygame.font.Font('freesansbold.ttf', 150)
  195. gameSurf = gameOverFont.render('Game', True, WHITE)
  196. overSurf = gameOverFont.render('Over', True, WHITE)
  197. gameRect = gameSurf.get_rect()
  198. overRect = overSurf.get_rect()
  199. gameRect.midtop = (WINDOWWIDTH / 2, 10)
  200. overRect.midtop = (WINDOWWIDTH / 2, gameRect.height + 10 + 25)
  201.  
  202. DISPLAYSURF.blit(gameSurf, gameRect)
  203. DISPLAYSURF.blit(overSurf, overRect)
  204. drawPressKeyMsg()
  205. pygame.display.update()
  206. pygame.time.wait(500)
  207. #KRT 14/06/2012 rewrite event detection to deal with mouse use
  208. pygame.event.get() #clear out event queue
  209. while True:
  210. if checkForKeyPress():
  211. return
  212. #KRT 12/06/2012 reduce processor loading in gameover screen.
  213. pygame.time.wait(100)
  214.  
  215. def drawScore(score):
  216. scoreSurf = BASICFONT.render('Score: %s' % (score), True, WHITE)
  217. scoreRect = scoreSurf.get_rect()
  218. scoreRect.topleft = (WINDOWWIDTH - 120, 10)
  219. DISPLAYSURF.blit(scoreSurf, scoreRect)
  220.  
  221.  
  222. def drawPlayer(gPlayerCoords, gPlayerVelocity):
  223. x = gPlayerCoords['x']
  224. y = gPlayerCoords['y']
  225. Rect = pygame.Rect(x, y, 5, 5)
  226. pygame.draw.rect(DISPLAYSURF, DARKGREEN, Rect)
  227.  
  228.  
  229. if __name__ == '__main__':
  230. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement