Re: Keeping track of state without globals?
3
import logging
import sys
import os
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide" import pygame import XInput as xin from cytolk import tolk from pygame.locals import * logging.basicConfig( filename="controller_info.log", level=logging.DEBUG, format="%(asctime)s %(levelname)s:\t%(message)s", datefmt="%m/%d/%Y %I:%M%p", ) def buzz(state): if state: xin.set_vibration(0, 0, 1.0) else: xin.set_vibration(0, 0, 0) def setup_speech(): logging.info("Initializing Tolk") tolk.load() tolk.try_sapi(True) sr = tolk.detect_screen_reader() if not sr: tolk.prefer_sapi(True) logging.warn("No screen reader detected, falling back to SAPI.") else: logging.info("Proceeding with %s", sr) def get_battery(controller): battinfo = xin.get_battery_information(controller) if battinfo[0] == "DISCONNECTED": battstring = "there is no battery present" else: battstring = f"the battery is of type {battinfo[0]} and is operating at a {battinfo[1]} level of charge" return battstring def main(): logging.info("Initializing pygame") pygame.init() screen = pygame.display.set_mode((400, 200)) pygame.display.set_caption("Controller Information") background = "" background = "" background.fill((250, 250, 250)) clock = pygame.time.Clock() setup_speech() logging.info("Entering main loop") while True: for event in pygame.event.get(): if ( event.type == QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE ): tolk.speak("Exiting") pygame.time.delay(1000) logging.info("Exiting program") return for inevent in xin.get_events(): if inevent.type == xin.EVENT_CONNECTED: tolk.speak( f"Controller {inevent.user_index + 1} has been connected. {get_battery(inevent.user_index)}" ) if inevent.type == xin.EVENT_DISCONNECTED: tolk.speak( f"Controller {inevent.user_index + 1} has been disconnected." ) if inevent.type == xin.EVENT_BUTTON_PRESSED and inevent.button == "A": buzz(True) elif inevent.type == xin.EVENT_BUTTON_RELEASED and inevent.button == "A": buzz(False) screen.blit(background, (0, 0)) pygame.display.flip() clock.tick(60) if __name__ == "__main__": main()
-- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector