def button(msg, x, y, w, h, ic, ac, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x + w > mouse[0] > x and y + h > mouse[1] > y: pygame.draw.rect(gameDisplay, ac, (x, y, w, h)) if click[0] == 1 and action != None: action() else: pygame.draw.rect(gameDisplay, ic, (x, y, w, h)) smallText = pygame.font.SysFont("comicsansms", 20) textSurf, textRect = text_objects(msg, smallText) textRect.center = ((x + (w / 2)), (y + (h / 2))) gameDisplay.blit(textSurf, textRect)
def game_loop(): global pause # Start the game play music # green.write(1) light(lights["green"], on) pygame.mixer.music.stop() pygame.mixer.music.load(gamePlayMusic) pygame.mixer.music.play(-1) # Background gameDisplay.blit(spaceShip, (0, 0)) pygame.display.update() dodged = clock.tick() # TODO: Play clock at dodged = 0... # TODO: Add time limit to game. gameExit = False while not gameExit: # Keyboard logic for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: pause = True paused() if event.key == pygame.K_h: fail() if event.key == pygame.K_g: success() # Button box logic if buttonsPressed(["blue"]): success() if buttonsPressed(["yellow"]): fail() if buttonsPressed(["restart"]): pause = True paused() pygame.display.update() clock.tick(60)
def game_intro(): intro = True startMusicPlay = False while intro: # Abilty to quite the game for event in pygame.event.get(): # print(event) if event.type == pygame.QUIT: pygame.quit() quit() # Start into music while not startMusicPlay: pygame.mixer.music.load(introMusic) pygame.mixer.music.play(-1) startMusicPlay = True # Background and title gameDisplay.blit(stars, (0, 0)) largeText = pygame.font.SysFont("comicsansms", 250) TextSurf, TextRect = text_objects("Zulu", largeText) TextRect.center = ((DISPLAY_WIDTH * 0.5), (DISPLAY_HEIGHT * 0.3)) gameDisplay.blit(TextSurf, TextRect) button( "Play", BUTTON_CENTER_ONE_THIRD, DISPLAY_HEIGHT * 0.6, BUTTON_WIDTH, BUTTON_HEIGHT, GREEN, BRIGHT_GREEN, game_loop, ) button( "Quit", BUTTON_CENTER_TWO_THIRD, DISPLAY_HEIGHT * 0.6, BUTTON_WIDTH, BUTTON_HEIGHT, RED, BRIGHT_RED, quitgame, ) pygame.display.update() clock.tick(15)
def success(): # Start the success sounds soundSuccess.play() pygame.mixer.music.stop() logging.info("Game Success") # Display a GREEN spaceship gameDisplay.blit(spaceShipSuccess, (0, 0)) pygame.display.update() largeText = pygame.font.SysFont("comicsansms", 250) TextSurf, TextRect = text_objects("", largeText) TextRect.center = ((DISPLAY_WIDTH * 0.5), (DISPLAY_HEIGHT * 0.33)) gameDisplay.blit(TextSurf, TextRect) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() button( "Play Again", BUTTON_CENTER_ONE_THIRD, BUTTON_CENTER_VERTICAL, BUTTON_WIDTH, BUTTON_HEIGHT, GREEN, BRIGHT_GREEN, game_loop, ) button( "Quit", BUTTON_CENTER_TWO_THIRD, BUTTON_CENTER_VERTICAL, BUTTON_WIDTH, BUTTON_HEIGHT, RED, BRIGHT_RED, quitgame, ) pygame.display.update() clock.tick(15)
def paused(): ############ pygame.mixer.music.pause() ############# largeText = pygame.font.SysFont("comicsansms", 250) TextSurf, TextRect = text_objects("Paused", largeText) TextRect.center = ((DISPLAY_WIDTH * 0.5), (DISPLAY_HEIGHT * 0.33)) gameDisplay.blit(TextSurf, TextRect) while pause: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() button( "Play Again", BUTTON_CENTER_ONE_THIRD, DISPLAY_HEIGHT * 0.6, BUTTON_WIDTH, BUTTON_HEIGHT, GREEN, BRIGHT_GREEN, game_loop, ) button( "Quit", BUTTON_CENTER_TWO_THIRD, DISPLAY_HEIGHT * 0.6, BUTTON_WIDTH, BUTTON_HEIGHT, RED, BRIGHT_RED, quitgame, ) pygame.display.update() clock.tick(15)