Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # My name
- # Final project for ECE102
- # Started 9/05/2016 Due 12/08/2016
- import time, pygame, random, sys, os, logging
- from player import Player
- from pygame.locals import *
- logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
- logging.disable(logging.DEBUG) # Disable annoying logging
- logging.disable(logging.INFO) # Disable all logging
- #os.environ['SDL_VIDEO_CENTERED'] = '1' # Center game window
- FPS = 30
- WINDOW_WIDTH = 1024
- WINDOW_HEIGHT = 720
- # Store player keypresses for movement, used in checkForEvents()
- playerMoveUp = False
- playerMoveDown = False
- playerMoveRight = False
- playerMoveLeft = False
- # Color R G B
- BLACK = ( 0, 0, 0)
- def main():
- global FPSCLOCK, DISPLAY
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- DISPLAY = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
- pygame.display.set_caption('ECE102 Final Project')
- # Create Player object
- player = Player(DISPLAY, WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)
- while True: # Game loop
- # Deal with all events, including quit
- checkForEvents()
- # Clear the screen before redrawing frame
- DISPLAY.fill(BLACK)
- # Move then redraw player
- player.move(playerMoveUp, playerMoveLeft, playerMoveDown, playerMoveRight)
- player.draw()
- logging.debug('Player coordinates: (%s, %s)' % (str(player.getLeftX()), str(player.getTopY())))
- # Update game window then tick
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- # Quit pygame and close the window
- def terminate():
- logging.info('Terminating program')
- pygame.display.quit() # Close window faster
- pygame.quit()
- sys.exit()
- # Event handling loop
- def checkForEvents():
- global playerMoveUp, playerMoveLeft, playerMoveDown, playerMoveRight
- for event in pygame.event.get(): # Any unhandled events are dropped
- if event.type == QUIT:
- terminate()
- elif event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- terminate()
- elif event.key in (K_w, K_UP):
- playerMoveUp = True
- logging.info('K_w down')
- elif event.key in (K_a, K_LEFT):
- playerMoveLeft = True
- logging.info('K_a down')
- elif event.key in (K_s, K_DOWN):
- playerMoveDown = True
- logging.info('K_s down')
- elif event.key in (K_d, K_RIGHT):
- playerMoveRight = True
- logging.info('K_d down')
- elif event.type == KEYUP:
- if event.key in (K_w, K_UP):
- playerMoveUp = False
- logging.info('K_w up')
- elif event.key in (K_a, K_LEFT):
- playerMoveLeft = False
- logging.info('K_a up')
- elif event.key in (K_s, K_DOWN):
- playerMoveDown = False
- logging.info('K_s up')
- elif event.key in (K_d, K_RIGHT):
- playerMoveRight = False
- logging.info('K_d up')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement