def __init__(self, window, sceneKey): # Save window and sceneKey in instance variables self.window = window self.sceneKey = sceneKey self.backgroundImage = pygwidgets.Image(self.window, (0, 0), "images/GameOverBackground.jpg") # The following will create a list of lists # Either by building a blank one from scratch, or by reading from a text file # The result will look like: # [[name, score], [name, score], [name, score] ...] # and will always be kept in order of the score (highest to lowest) if not SceneManager.fileExists(SceneGameOver.DATA_FILE_PATH): self.setEmptyGameOver() else: data = SceneManager.readFile(SceneGameOver.DATA_FILE_PATH) # read in all the data in json format, converts to a list of lists self.scoresList = json.loads(data) self.scoresField = pygwidgets.DisplayText(self.window, (25, 84), '', \ fontSize=48, textColor=BLACK, width=175, justified='right') self.namesField = pygwidgets.DisplayText(self.window, (260, 84), '', \ fontSize=48, textColor=BLACK, width=300, justified='left') self.quitButton = pygwidgets.TextButton(self.window, (30, 650), 'Quit') self.resetScoresButton = pygwidgets.TextButton(self.window, (240, 650), 'Reset high scores') self.startNewGameButton = pygwidgets.TextButton(self.window, (450, 650), 'Start new game') self.showGameOver()
def __init__(self, window): # Save window in instance variables self.window = window self.backgroundImage = pygwidgets.Image(self.window, (0, 0), "images/gridBG.png") self.dialogImage = pygwidgets.Image(self.window, (150, 30), "images/score.jpg") self.quitButton = pygwidgets.TextButton(self.window, (270, 420), 'Quit') self.restartButton = pygwidgets.TextButton(self.window, (570, 420), 'Restart') self.clicksField = pygwidgets.DisplayText(self.window, (500, 223), '', fontSize=30) self.hitsField = pygwidgets.DisplayText(self.window, (500, 250), '', fontSize=30) self.missesField = pygwidgets.DisplayText(self.window, (500, 278), '', fontSize=30) self.missedTargetsField = pygwidgets.DisplayText(self.window, (500, 306), '', fontSize=30) self.scoreField = pygwidgets.DisplayText(self.window, (500, 362), '', fontSize=30)
def __init__(self, window, sceneKey): # Save window and sceneKey in instance variables self.window = window self.sceneKey = sceneKey # Set background self.backgroundImage = pygwidgets.Image( self.window, (0, 0), "images/GameOverBackground.jpg") # Read/load game data if not SceneManager.fileExists(SceneGameOver.DATA_FILE_PATH): pass else: data = SceneManager.readFile(SceneGameOver.DATA_FILE_PATH) # read in all the data in json format, converts to a list of lists self.scoresList = json.loads(data) self.deathCount = pygwidgets.DisplayText(self.window, (25, 84), 'Deaths: ', \ fontSize=48, textColor=DARKVIOLET, width=175, justified='right') # + str(nameScoreList[1]),\ self.deathMessage = pygwidgets.DisplayText(self.window, (260, 84), 'YOU DIED', \ fontSize=48, textColor=DARKVIOLET, width=300, justified='left') self.quitButton = pygwidgets.TextButton(self.window, (30, 650), 'Quit') self.startNewGameButton = pygwidgets.TextButton( self.window, (450, 650), 'Try Again')
def __init__(self, window): self.window = window self.blockImage = pygame.image.load('images/oneBlock.png') self.line = pygame.image.load('images/line.png') self.lowestY = 490 self.blockHeight = 30 self.towerX = 220 self.computerWins = 0 self.humanWins = 0 self.lineBottomY = self.lowestY + self.blockHeight self.lineTopY = self.lowestY - ( (MAX_BLOCKS - 1) * self.blockHeight) - 3 self.newGameButton = pygwidgets.TextButton(window, (360, 460), 'New Game', width=100, height=35) self.quitButton = pygwidgets.TextButton(window, (360, 400), 'Quit', width=100, height=35) self.oneBlockButton = pygwidgets.CustomButton(window, (35, 230), up='images/oneBlock.png',\ over='images/OneBlockOver.png',\ down='images/OneBlockDown.png',\ disabled='images/OneBlockDisabled.png') self.twoBlocksButton = pygwidgets.CustomButton(window, (35, 300), up='images/twoBlocks.png', \ over='images/TwoBlocksOver.png', \ down='images/TwoBlocksDown.png', \ disabled='images/TwoBlocksDisabled.png') self.threeBlocksButton = pygwidgets.CustomButton(window, (35, 400), up='images/threeBlocks.png', \ over='images/ThreeBlocksOver.png', \ down='images/ThreeBlocksDown.png', \ disabled='images/ThreeBlocksDisabled.png') self.nBlocksDisplay = pygwidgets.DisplayText(window, (self.towerX, 530), '0 of ' + str(MAX_BLOCKS), \ fontName='monospaces', fontSize=24, textColor=BLACK) self.messageDisplay = pygwidgets.DisplayText(window, (10, 572), '', \ fontName='monospaces', fontSize=28, textColor=BLACK) self.humanWinsDisplay = pygwidgets.DisplayText(window, (360, 230), 'Player: 0', \ fontName='monospaces', fontSize=28, textColor=BLACK) self.computerWinsDisplay = pygwidgets.DisplayText(window, (360, 300), 'Computer: 0', \ fontName='monospaces', fontSize=28, textColor=BLACK) self.humanBlip = pygame.mixer.Sound('sounds/humanBlip.wav') self.computerBlip = pygame.mixer.Sound('sounds/computerBlip.wav') self.applause = pygame.mixer.Sound('sounds/applause.wav') self.ding = pygame.mixer.Sound('sounds/ding.wav') self.reset()
def __init__(self, window, sceneKey): # Save window and sceneKey in instance variables self.window = window self.sceneKey = sceneKey pygame.mixer.music.load(MUSIC_SPLASH) pygame.mixer.music.play(-1) pygame.mixer.music.set_volume(1) self.backgroundImage = pygwidgets.Image(self.window, (0, 0), BG_SPLASH) self.logo = pygwidgets.Image(self.window, (WINDOW_WIDTH/2 - 250, SPLASH_WINDOW_HEIGHT/2 - 200), LOGO) self.startButton = pygwidgets.TextButton(self.window, (WINDOW_WIDTH/2 -50, SPLASH_WINDOW_HEIGHT/2 - 50), 'Start', \ upColor=DUSTYPURPLE, downColor=DARKVIOLET, textColor=WHITE, fontName = FONT_NAME, enterToActivate=True) self.quitButton = pygwidgets.TextButton(self.window, (WINDOW_WIDTH/2 -50, SPLASH_WINDOW_HEIGHT/2 + 50), 'Quit', \ upColor=DUSTYPURPLE, downColor=DARKVIOLET, textColor=WHITE, fontName = FONT_NAME, ) self.controlsButton = pygwidgets.TextButton(self.window, (WINDOW_WIDTH/2 -50, SPLASH_WINDOW_HEIGHT/2), 'Controls', \ upColor=DUSTYPURPLE, downColor=DARKVIOLET, textColor=WHITE, fontName = FONT_NAME,)
def __init__(self, window): """ This method is called when the scene is created Create and/or load any assets (images, buttons, sounds) that you need for this scene """ self.window = window # As a sample, let's create a button self.navButton = pygwidgets.TextButton(self.window, (300, 230), 'Navigate')
def __init__(self, window): # Save window in instance variable self.window = window self.backgroundImage = pygwidgets.Image(self.window, (0, 0), "images/gridBG.png") self.dialogImage = pygwidgets.Image(self.window, (150, 30), "images/splash.jpg") self.startButton = pygwidgets.TextButton(self.window, (430, 380), 'Start')
def __init__(self, window, sceneKey): # Save window and sceneKey in instance variables self.window = window self.sceneKey = sceneKey self.messageField = pygwidgets.DisplayText(self.window, (15, 25), 'Welcome to Turtle-Hare Race!', \ fontSize=50, textColor=Constants.GRAYA, width=610, justified='center') self.oEnterButton = pygwidgets.TextButton(self.window, (250, 100), 'Enter') self.hare = pygwidgets.Image(window, (150, 100), 'images/hare.gif') self.turtle = pygwidgets.Image(window, (390, 100), 'images/turtle.gif') self.oRace = RaceWidgets.Racing
def __init__(self, window, myLetter, theLeft, theTop, theWidth, theHeight): self.window = window self.myLetter = myLetter self.loc = (theLeft, theTop) self.myRect = pygame.Rect(theLeft, theTop, theWidth, theHeight) self.oButton = pygwidgets.TextButton(window, self.loc, self.myLetter, width=theWidth, height=theHeight, fontSize=24) self.available = True self.reset()
def __init__(self, window): ''' This method is called when the scene is created Create and/or load any assets (images, buttons, sounds) that you need for this scene :param window: :param sceneKey: ''' # Save window and sceneKey in instance variables self.window = window # As a sample, let's create a button self.navButton = pygwidgets.TextButton(self.window, (300, 230), 'Navigate')
def __init__(self, window, sceneKey): # Save window and sceneKey in instance variables self.window = window self.sceneKey = sceneKey self.messageField = pygwidgets.DisplayText(self.window, (40, 55), 'Start the Race!!!', \ fontSize=30, textColor=Constants.BLUE, width=510, justified='center') self.oStartButton = pygwidgets.TextButton(self.window, (250, 100), 'Start', textColor=Constants.BLACK) self.oTurtle = Turtle() self.oHare = Turtle() self.speedFile = 'speedRate.txt' self.winnerRecordFile = 'winnerRecords.txt' self.oRace = RaceWidgets.Racing
def __init__(self, window, sceneKey): # Save window and sceneKey in instance variables self.window = window self.sceneKey = sceneKey self.messageField = pygwidgets.DisplayText(self.window, (30, 55), 'Show player informatin one by one', \ fontSize=30, textColor=Constants.PURPLE, width=510, justified='center') self.oStartButton = pygwidgets.TextButton(self.window, (250, 100), 'Show', textColor=Constants.WHITE) self.oTurtle = Turtle() self.oHare = Turtle() self.speedFile = 'speedRate.txt' self.winnerRecordFile = 'winnerRecords.txt' self.oRace = RaceWidgets.Racing
def __init__(self, window, sceneKey): # Save window and sceneKey in instance variables self.window = window self.sceneKey = sceneKey self.backgroundImage = pygwidgets.Image(self.window, (0, 0), BG_SPLASH) self.backButton = pygwidgets.TextButton(self.window, (WINDOW_WIDTH/2 - 50, SPLASH_WINDOW_HEIGHT/2 - 150), 'Back', \ upColor=DUSTYPURPLE, downColor=DARKVIOLET, textColor=WHITE,fontName=FONT_NAME, enterToActivate=True) self.moveKeys = pygwidgets.DisplayText(self.window, (WINDOW_WIDTH/2 - 70, SPLASH_WINDOW_HEIGHT/2 - 50), 'Move: Arrow Keys', \ fontSize=16, fontName = FONT_NAME,textColor=CONTROL_FONT_COLOR, justified='left') self.jumpKeys = pygwidgets.DisplayText(self.window, (WINDOW_WIDTH/2 - 70, SPLASH_WINDOW_HEIGHT/2), 'Jump: Spacebar', \ fontSize=16, fontName = FONT_NAME,textColor=CONTROL_FONT_COLOR, justified='left') self.interactKeys = pygwidgets.DisplayText(self.window, (WINDOW_WIDTH/2 - 70, SPLASH_WINDOW_HEIGHT/2 + 50), 'Interact: Enter', \ fontSize=16, fontName = FONT_NAME,textColor=CONTROL_FONT_COLOR, justified='left') self.attackKeys = pygwidgets.DisplayText(self.window, (WINDOW_WIDTH/2 - 70, SPLASH_WINDOW_HEIGHT /2 + 100), 'Attack: Mouse Left', \ fontName=FONT_NAME,fontSize=16, textColor=CONTROL_FONT_COLOR, justified='left')
def textAnswerDialog(theWindow, theRect, prompt, trueButtonText='OK',\ falseButtonText='Cancel', backgroundColor=DIALOG_BACKGROUND_COLOR): """Puts up a text-based two-button answerable modal dialog (typically Yes/No or OK/Cancel) Parameters: | theWindow - the window to draw in | theRect - the rectangle of the dialog box in the application window | prompt - prompt (title) string to be displayed in the dialog box Optional keyword parameters: | trueButtonText - text on the True button (defaults to 'OK') | falseButtonText - text on the False button (defaults to 'Cancel') | backgroundColor - rgb background color for the dialog box (defaults to (0, 200, 200)) Returns: | trueOrFalse - True means true button was pressed, False means false button was pressed | userText - if above is True, then this contains the text that the user typed. """ dialogLeft = theRect[0] dialogTop = theRect[1] dialogWidth = theRect[2] dialogHeight = theRect[3] frameRect = pygame.Rect(dialogLeft + 1, dialogTop + 1, dialogWidth - 2, dialogHeight - 2) INSET = 30 # inset buttons from the edges of the dialog box promptText = pygwidgets.DisplayText(theWindow, (dialogLeft, dialogTop + 30), prompt, fontSize=24, width=dialogWidth, justified='center') inputWidth = dialogWidth - (2 * INSET) inputText = pygwidgets.InputText(theWindow, (dialogLeft + INSET, dialogTop + 80), width=inputWidth, initialFocus=True) falseButton = pygwidgets.TextButton(theWindow, (0, 0), falseButtonText) trueButton = pygwidgets.TextButton(theWindow, (0, 0), trueButtonText) trueButtonRect = trueButton.getRect() trueButtonHeight = trueButtonRect[3] trueButtonWidth = trueButtonRect[2] # get width xPos = dialogLeft + dialogWidth - trueButtonWidth - INSET buttonsY = dialogTop + dialogHeight - trueButtonHeight - 20 falseButton.setLoc((dialogLeft + INSET, buttonsY)) trueButton.setLoc((xPos, buttonsY)) # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): if (event.type == QUIT) or \ ((event.type == KEYDOWN) and (event.key == K_ESCAPE)): pygame.quit() sys.exit() if inputText.handleEvent(event): theAnswer = inputText.getValue() return True, theAnswer if trueButton.handleEvent(event): theAnswer = inputText.getValue() return True, theAnswer if falseButton.handleEvent(event): return False, None # 8 - Do any "per frame" actions # 9 - Clear the screen area before drawing it again pygame.draw.rect(theWindow, backgroundColor, theRect) pygame.draw.rect(theWindow, DIALOG_BLACK, theRect, 1) # 10 - Draw the screen elements promptText.draw() inputText.draw() falseButton.draw() trueButton.draw() # 11 - Update the screen pygame.display.update()
def textYesNoDialog(theWindow, theRect, prompt, trueButtonText='OK', \ falseButtonText='Cancel', backgroundColor=DIALOG_BACKGROUND_COLOR): """Puts up a text-based two-button modal dialog (typically Yes/No or OK/Cancel) It can also be used to put up a single button alert dialog (typically with an OK button) Parameters: | theWindow - the window to draw in | theRect - the rectangle of the dialog box in the application window | prompt - prompt (title) string to be displayed in the dialog box Optional keyword parameters: | trueButtonText - text on the True button (defaults to 'OK') | falseButtonText - text on the False button (defaults to 'Cancel') | Note: If falseButtonText is None or the empty string, the false button will not be drawn | This way, you can present an "alert" box with only an 'OK' button | backgroundColor - rgb background color for the dialog box (defaults to (0, 200, 200)) Returns: | trueOrFalse - True means true button was pressed, False means false button was pressed """ dialogLeft = theRect[0] dialogTop = theRect[1] dialogWidth = theRect[2] dialogHeight = theRect[3] frameRect = pygame.Rect(dialogLeft + 1, dialogTop + 1, dialogWidth - 2, dialogHeight - 2) INSET = 30 # inset buttons from the edges of the dialog box promptText = pygwidgets.DisplayText(theWindow, (dialogLeft, dialogTop + 30), prompt, fontSize=24, width=dialogWidth, justified='center') # Create buttons, fix locations after finding out the size of the button(s) hideFalseButton = (falseButtonText is None) or (falseButtonText == '') showFalseButton = not hideFalseButton if showFalseButton: falseButton = pygwidgets.TextButton(theWindow, (0, 0), falseButtonText) trueButton = pygwidgets.TextButton(theWindow, (0, 0), trueButtonText) trueButtonRect = trueButton.getRect() trueButtonHeight = trueButtonRect[3] trueButtonWidth = trueButtonRect[2] # get width xPos = dialogLeft + dialogWidth - trueButtonWidth - INSET buttonsY = dialogTop + dialogHeight - trueButtonHeight - 20 if showFalseButton: falseButton.setLoc((dialogLeft + INSET, buttonsY)) trueButton.setLoc((xPos, buttonsY)) #print('In dialogYesNo') #print('theRect is', theRect) #print('frameRect is', frameRect) # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): if (event.type == QUIT) or \ ((event.type == KEYDOWN) and (event.key == K_ESCAPE)): pygame.quit() sys.exit() if showFalseButton: if falseButton.handleEvent(event): return False if trueButton.handleEvent(event): return True # 8 - Do any "per frame" actions # 9 - Clear the screen area before drawing it again pygame.draw.rect(theWindow, backgroundColor, theRect) pygame.draw.rect(theWindow, DIALOG_BLACK, frameRect, 1) # 10 - Draw the screen elements promptText.draw() if showFalseButton: falseButton.draw() trueButton.draw() # 11 - Update the screen pygame.display.update()
'Default Group', 'Radio Text 1', value=False) oRadioText2 = pygwidgets.TextRadioButton(window, (500, 360), 'Default Group', 'Radio Text 2', value=True) oRadioText3 = pygwidgets.TextRadioButton(window, (500, 400), 'Default Group', 'Radio Text 3', value=False) oStatusButton = pygwidgets.TextButton( window, (500, 430), 'Show Status', callBack=oTest.myMethod) # callBack here is not required oDragger = pygwidgets.Dragger(window, (300, 200), 'images/dragMeUp.png', 'images/dragMeDown.png', 'images/dragMeOver.png', 'images/dragMeDisabled.png', nickname='My Dragger') oPythonIcon = pygwidgets.Image(window, (15, 500), 'images/pythonIcon.png') oImageCollection = pygwidgets.ImageCollection(window, (400, 490), { 'start': 'imageStart.jpg', 'left': 'imageLeft.jpg', 'right': 'imageRight.jpg',
jackpotDisplay = pygwidgets.DisplayText(window,(530, 212), '', \ fontName='Broadway', fontSize=50, textColor=BLACKISH) betDisplay = pygwidgets.DisplayText(window, (376,710), '', \ fontName='Haettenschweiler', fontSize=30, textColor=YELLOW) coinsDisplay = pygwidgets.DisplayText(window,(610, 710), '', \ fontName='Haettenschweiler', fontSize=30, textColor=YELLOW) winningsDisplay = pygwidgets.DisplayText(window, (800, 710), '', \ fontName='Haettenschweiler', fontSize=30, textColor=YELLOW) messageDisplay = pygwidgets.DisplayText(window, (181, 870), '', \ fontName='Haettenschweiler', fontSize=35, textColor=BLACKISH) spinButton = pygwidgets.CustomButton(window,(755, 772, 108, 44), \ up='images/spinButton.png', down='images/spinButtonDown.png', \ over='images/spinButtonOver.png', disabled='images/spinButtonGray.png') bet1Button = pygwidgets.TextButton(window, (230, 785), 'Bet 1', width=108, height=44,\ soundOnClick=COIN_DROP_SOUND) up1Button = pygwidgets.CustomButton(window, (395, 766),\ up='images/upArrow.png', down='images/upArrowDown.png', \ over='images/upArrowOver.png', disabled='images/upArrowGray.png', soundOnClick=COIN_DROP_SOUND) down1Button = pygwidgets.CustomButton(window, (395, 818),\ up='images/downArrow.png', down='images/downArrowDown.png',\ over='images/downArrowOver.png', disabled='images/downArrowGray.png', soundOnClick=COIN_DOWN_SOUND) up10Button = pygwidgets.CustomButton(window, (490, 766),\ up='images/upArrow.png', down='images/upArrowDown.png', \ over='images/upArrowOver.png', disabled='images/upArrowGray.png', soundOnClick=COIN_DROP_SOUND) down10Button = pygwidgets.CustomButton(window, (490, 818),\ up='images/downArrow.png', down='images/downArrowDown.png',\ over='images/downArrowOver.png', disabled='images/downArrowGray.png',
# 2 Define constants SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 FRAMES_PER_SECOND = 30 BGCOLOR = (0, 128, 128) # 3 - Initialize the world pygame.init() window = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) clock = pygame.time.Clock() # 4 - Load assets: images(s), sounds, etc. # 5 - Initialize variables oResetButton = pygwidgets.TextButton(window, (300, 420), 'Reset star') oStar = Star(window, (280, 200)) oFrisbee = Frisbee(window, (180, 200)) oSquare = Square(window, (280, 300)) oCar = Car(window, (280, 100)) oDinosaur = Dinosaur(window, (280, 300)) myList = [oStar, oFrisbee, oSquare, oCar, oDinosaur] # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): # check if the event is the X button
nLevels = len(levelInfoDict) level = 0 # increments before level starts nextLevel = True # So we build level 1 first time through playerCol = NCOLS // 2 messageLine1 = pygwidgets.DisplayText(window, (0, 315), '', \ fontSize=40, textColor=BACKGROUND_COLOR, width=480, justified='center') timeMessage = pygwidgets.DisplayText(window, (0, 360), '', \ fontSize=80, textColor=PLAYER_COLOR, width=480, justified='center') messageLine2 = pygwidgets.DisplayText(window, (0, 435), '', \ fontSize=40, textColor=BACKGROUND_COLOR, width=480, justified='center') startButton = pygwidgets.TextButton(window, (195, 500), 'Start', enterToActivate=True) startOverButton = pygwidgets.TextButton(window, (200, 500), 'Start Over') restartLevelButton = pygwidgets.TextButton(window, (200, 560), 'Restart Current Level') quitButton = pygwidgets.TextButton(window, (200, 620), 'Quit') # # Loop Forever # while True: # Loop through events for event in pygame.event.get(): # check if quitting
self.valueText.setValue(str(self.handValue)) self.valueText.draw() # # MAIN CODE # pygame.init() clock = pygame.time.Clock() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) backgroundImage = pygame.image.load('images/background.png') hitButton = pygwidgets.TextButton(window, (22, 350), 'Hit', width=108, height=44) stayButton = pygwidgets.TextButton(window, (22, 420), 'Stay', width=108, height=44) playGameButton = pygwidgets.TextButton(window, (20, 530), 'Play Game', width=108, height=44) quitButton = pygwidgets.TextButton(window, (800, 530), 'Quit', width=108, height=44) bet1Button = pygwidgets.TextButton(window, (230, 530), 'Bet 1',
fontSize=30, textColor=GREEN) # Stuff dealing with dialog box when one round of the game is done messageDisplay = pygwidgets.DisplayText(window, (565, 290), '', \ fontSize=48, textColor=BLACK) speedDisplay = pygwidgets.DisplayText(window, (565, 340), '', \ fontSize=48, textColor=BLACK) newSoftestField = pygwidgets.DisplayText(window, (565, 390), '', \ fontSize=48, textColor=BLACK) newFastestField = pygwidgets.DisplayText(window, (565, 440), '', \ fontSize=48, textColor=BLACK) playAgainDisplay = pygwidgets.DisplayText(window, (690, 550), 'Play again?', \ fontSize=48, textColor=BLACK) startButton = pygwidgets.TextButton(window, (750, 610), 'Start', width=60, height=30) yesButton = pygwidgets.TextButton(window, (720, 610), 'Yes', width=60, height=30) noButton = pygwidgets.TextButton(window, (820, 610), 'No', width=60, height=30) DATA_FILE_PATH = 'LanderData.txt' # Data file will be made of two entries - separated by a comma: # <softestSoFar>,<fastestSoFar> if pyghelpers.fileExists(DATA_FILE_PATH): savedDataString = pyghelpers.readFile(DATA_FILE_PATH) savedDataList = savedDataString.split(',') softestSoFar = float(savedDataList[0])
# 2 - Define constants # CONSTANTS WINDOW_WIDTH = 1000 WINDOW_HEIGHT = 600 FRAMES_PER_SECOND = 30 # 3 - Initialize the world pygame.init() clock = pygame.time.Clock() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) # 4 - Load assets: image(s), sounds, etc. background = pygwidgets.Image(window, (0, 0), 'images/background.png') newGameButton = pygwidgets.TextButton(window, (20, 530), 'New Game', width=100, height=45) higherButton = pygwidgets.TextButton(window, (540, 520), 'Higher', width=120, height=55) lowerButton = pygwidgets.TextButton(window, (340, 520), 'Lower', width=120, height=55) quitButton = pygwidgets.TextButton(window, (880, 530), 'Quit', width=100, height=45) # 5 - Initialize variables
'', fontSize=30) # 5 - Initialize variables oBasket = Basket(window, WINDOW_WIDTH, WINDOW_HEIGHT) fruitList = [] fruitNames = ['apple', 'banana', 'cherry', 'grapes', 'pear', 'strawberry'] for fruitName in fruitNames: if fruitName == 'pear': fruitList.append( Fruit(window, WINDOW_WIDTH, WINDOW_HEIGHT, fruitName, -100)) else: fruitList.append(Fruit(window, WINDOW_WIDTH, WINDOW_HEIGHT, fruitName)) oRestartButton = pygwidgets.TextButton(window, (5, 5), 'Restart') score = 0 # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if oRestartButton.handleEvent(event): # ckicked on the Restart button score = 0 for oFruit in fruitList:
borderSquaresList = self.oLineMgr.mComputerTakesLine(lineNumberChoice) self.mCheckForCompletedSquares(lineNumberChoice, borderSquaresList) print('Computer chooses line number', lineNumberChoice) self.computerBlipSound.play() self.oLineMgr.mStartComputerAnimation(lineNumberChoice) def mPing(self): self.oLineMgr.mAnimate() # Main code oGame = Game(window) startButton = pygwidgets.TextButton(window, (550, 255), 'New Game', width=100, height=35) yourTurnAgainSound = buzz = pygame.mixer.Sound('sounds/yourTurnAgain.wav') while True: # Handle events for event in pygame.event.get(): if (event.type == QUIT) or ((event.type == KEYDOWN) and (event.key == K_ESCAPE)): pygame.mixer.music.stop() pygame.quit() sys.exit() if event.type == MOUSEBUTTONDOWN: mousePos = event.pos oGame.mCheckForMakingMove(mousePos)
def textAnswerDialog(theWindow, theRect, prompt, okButtonText='OK', cancelButtonText='Cancel', backgroundColor=DIALOG_BACKGROUND_COLOR, promptTextColor=DIALOG_BLACK, inputTextColor=DIALOG_BLACK): """A function that puts up a text-based two-button answerable modal dialog (typically OK/Cancel) Parameters: | theWindow - the window to draw in | theRect - the rectangle (or tuple) of the dialog box in the application window | prompt - prompt (title) string to be displayed in the dialog box Optional keyword parameters: | okButtonText - text on the OK button (defaults to 'OK') | cancelButtonText - text on the Cancel button (defaults to 'Cancel') | backgroundColor - rgb background color for the dialog box (defaults to (0, 200, 200)) | promptTextColor - rgb color of the prompt text (defaults to black) | inputTextColor - rgb color of the input text (defaults to black) Returns: | userAnswer - If user presses OK, returns the text the user typed. Otherwise returns None """ dialogLeft = theRect[0] dialogTop = theRect[1] dialogWidth = theRect[2] dialogHeight = theRect[3] INSET = 30 # inset buttons from the edges of the dialog box promptText = pygwidgets.DisplayText(theWindow, (dialogLeft, dialogTop + 30), prompt, fontSize=24, width=dialogWidth, justified='center', textColor=promptTextColor) inputWidth = dialogWidth - (2 * INSET) inputText = pygwidgets.InputText(theWindow, (dialogLeft + INSET, dialogTop + 80), width=inputWidth, initialFocus=True, textColor=inputTextColor) cancelButton = pygwidgets.TextButton(theWindow, (0, 0), cancelButtonText) okButton = pygwidgets.TextButton(theWindow, (0, 0), okButtonText) okButtonRect = okButton.getRect() okButtonHeight = okButtonRect[3] okButtonWidth = okButtonRect[2] # get width xPos = dialogLeft + dialogWidth - okButtonWidth - INSET buttonsY = dialogTop + dialogHeight - okButtonHeight - 20 cancelButton.setLoc((dialogLeft + INSET, buttonsY)) okButton.setLoc((xPos, buttonsY)) # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): if (event.type == QUIT) or \ ((event.type == KEYDOWN) and (event.key == K_ESCAPE)): pygame.quit() sys.exit() if inputText.handleEvent(event) or okButton.handleEvent(event): theAnswer = inputText.getValue() return theAnswer if cancelButton.handleEvent(event): return None # 8 - Do any "per frame" actions # 9 - Clear the window area before drawing it again pygame.draw.rect(theWindow, backgroundColor, theRect) pygame.draw.rect(theWindow, DIALOG_BLACK, theRect, 1) # 10 - Draw the window elements promptText.draw() inputText.draw() cancelButton.draw() okButton.draw() # 11 - Update the window pygame.display.update()
def textYesNoDialog(theWindow, theRect, prompt, yesButtonText='Yes', noButtonText='No', backgroundColor=DIALOG_BACKGROUND_COLOR, textColor=DIALOG_BLACK): """A function that puts up a text-based two-button modal dialog (typically Yes/No or OK/Cancel) It can also be used to put up a single button alert dialog (typically with an OK button) Parameters: | theWindow - the window to draw in | theRect - the rectangle (or tuple) of the dialog box in the application window | prompt - prompt (title) string to be displayed in the dialog box Optional keyword parameters: | yesButtonText - text on the Yes button (defaults to 'Yes') | noButtonText - text on the No button (defaults to 'No') | Note: If noButtonText is None, the nothing will be drawn for the No button | This way, you can present an "alert" box with only an 'OK' button | backgroundColor - rgb background color for the dialog box (defaults to (0, 200, 200)) | textColor - rgb color for the prompt text (defaults to black) Returns: | True - meaning the Yes button was pressed | or | False - meaning the No button was pressed | | (With an alert dialog, you can ignore the returned value, as it will always be True.) """ dialogLeft = theRect[0] dialogTop = theRect[1] dialogWidth = theRect[2] dialogHeight = theRect[3] frameRect = pygame.Rect(dialogLeft + 1, dialogTop + 1, dialogWidth - 2, dialogHeight - 2) INSET = 30 # inset buttons from the edges of the dialog box promptText = pygwidgets.DisplayText(theWindow, (dialogLeft, dialogTop + 30), prompt, fontSize=24, width=dialogWidth, justified='center', textColor=textColor) # Create buttons, fix locations after finding out the size of the button(s) showNoButton = not (noButtonText is None) if showNoButton: noButton = pygwidgets.TextButton(theWindow, (0, 0), noButtonText) yesButton = pygwidgets.TextButton(theWindow, (0, 0), yesButtonText) yesButtonRect = yesButton.getRect() yesButtonHeight = yesButtonRect[3] yesButtonWidth = yesButtonRect[2] # get width xPos = dialogLeft + dialogWidth - yesButtonWidth - INSET buttonsY = dialogTop + dialogHeight - yesButtonHeight - 20 if showNoButton: noButton.setLoc((dialogLeft + INSET, buttonsY)) yesButton.setLoc((xPos, buttonsY)) #print('In dialogYesNo') #print('theRect is', theRect) #print('frameRect is', frameRect) # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): if (event.type == QUIT) or \ ((event.type == KEYDOWN) and (event.key == K_ESCAPE)): pygame.quit() sys.exit() if showNoButton: if noButton.handleEvent(event): return False if yesButton.handleEvent(event): return True # 8 - Do any "per frame" actions # 9 - Clear the window area before drawing it again pygame.draw.rect(theWindow, backgroundColor, theRect) pygame.draw.rect(theWindow, DIALOG_BLACK, frameRect, 1) # 10 - Draw the window elements promptText.draw() if showNoButton: noButton.draw() yesButton.draw() # 11 - Update the window pygame.display.update()
pygame.init() clock = pygame.time.Clock() pygame.display.set_caption('Roll Them Dice') window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) state = STATE_RUNNING nRoundsRolled = 0 nRoundsToRun = MAX_ROUNDS oTitleDisplay = pygwidgets.DisplayText(window, (330, 30), 'Roll Them Dice!', fontName='monospaces', fontSize=34) oQuitButton = pygwidgets.TextButton(window, (20, 460), 'Quit', width=100, height=35) oRunButton = pygwidgets.TextButton(window, (690, 460), 'Run', width=100, height=35) oRunButton.disable() oRoundsDisplay = pygwidgets.DisplayText(window, (315, 460), '0', fontName='monospaces', fontSize=28, width=70, justified='right') oOutOfDisplay = pygwidgets.DisplayText(window, (390, 460), 'out of',
choiceAsBoolean, userAnswer = pyghelpers.customAnswerDialog(theWindow, oDialogBackground, \ oPromptDisplayText, oUserInputText, oYesButton, oNoButton) return choiceAsBoolean, userAnswer # 3 - Initialize the world pygame.init() window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) clock = pygame.time.Clock() # set the speed (frames per second) # 4 - Load assets: image(s), sounds, etc. # 5 - Initialize variables textAlertButton = pygwidgets.TextButton(window, (75, 370), 'Text Alert') customAlertButton = pygwidgets.TextButton(window, (75, 420), 'Custom Alert') textYesNoButton = pygwidgets.TextButton(window, (280, 370), 'Text Yes/No') customYesNoButton = pygwidgets.TextButton(window, (280, 420), 'Custom Yes/No') textAnswerButton = pygwidgets.TextButton(window, (485, 370), 'Text Answer') customAnswerButton = pygwidgets.TextButton(window, (485, 420), 'Custom Answer') title = pygwidgets.DisplayText(window, (150, 50), 'Click all buttons to test dialogs', \ fontSize=36, textColor=WHITE) # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get():