Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. # My name
  2. # Final project for ECE102
  3. # Started 9/05/2016 Due 12/08/2016
  4.  
  5. import time, pygame, random, sys, os, logging
  6. from player import Player
  7. from pygame.locals import *
  8. logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
  9. logging.disable(logging.DEBUG) # Disable annoying logging
  10. logging.disable(logging.INFO) # Disable all logging
  11. #os.environ['SDL_VIDEO_CENTERED'] = '1' # Center game window
  12.  
  13. FPS = 30
  14. WINDOW_WIDTH = 1024
  15. WINDOW_HEIGHT = 720
  16.  
  17. # Store player keypresses for movement, used in checkForEvents()
  18. playerMoveUp = False
  19. playerMoveDown = False
  20. playerMoveRight = False
  21. playerMoveLeft = False
  22.  
  23. # Color R G B
  24. BLACK = ( 0, 0, 0)
  25.  
  26.  
  27. def main():
  28. global FPSCLOCK, DISPLAY
  29.  
  30. pygame.init()
  31. FPSCLOCK = pygame.time.Clock()
  32. DISPLAY = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
  33. pygame.display.set_caption('ECE102 Final Project')
  34.  
  35.  
  36. # Create Player object
  37. player = Player(DISPLAY, WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
  38.  
  39. while True: # Game loop
  40.  
  41. # Deal with all events, including quit
  42. checkForEvents()
  43.  
  44. # Clear the screen before redrawing frame
  45. DISPLAY.fill(BLACK)
  46.  
  47. # Move then redraw player
  48. player.move(playerMoveUp, playerMoveLeft, playerMoveDown, playerMoveRight)
  49. player.draw()
  50. logging.debug('Player coordinates: (%s, %s)' % (str(player.getLeftX()), str(player.getTopY())))
  51.  
  52. # Update game window then tick
  53. pygame.display.update()
  54. FPSCLOCK.tick(FPS)
  55.  
  56.  
  57. # Quit pygame and close the window
  58. def terminate():
  59. logging.info('Terminating program')
  60. pygame.display.quit() # Close window faster
  61. pygame.quit()
  62. sys.exit()
  63.  
  64. # Event handling loop
  65. def checkForEvents():
  66. global playerMoveUp, playerMoveLeft, playerMoveDown, playerMoveRight
  67. for event in pygame.event.get(): # Any unhandled events are dropped
  68.  
  69. if event.type == QUIT:
  70. terminate()
  71.  
  72. elif event.type == KEYDOWN:
  73. if event.key == K_ESCAPE:
  74. terminate()
  75. elif event.key in (K_w, K_UP):
  76. playerMoveUp = True
  77. logging.info('K_w down')
  78. elif event.key in (K_a, K_LEFT):
  79. playerMoveLeft = True
  80. logging.info('K_a down')
  81. elif event.key in (K_s, K_DOWN):
  82. playerMoveDown = True
  83. logging.info('K_s down')
  84. elif event.key in (K_d, K_RIGHT):
  85. playerMoveRight = True
  86. logging.info('K_d down')
  87.  
  88. elif event.type == KEYUP:
  89. if event.key in (K_w, K_UP):
  90. playerMoveUp = False
  91. logging.info('K_w up')
  92. elif event.key in (K_a, K_LEFT):
  93. playerMoveLeft = False
  94. logging.info('K_a up')
  95. elif event.key in (K_s, K_DOWN):
  96. playerMoveDown = False
  97. logging.info('K_s up')
  98. elif event.key in (K_d, K_RIGHT):
  99. playerMoveRight = False
  100. logging.info('K_d up')
  101.  
  102.  
  103. if __name__ == '__main__':
  104. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement