├── .gitattributes ├── .gitignore ├── Chapter_1 ├── .project ├── .pydevproject ├── Account.py ├── Bank1_OneAccount.py ├── Bank2_OneAccountWithFunctions.py ├── Bank3_TwoAccounts.py ├── Bank4_N_Accounts.py ├── Bank5_Dictionary.py └── HigherOrLowerProcedural.py ├── Chapter_10 ├── EmployeeManagerInheritance │ └── EmployeeManagerInheritance.py ├── InheritedShapes │ ├── Circle.py │ ├── Main_Shapes.py │ ├── Main_ShapesWithRectangle.py │ ├── Rectangle.py │ ├── Shape.py │ ├── ShapeBasic.py │ ├── Square.py │ └── Triangle.py ├── MoneyExamples │ ├── DisplayMoney.py │ ├── InputNumber.py │ ├── Main_InputNumber.py │ └── Main_MoneyExample.py └── isInstanceIsSubclass.py ├── Chapter_11 ├── BalloonGame │ ├── Balloon.py │ ├── BalloonConstants.py │ ├── BalloonMgr.py │ ├── Main_BalloonGame.py │ ├── images │ │ ├── blueBalloon.png │ │ ├── greenBalloon.png │ │ ├── megaBalloon.png │ │ ├── megaBalloon1.png │ │ ├── megaBalloon2.png │ │ ├── megaBalloon3.png │ │ ├── orangeBalloon.png │ │ ├── pinkBalloon.png │ │ ├── purpleBalloon.png │ │ ├── redBalloon.png │ │ ├── redBalloonLarge.png │ │ ├── redBalloonMedium.png │ │ ├── redBalloonSmall.png │ │ └── yellowBalloon.png │ └── sounds │ │ ├── balloonPop.wav │ │ └── balloonSqueak.wav ├── ClassVariable.py ├── DeleteExample_Teacher_Student.py ├── ReferenceCount.py └── SlotsTest.py ├── Chapter_12 └── HigherOrLower │ ├── Card.py │ ├── Constants.py │ ├── Deck.py │ ├── Game.py │ ├── Main_HigherOrLower.py │ ├── images │ ├── 10 of Clubs.png │ ├── 10 of Diamonds.png │ ├── 10 of Hearts.png │ ├── 10 of Spades.png │ ├── 2 of Clubs.png │ ├── 2 of Diamonds.png │ ├── 2 of Hearts.png │ ├── 2 of Spades.png │ ├── 3 of Clubs.png │ ├── 3 of Diamonds.png │ ├── 3 of Hearts.png │ ├── 3 of Spades.png │ ├── 4 of Clubs.png │ ├── 4 of Diamonds.png │ ├── 4 of Hearts.png │ ├── 4 of Spades.png │ ├── 5 of Clubs.png │ ├── 5 of Diamonds.png │ ├── 5 of Hearts.png │ ├── 5 of Spades.png │ ├── 6 of Clubs.png │ ├── 6 of Diamonds.png │ ├── 6 of Hearts.png │ ├── 6 of Spades.png │ ├── 7 of Clubs.png │ ├── 7 of Diamonds.png │ ├── 7 of Hearts.png │ ├── 7 of Spades.png │ ├── 8 of Clubs.png │ ├── 8 of Diamonds.png │ ├── 8 of Hearts.png │ ├── 8 of Spades.png │ ├── 9 of Clubs.png │ ├── 9 of Diamonds.png │ ├── 9 of Hearts.png │ ├── 9 of Spades.png │ ├── Ace of Clubs.png │ ├── Ace of Diamonds.png │ ├── Ace of Hearts.png │ ├── Ace of Spades.png │ ├── BackOfCard.png │ ├── Jack of Clubs.png │ ├── Jack of Diamonds.png │ ├── Jack of Hearts.png │ ├── Jack of Spades.png │ ├── King of Clubs.png │ ├── King of Diamonds.png │ ├── King of Hearts.png │ ├── King of Spades.png │ ├── Queen of Clubs.png │ ├── Queen of Diamonds.png │ ├── Queen of Hearts.png │ ├── Queen of Spades.png │ └── background.png │ └── sounds │ ├── cardFlip.wav │ ├── cardShuffle.wav │ ├── ding.wav │ ├── loser.wav │ └── push.wav ├── Chapter_13 ├── InLineTimerExamples │ ├── CountingFrames.py │ ├── ElapsedTime.py │ └── TimerEvent.py ├── SliderPuzzles │ ├── Constants.py │ ├── Game.py │ ├── Main_SliderPuzzleCountDown.py │ ├── Main_SliderPuzzleCountUp.py │ ├── Square.py │ ├── Tile.py │ ├── images │ │ ├── restartButtonDown.jpg │ │ ├── restartButtonOver.jpg │ │ └── restartButtonUp.jpg │ └── sounds │ │ ├── applause.wav │ │ ├── buzz.wav │ │ ├── nope.wav │ │ └── tick.wav └── TimerObjectExamples │ ├── CountDownTimerTester.py │ ├── CountUpTimerTester.py │ └── SimpleTimerExample.py ├── Chapter_14 ├── AnimationExample │ ├── Main_AnimationExample ORIG from book.py │ ├── Main_AnimationExample.py │ ├── Player.py │ └── images │ │ ├── Dinowalk │ │ ├── f1.png │ │ ├── f10.png │ │ ├── f11.png │ │ ├── f12.png │ │ ├── f13.png │ │ ├── f14.png │ │ ├── f15.png │ │ ├── f16.png │ │ ├── f17.png │ │ ├── f2.png │ │ ├── f3.png │ │ ├── f4.png │ │ ├── f5.png │ │ ├── f6.png │ │ ├── f7.png │ │ ├── f8.png │ │ └── f9.png │ │ ├── TRex │ │ ├── f1.gif │ │ ├── f10.gif │ │ ├── f2.gif │ │ ├── f3.gif │ │ ├── f4.gif │ │ ├── f5.gif │ │ ├── f6.gif │ │ ├── f7.gif │ │ ├── f8.gif │ │ └── f9.gif │ │ ├── effect_010.png │ │ ├── male_walkcycle.png │ │ ├── player │ │ ├── walkB0.png │ │ ├── walkB1.png │ │ ├── walkB2.png │ │ ├── walkB3.png │ │ ├── walkB4.png │ │ ├── walkB5.png │ │ ├── walkB6.png │ │ ├── walkB7.png │ │ ├── walkB8.png │ │ ├── walkF0.png │ │ ├── walkF1.png │ │ ├── walkF2.png │ │ ├── walkF3.png │ │ ├── walkF4.png │ │ ├── walkF5.png │ │ ├── walkF6.png │ │ ├── walkF7.png │ │ ├── walkF8.png │ │ ├── walkL0.png │ │ ├── walkL1.png │ │ ├── walkL2.png │ │ ├── walkL3.png │ │ ├── walkL4.png │ │ ├── walkL5.png │ │ ├── walkL6.png │ │ ├── walkL7.png │ │ ├── walkL8.png │ │ ├── walkR0.png │ │ ├── walkR1.png │ │ ├── walkR2.png │ │ ├── walkR3.png │ │ ├── walkR4.png │ │ ├── walkR5.png │ │ ├── walkR6.png │ │ ├── walkR7.png │ │ └── walkR8.png │ │ ├── runLeft.png │ │ └── runRight.png ├── SimpleAnimation │ ├── Main_SimpleAnimationExample.py │ ├── SimpleAnimation.py │ └── images │ │ └── Dinobike │ │ ├── f1.gif │ │ ├── f10.gif │ │ ├── f2.gif │ │ ├── f3.gif │ │ ├── f4.gif │ │ ├── f5.gif │ │ ├── f6.gif │ │ ├── f7.gif │ │ ├── f8.gif │ │ └── f9.gif └── SimpleSpriteSheetAnimation │ ├── Main_SimpleSpriteSheetAnimation.py │ ├── SimpleSpriteSheetAnimation.py │ └── images │ ├── numbers.png │ └── water_003.png ├── Chapter_15 ├── RockPaperScissorsStateMachine │ ├── RockPaperScissors.py │ ├── images │ │ ├── Paper.png │ │ ├── PaperDown.png │ │ ├── PaperGray.png │ │ ├── PaperOver.png │ │ ├── RPS.png │ │ ├── Rock.png │ │ ├── RockDown.png │ │ ├── RockGray.png │ │ ├── RockOver.png │ │ ├── Scissors.png │ │ ├── ScissorsDown.png │ │ ├── ScissorsGray.png │ │ ├── ScissorsOver.png │ │ ├── restartButtonDown.png │ │ ├── restartButtonHighlight.png │ │ ├── restartButtonUp.png │ │ ├── startButtonDown.png │ │ ├── startButtonHighlight.png │ │ └── startButtonUp.png │ └── sounds │ │ ├── buzz.wav │ │ ├── ding.wav │ │ └── push.wav ├── RockPaperScissorsWithScenes │ ├── Constants.py │ ├── Main_RockPaperScissors.py │ ├── ScenePlay.py │ ├── SceneResults.py │ ├── SceneSplash.py │ ├── images │ │ ├── Paper.png │ │ ├── PaperDown.png │ │ ├── PaperOver.png │ │ ├── RPS.png │ │ ├── Rock.png │ │ ├── RockDown.png │ │ ├── RockOver.png │ │ ├── Scissors.png │ │ ├── ScissorsDown.png │ │ ├── ScissorsOver.png │ │ ├── restartButtonDown.png │ │ ├── restartButtonHighlight.png │ │ ├── restartButtonUp.png │ │ ├── startButtonDown.png │ │ ├── startButtonHighlight.png │ │ └── startButtonUp.png │ └── sounds │ │ ├── buzz.wav │ │ ├── ding.wav │ │ └── push.wav └── SceneDemo │ ├── Constants.py │ ├── Main_SceneDemo.py │ ├── SceneA.py │ ├── SceneB.py │ ├── SceneC.py │ └── SceneExample.py ├── Chapter_16 ├── DialogTester │ ├── Main_DialogTester.py │ └── images │ │ ├── addDisabled.png │ │ ├── addDown.png │ │ ├── addNormal.png │ │ ├── addOver.png │ │ ├── cancelDisabled.png │ │ ├── cancelDown.png │ │ ├── cancelNormal.png │ │ ├── cancelOver.png │ │ ├── controlsBackground.jpg │ │ ├── dialog.png │ │ ├── highScoresBackground.jpg │ │ ├── noDisabled.png │ │ ├── noDown.png │ │ ├── noNormal.png │ │ ├── noOver.png │ │ ├── noThanksDisabled.png │ │ ├── noThanksDown.png │ │ ├── noThanksNormal.png │ │ ├── noThanksOver.png │ │ ├── okDisabled.png │ │ ├── okDown.png │ │ ├── okNormal.png │ │ ├── okOver.png │ │ ├── yesDisabled.png │ │ ├── yesDown.png │ │ ├── yesNormal.png │ │ └── yesOver.png └── Dodger │ ├── Baddies.py │ ├── Constants.py │ ├── Goodies.py │ ├── HighScores.json │ ├── HighScoresData.py │ ├── Main_Dodger.py │ ├── Player.py │ ├── SceneExample.py │ ├── SceneHighScores.py │ ├── ScenePlay.py │ ├── SceneSplash.py │ ├── __init__.py │ ├── images │ ├── addDisabled.png │ ├── addDown.png │ ├── addNormal.png │ ├── addOver.png │ ├── backDisabled.png │ ├── backDown.png │ ├── backNormal.png │ ├── backOver.png │ ├── baddie.png │ ├── cancelDisabled.png │ ├── cancelDown.png │ ├── cancelNormal.png │ ├── cancelOver.png │ ├── controlsBackground.jpg │ ├── dialog.png │ ├── dodger.png │ ├── gameOver.png │ ├── goodie.png │ ├── gotoHighScoresDisabled.png │ ├── gotoHighScoresDown.png │ ├── gotoHighScoresNormal.png │ ├── gotoHighScoresOver.png │ ├── highScoresBackground.jpg │ ├── highScoresDisabled.png │ ├── highScoresDown.png │ ├── highScoresNormal.png │ ├── highScoresOver.png │ ├── noThanksDisabled.png │ ├── noThanksDown.png │ ├── noThanksNormal.png │ ├── noThanksOver.png │ ├── okDisabled.png │ ├── okDown.png │ ├── okNormal.png │ ├── okOver.png │ ├── player.png │ ├── quitDisabled.png │ ├── quitDown.png │ ├── quitNormal.png │ ├── quitOver.png │ ├── resetDisabled.png │ ├── resetDown.png │ ├── resetNormal.png │ ├── resetOver.png │ ├── splashBackground.jpg │ ├── startDisabled.png │ ├── startDown.png │ ├── startNewDisabled.png │ ├── startNewDown.png │ ├── startNewNormal.png │ ├── startNewOver.png │ ├── startNormal.png │ └── startOver.png │ └── sounds │ ├── background.mid │ ├── ding.wav │ └── gameover.wav ├── Chapter_17 └── MVC_RollTheDice │ ├── BarView.py │ ├── Bin.py │ ├── Constants.py │ ├── Controller.py │ ├── InputNumber.py │ ├── Main_MVC.py │ ├── Model.py │ ├── PieView.py │ ├── TextView.py │ └── images │ └── twoDice.png ├── Chapter_2 ├── DimmerSwitch.py ├── LightSwitch_Procedural.py ├── OO_DimmerSwitch_Two_Instances.py ├── OO_DimmerSwitch_with_Test_Code.py ├── OO_LightSwitch_Two_Instances.py ├── OO_LightSwitch_with_Test_Code.py ├── OO_TV_Two_Instances.py ├── OO_TV_Two_Instances_with_Init_Params.py ├── OO_TV_with_Test_Code.py └── TV.py ├── Chapter_3 ├── OO_DimmerSwitch_Model1.py ├── OO_DimmerSwitch_Model2_Instantiation.py └── OO_DimmerSwitch_Model2_Method_Calls.py ├── Chapter_4 ├── Account.py ├── BankOOP1_IndividualVariables │ ├── .project │ ├── .pydevproject │ ├── Account.py │ └── Main_Bank_Version1.py ├── BankOOP2_ListOfAccountObjects │ ├── .project │ ├── .pydevproject │ ├── Account.py │ └── Main_Bank_Version2.py ├── BankOOP3_DictionaryOfAcountObjects │ ├── .project │ ├── .pydevproject │ ├── Account.py │ └── Main_Bank_Version3.py ├── BankOOP4_InteractiveMenu │ ├── .project │ ├── .pydevproject │ ├── Account.py │ └── Main_Bank_Version4.py ├── BankOOP5_SeparateBankClass │ ├── Account.py │ ├── Bank.py │ └── Main_Bank_Version5.py ├── BankOOP6_UsingExceptions │ ├── .project │ ├── .pydevproject │ ├── Account.py │ ├── Bank.py │ └── Main_Bank_Version6.py └── MonsterExample.py ├── Chapter_5 ├── PygameDemo0_WindowOnly │ └── PygameWindowOnly.py ├── PygameDemo1_OneImage │ ├── PygameOneImage.py │ └── images │ │ └── ball.png ├── PygameDemo2_ImageClickAndMove │ ├── PygameImageClickAndMove.py │ └── images │ │ └── ball.png ├── PygameDemo3_MoveByKeyboard │ ├── PygameMoveByKeyboardContinuous.py │ ├── PygameMoveByKeyboardOncePerKey.py │ └── images │ │ ├── ball.png │ │ └── target.jpg ├── PygameDemo4_OneBallBounce │ ├── PygameOneBallBouceRects.py │ ├── PygameOneBallBouceWithSound.py │ ├── PygameOneBallBouceXY.py │ ├── images │ │ └── ball.png │ └── sounds │ │ ├── background.mp3 │ │ └── boing.wav ├── PygameDemo5_DrawingShapes.py └── TwoButtons │ ├── TwoButtons.py │ ├── bark.wav │ └── meow.wav ├── Chapter_6 ├── PygameDemo6_BallBounceObjectOriented │ ├── Ball.py │ ├── Main_BallBounce.py │ ├── Main_BallBounceManyBalls.py │ └── images │ │ └── ball.png ├── PygameDemo7_SimpleButton │ ├── Main_SimpleButton.py │ ├── Main_SimpleButton3Buttons.py │ ├── SimpleButton.py │ └── images │ │ ├── buttonADown.png │ │ ├── buttonAUp.png │ │ ├── buttonBDown.png │ │ ├── buttonBUp.png │ │ ├── buttonCDown.png │ │ ├── buttonCUp.png │ │ ├── buttonDown.png │ │ └── buttonUp.png ├── PygameDemo8_SimpleTextDisplay │ ├── Ball.py │ ├── Main_BallTextAndButton.py │ ├── SimpleButton.py │ ├── SimpleText.py │ └── images │ │ ├── ball.png │ │ ├── restartDown.png │ │ └── restartUp.png └── PygameDemo9_SimpleButtonWithCallback │ ├── Main_SimpleButtonCallback.py │ ├── SimpleButton.py │ └── images │ ├── buttonADown.png │ ├── buttonAUp.png │ ├── buttonBDown.png │ ├── buttonBUp.png │ ├── buttonCDown.png │ └── buttonCUp.png ├── Chapter_7 ├── OrderPizzaWithNone.py └── PygameDemo10_ImageTextButtonBall │ ├── PygameTextButtonBall.py │ ├── images │ ├── background.jpg │ ├── ball.png │ ├── restartDisabled.png │ ├── restartDown.png │ ├── restartOver.png │ └── restartUp.png │ └── sounds │ └── blip.wav ├── Chapter_8 ├── PersonGettersSettersAndDirectAccess │ ├── Main_PersonDirectAccess.py │ ├── Main_PersonGetterSetter.py │ └── Person.py ├── PrivatePersonWIthNameMangling │ ├── Main_PrivatePerson.py │ └── PrivatePerson.py ├── PropertyDecorator │ ├── Main_Property.py │ └── Student.py ├── Stack │ ├── Main_StackTest.py │ └── Stack.py └── ValidatingData_ClubExample │ ├── Club.py │ └── Main_Club.py ├── Chapter_9 ├── MagicMethods │ ├── Fraction.py │ ├── Rectangle │ │ ├── Main_RectangleExample.py │ │ └── Rectangle.py │ └── Vectors │ │ ├── Main_Vectors.py │ │ └── Vector.py ├── PetsPolymorphism.py └── Shapes │ ├── Circle.py │ ├── Main_ShapesExample.py │ ├── Square.py │ └── Triangle.py ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Chapter_1/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bank 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter_1/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | /Bank/src 7 | 8 | python 2.7 9 | Default 10 | 11 | -------------------------------------------------------------------------------- /Chapter_1/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | 3 | class Account(): 4 | def __init__(self, name, balance, password): 5 | self.name = name 6 | self.balance = int(balance) 7 | self.password = password 8 | 9 | def deposit(self, amountToDeposit, password): 10 | if password != self.password: 11 | print('Sorry, incorrect password') 12 | return None 13 | 14 | if amountToDeposit < 0: 15 | print('You cannot deposit a negative amount') 16 | return None 17 | 18 | self.balance = self.balance + amountToDeposit 19 | return self.balance 20 | 21 | def withdraw(self, amountToWithdraw, password): 22 | if password != self.password: 23 | print('Incorrect password for this account') 24 | return None 25 | 26 | if amountToWithdraw < 0: 27 | print('You cannot withdraw a negative amount') 28 | return None 29 | 30 | if amountToWithdraw > self.balance: 31 | print('You cannot withdraw more than you have in your account') 32 | return None 33 | 34 | self.balance = self.balance - amountToWithdraw 35 | return self.balance 36 | 37 | def getBalance(self, password): 38 | if password != self.password: 39 | print('Sorry, incorrect password') 40 | return None 41 | return self.balance 42 | 43 | # Added for debugging 44 | def show(self): 45 | print(' Name:', self.name) 46 | print(' Balance:', self.balance) 47 | print(' Password:', self.password) 48 | print() 49 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/Circle.py: -------------------------------------------------------------------------------- 1 | # Circle class 2 | 3 | import pygame 4 | from Shape import * 5 | import math 6 | 7 | class Circle(Shape): 8 | 9 | def __init__(self, window, maxWidth, maxHeight): 10 | super().__init__(window, 'Circle', maxWidth, maxHeight) 11 | self.radius = random.randrange(10, 50) 12 | self.centerX = self.x + self.radius 13 | self.centerY = self.y + self.radius 14 | self.rect = pygame.Rect(self.x, self.y, self.radius * 2, self.radius * 2) 15 | 16 | def clickedInside(self, mousePoint): 17 | theDistance = math.sqrt(((mousePoint[0] - self.centerX) ** 2) + 18 | ((mousePoint[1] - self.centerY) ** 2)) 19 | if theDistance <= self.radius: 20 | return True 21 | else: 22 | return False 23 | 24 | def getArea(self): 25 | theArea = math.pi * (self.radius ** 2) 26 | return theArea 27 | 28 | def draw(self): 29 | pygame.draw.circle(self.window, self.color, (self.centerX, self.centerY), self.radius, 0) 30 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/Main_Shapes.py: -------------------------------------------------------------------------------- 1 | # Main Shapes program 2 | 3 | import pygame 4 | import sys 5 | from pygame.locals import * 6 | from Square import * 7 | from Circle import * 8 | from Triangle import * 9 | import pygwidgets 10 | 11 | # set up the constants 12 | WHITE = (255, 255, 255) 13 | WINDOW_WIDTH = 640 14 | WINDOW_HEIGHT = 480 15 | FRAMES_PER_SECOND = 30 16 | N_SHAPES = 10 17 | 18 | # set up the window 19 | pygame.init() 20 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) 21 | clock = pygame.time.Clock() 22 | 23 | shapesList = [] 24 | shapeClassesTuple = (Square, Circle, Triangle) 25 | for i in range(0, N_SHAPES): 26 | randomlyChosenClass = random.choice(shapeClassesTuple) 27 | oShape = randomlyChosenClass(window, WINDOW_WIDTH, WINDOW_HEIGHT) 28 | shapesList.append(oShape) 29 | 30 | oStatusLine = pygwidgets.DisplayText(window, (4, 4), \ 31 | 'Click on shapes', fontSize=28) 32 | 33 | # main loop 34 | while True: 35 | for event in pygame.event.get(): 36 | if event.type == QUIT: 37 | pygame.quit() 38 | sys.exit() 39 | 40 | if event.type == MOUSEBUTTONDOWN: 41 | for oShape in shapesList: 42 | if oShape.clickedInside(event.pos): 43 | thisArea = oShape.getArea() 44 | thisType = oShape.getType() 45 | newText = 'Clicked on a ' + thisType \ 46 | + ' whose area is ' + str(thisArea) 47 | oStatusLine.setValue(newText) 48 | window.fill(WHITE) 49 | # Tell each shape to draw itself 50 | for oShape in shapesList: 51 | oShape.draw() 52 | oStatusLine.draw() 53 | 54 | pygame.display.update() 55 | clock.tick(FRAMES_PER_SECOND) 56 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/Main_ShapesWithRectangle.py: -------------------------------------------------------------------------------- 1 | # Main Shapes program with Rectangle 2 | 3 | import pygame 4 | import sys 5 | from pygame.locals import * 6 | from Square import * 7 | from Circle import * 8 | from Triangle import * 9 | from Rectangle import * 10 | import pygwidgets 11 | 12 | # set up the constants 13 | WHITE = (255, 255, 255) 14 | WINDOW_WIDTH = 640 15 | WINDOW_HEIGHT = 480 16 | FRAMES_PER_SECOND = 30 17 | N_SHAPES = 10 18 | 19 | # set up the window 20 | pygame.init() 21 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) 22 | clock = pygame.time.Clock() 23 | 24 | shapesList = [] 25 | shapeClassesTuple = (Square, Circle, Triangle, Rectangle) 26 | for i in range(0, N_SHAPES): 27 | randomlyChosenClass = random.choice(shapeClassesTuple) 28 | oShape = randomlyChosenClass(window, WINDOW_WIDTH, WINDOW_HEIGHT) 29 | shapesList.append(oShape) 30 | 31 | oStatusLine = pygwidgets.DisplayText(window, (4, 4), \ 32 | 'Click on shapes', fontSize=28) 33 | 34 | # main loop 35 | while True: 36 | for event in pygame.event.get(): 37 | if event.type == QUIT: 38 | pygame.quit() 39 | sys.exit() 40 | 41 | if event.type == MOUSEBUTTONDOWN: 42 | for oShape in reversed(shapesList): 43 | if oShape.clickedInside(event.pos): 44 | thisArea = oShape.getArea() 45 | thisType = oShape.getType() 46 | newText = 'Clicked on a ' + thisType \ 47 | + ' whose area is ' + str(thisArea) 48 | oStatusLine.setValue(newText) 49 | break # only deal with the topmost shape 50 | window.fill(WHITE) 51 | # Tell each shape to draw itself 52 | for oShape in shapesList: 53 | oShape.draw() 54 | oStatusLine.draw() 55 | 56 | pygame.display.update() 57 | clock.tick(FRAMES_PER_SECOND) 58 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/Rectangle.py: -------------------------------------------------------------------------------- 1 | # Rectangle class 2 | 3 | import pygame 4 | from Shape import * 5 | 6 | class Rectangle(Shape): 7 | 8 | def __init__(self, window, maxWidth, maxHeight): 9 | super().__init__(window, 'Rectangle', maxWidth, maxHeight) 10 | self.width = random.randrange(10, 100) 11 | self.height = random.randrange(10, 100) 12 | self.rect = pygame.Rect(self.x, self.y, self.width, self.height) 13 | 14 | def clickedInside(self, mousePoint): 15 | clicked = self.rect.collidepoint(mousePoint) 16 | return clicked 17 | 18 | def getArea(self): 19 | theArea = self.width * self.height 20 | return theArea 21 | 22 | # Uncomment to implement draw method for Rectangle 23 | # def draw(self): 24 | # pygame.draw.rect(self.window, self.color, (self.x, self.y, self.width, self.height)) 25 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/Shape.py: -------------------------------------------------------------------------------- 1 | # Shape class 2 | # 3 | # To be used as a base class for other classes 4 | 5 | import random 6 | from abc import ABC, abstractmethod 7 | 8 | # Set up the colors 9 | RED = (255, 0, 0) 10 | GREEN = (0, 255, 0) 11 | BLUE = (0, 0, 255) 12 | 13 | class Shape(ABC): # identifies this as an abstract base class 14 | 15 | def __init__(self, window, shapeType, maxWidth, maxHeight): 16 | self.window = window 17 | self.shapeType = shapeType 18 | self.color = random.choice((RED, GREEN, BLUE)) 19 | self.x = random.randrange(1, maxWidth - 100) 20 | self.y = random.randrange(25, maxHeight - 100) 21 | 22 | def getType(self): 23 | return self.shapeType 24 | 25 | @abstractmethod 26 | def clickedInside(self, mousePoint): 27 | raise NotImplementedError 28 | 29 | @abstractmethod 30 | def getArea(self): 31 | raise NotImplementedError 32 | 33 | @abstractmethod 34 | def draw(self): 35 | raise NotImplementedError 36 | 37 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/ShapeBasic.py: -------------------------------------------------------------------------------- 1 | # Shape class - basic 2 | 3 | import random 4 | 5 | # Set up the colors 6 | RED = (255, 0, 0) 7 | GREEN = (0, 255, 0) 8 | BLUE = (0, 0, 255) 9 | 10 | class Shape(): # This is an abstract class 11 | 12 | def __init__(self, window, shapeType, maxWidth, maxHeight): 13 | self.window = window 14 | self.shapeType = shapeType 15 | self.color = random.choice((RED, GREEN, BLUE)) 16 | self.x = random.randrange(1, maxWidth - 100) 17 | self.y = random.randrange(25, maxHeight - 100) 18 | 19 | def getType(self): 20 | return self.shapeType 21 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/Square.py: -------------------------------------------------------------------------------- 1 | # Square class 2 | 3 | import pygame 4 | from Shape import * 5 | 6 | class Square(Shape): 7 | 8 | def __init__(self, window, maxWidth, maxHeight): 9 | super().__init__(window, 'Square', maxWidth, maxHeight) 10 | self.widthAndHeight = random.randrange(10, 100) 11 | self.rect = pygame.Rect(self.x, self.y, 12 | self.widthAndHeight, self.widthAndHeight) 13 | 14 | def clickedInside(self, mousePoint): 15 | clicked = self.rect.collidepoint(mousePoint) 16 | return clicked 17 | 18 | def getArea(self): 19 | theArea = self.widthAndHeight * self.widthAndHeight 20 | return theArea 21 | 22 | def draw(self): 23 | pygame.draw.rect(self.window, self.color, 24 | (self.x, self.y, self.widthAndHeight, self.widthAndHeight)) 25 | -------------------------------------------------------------------------------- /Chapter_10/InheritedShapes/Triangle.py: -------------------------------------------------------------------------------- 1 | # Triangle class 2 | 3 | import pygame 4 | from Shape import * 5 | 6 | class Triangle(Shape): 7 | 8 | def __init__(self, window, maxWidth, maxHeight): 9 | super().__init__(window, 'Triangle', maxWidth, maxHeight) 10 | self.width = random.randrange(10, 100) 11 | self.height = random.randrange(10, 100) 12 | self.triangleSlope = -1 * (self.height / self.width) 13 | self.rect = pygame.Rect(self.x, self.y, self.width, self.height) 14 | 15 | def clickedInside(self, mousePoint): 16 | inRect = self.rect.collidepoint(mousePoint) 17 | if not inRect: 18 | return False 19 | 20 | # Do some math to see if the point is inside the triangle 21 | xOffset = mousePoint[0] - self.x 22 | yOffset = mousePoint[1] - self.y 23 | if xOffset == 0: 24 | return True 25 | 26 | pointSlopeFromYIntercept = (yOffset - self.height) / xOffset #rise over run 27 | if pointSlopeFromYIntercept < self.triangleSlope: 28 | return True 29 | else: 30 | return False 31 | 32 | def getArea(self): 33 | theArea = .5 * self.width * self.height 34 | return theArea 35 | 36 | def draw(self): 37 | pygame.draw.polygon(self.window, self.color, ( 38 | (self.x, self.y + self.height), 39 | (self.x, self.y), 40 | (self.x + self.width, self.y))) 41 | -------------------------------------------------------------------------------- /Chapter_10/MoneyExamples/DisplayMoney.py: -------------------------------------------------------------------------------- 1 | # DisplayMoney class - displays a number as an amount of money 2 | # 3 | # Demo of inheritance 4 | 5 | import pygwidgets 6 | 7 | BLACK = (0, 0, 0) 8 | 9 | # DisplayMoney class inherits from DisplayText class 10 | class DisplayMoney(pygwidgets.DisplayText): 11 | 12 | def __init__(self, window, loc, value=None, 13 | fontName=None, fontSize=24, width=150, height=None, 14 | textColor=BLACK, backgroundColor=None, 15 | justified='left', nickname=None, currencySymbol='$', 16 | currencySymbolOnLeft=True, showCents=True): 17 | 18 | self.currencySymbol = currencySymbol 19 | self.currencySymbolOnLeft = currencySymbolOnLeft 20 | self.showCents = showCents 21 | if value is None: 22 | value = 0.00 23 | 24 | # Call the __init__ method of our base class 25 | super().__init__(window, loc, value, fontName, fontSize, 26 | width, height, textColor, backgroundColor, 27 | justified, nickname) 28 | 29 | def setValue(self, money): 30 | if money == '': 31 | money = 0.00 32 | 33 | money = float(money) 34 | 35 | if self.showCents: 36 | money = '{:,.2f}'.format(money) 37 | else: 38 | money = '{:,.0f}'.format(money) 39 | 40 | if self.currencySymbolOnLeft: 41 | theText = self.currencySymbol + money 42 | else: 43 | theText = money + self.currencySymbol 44 | 45 | # Call the setValue method of our base class 46 | super().setValue(theText) 47 | -------------------------------------------------------------------------------- /Chapter_10/isInstanceIsSubclass.py: -------------------------------------------------------------------------------- 1 | # Testing isinstance and issubclass 2 | class Base(): 3 | 4 | def __init__(self): 5 | print('In Base init method') 6 | 7 | class Sub(Base): 8 | 9 | def __init__(self): 10 | print('In Subclass init method') 11 | 12 | oSub = Sub() # create an instance of the Sub class 13 | oBase = Base() # create an instance of the Base class 14 | 15 | print(isinstance(oSub, Sub)) # returns True 16 | print(isinstance(oSub, Base)) # returns True 17 | 18 | print(isinstance(oBase, Base)) # returns True 19 | print(isinstance(oBase, Sub)) # returns False 20 | 21 | print(issubclass(Sub, Base)) # returns True 22 | print(issubclass(Base, Sub)) # returns False 23 | -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/BalloonConstants.py: -------------------------------------------------------------------------------- 1 | # Constants used by more than one Python modules 2 | 3 | N_BALLOONS = 15 # number of balloons in a round of the game 4 | BALLOON_MISSED = 'Missed' # balloon went off the top 5 | BALLOON_MOVING = 'Balloon Moving' # balloon is moving 6 | -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/blueBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/blueBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/greenBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/greenBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/megaBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/megaBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/megaBalloon1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/megaBalloon1.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/megaBalloon2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/megaBalloon2.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/megaBalloon3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/megaBalloon3.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/orangeBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/orangeBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/pinkBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/pinkBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/purpleBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/purpleBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/redBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/redBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/redBalloonLarge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/redBalloonLarge.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/redBalloonMedium.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/redBalloonMedium.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/redBalloonSmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/redBalloonSmall.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/images/yellowBalloon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/images/yellowBalloon.png -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/sounds/balloonPop.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/sounds/balloonPop.wav -------------------------------------------------------------------------------- /Chapter_11/BalloonGame/sounds/balloonSqueak.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_11/BalloonGame/sounds/balloonSqueak.wav -------------------------------------------------------------------------------- /Chapter_11/ClassVariable.py: -------------------------------------------------------------------------------- 1 | # Sample class 2 | class Sample(): 3 | 4 | nObjects = 0 # this is a class variable of the Sample class 5 | def __init__(self, name): 6 | self.name = name 7 | Sample.nObjects = Sample.nObjects + 1 8 | 9 | def howManyObjects(self): 10 | print('There are', Sample.nObjects, 'Sample objects') 11 | 12 | def __del__(self): 13 | Sample.nObjects = Sample.nObjects - 1 14 | 15 | # Instantiate 4 objects 16 | oSample1 = Sample('A') 17 | oSample2 = Sample('B') 18 | oSample3 = Sample('C') 19 | oSample4 = Sample('D') 20 | 21 | # Delete 1 object 22 | del oSample3 23 | 24 | # See how many we have 25 | print('Number of objects:', Sample.nObjects) 26 | -------------------------------------------------------------------------------- /Chapter_11/DeleteExample_Teacher_Student.py: -------------------------------------------------------------------------------- 1 | # Student class 2 | class Student(): 3 | def __init__(self, name): 4 | self.name = name 5 | print('Creating Student object', self.name) 6 | 7 | def __del__(self): 8 | print('In the __del__ method for student:', self.name) 9 | 10 | # Teacher class 11 | class Teacher(): 12 | def __init__(self): 13 | print('Creating the Teacher object') 14 | self.oStudent1 = Student('Joe') 15 | self.oStudent2 = Student('Sue') 16 | self.oStudent3 = Student('Chris') 17 | 18 | def __del__(self): 19 | print('In the __del__ method for Teacher') 20 | 21 | # Instantiate the Teacher object (that creates Student objects) 22 | oTeacher = Teacher() 23 | 24 | # Delete the Teacher object 25 | del oTeacher 26 | -------------------------------------------------------------------------------- /Chapter_11/ReferenceCount.py: -------------------------------------------------------------------------------- 1 | # Reference count example 2 | 3 | class Square(): 4 | def __init__(self, width, color): 5 | self.width = width 6 | self.color = color 7 | 8 | # Instantiate an object 9 | oSquare1 = Square(5, 'red') 10 | print(oSquare1) 11 | # Reference count of the Square object is 1 12 | 13 | # Now set another variable to the same object 14 | oSquare2 = oSquare1 15 | print(oSquare2) 16 | # Reference count of the Square object is 2 17 | -------------------------------------------------------------------------------- /Chapter_11/SlotsTest.py: -------------------------------------------------------------------------------- 1 | # Stanard example that uses a dictionary 2 | class Point(object): 3 | 4 | def __init__(self, x, y): 5 | self.x = x 6 | self.y = y 7 | print(x, y) 8 | 9 | # Try to create an additional instance variable 10 | self.color = 'black' #works fine 11 | print(self.color) 12 | 13 | oPoint = Point(3, 5) 14 | 15 | 16 | class PointWithSlots(): 17 | #Define slots for only two instance variables 18 | __slots__ = ['x', 'y'] 19 | 20 | def __init__(self, x, y): 21 | self.x = x 22 | self.y = y 23 | print(x, y) 24 | 25 | # Try to create an additional instance variable 26 | # Should fail 27 | self.color = 'black' 28 | print(self.color) 29 | 30 | oPointWithSlots = PointWithSlots(3, 5) 31 | -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/Card.py: -------------------------------------------------------------------------------- 1 | # Card class 2 | 3 | import pygame 4 | import pygwidgets 5 | 6 | class Card(): 7 | 8 | BACK_OF_CARD_IMAGE = pygame.image.load('images/BackOfCard.png') 9 | 10 | def __init__(self, window, rank, suit, value): 11 | self.window = window 12 | self.rank = rank 13 | self.suit = suit 14 | self.cardName = rank + ' of ' + suit 15 | self.value = value 16 | fileName = 'images/' + self.cardName + '.png' 17 | # Set some starting location; use setLoc below to change 18 | self.images = pygwidgets.ImageCollection(window, (0, 0), 19 | {'front': fileName, 20 | 'back': Card.BACK_OF_CARD_IMAGE}, 'back') 21 | 22 | def conceal(self): 23 | self.images.replace('back') 24 | 25 | def reveal(self): 26 | self.images.replace('front') 27 | 28 | def getName(self): 29 | return self.cardName 30 | 31 | def getValue(self): 32 | return self.value 33 | 34 | def getSuit(self): 35 | return self.suit 36 | 37 | def getRank(self): 38 | return self.rank 39 | 40 | def setLoc(self, loc): # call the setLoc method of the ImageCollection 41 | self.images.setLoc(loc) 42 | 43 | def getLoc(self): # get the location from the ImageCollection 44 | loc = self.images.getLoc() 45 | return loc 46 | 47 | def draw(self): 48 | self.images.draw() 49 | 50 | -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/Constants.py: -------------------------------------------------------------------------------- 1 | # Constants 2 | 3 | WHITE = (255, 255, 255) 4 | HIGHER = 'higher' 5 | LOWER = 'lower' -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/10 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/10 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/10 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/10 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/10 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/10 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/10 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/10 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/2 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/2 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/2 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/2 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/2 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/2 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/2 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/2 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/3 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/3 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/3 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/3 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/3 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/3 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/3 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/3 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/4 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/4 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/4 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/4 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/4 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/4 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/4 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/4 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/5 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/5 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/5 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/5 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/5 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/5 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/5 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/5 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/6 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/6 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/6 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/6 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/6 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/6 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/6 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/6 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/7 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/7 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/7 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/7 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/7 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/7 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/7 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/7 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/8 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/8 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/8 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/8 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/8 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/8 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/8 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/8 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/9 of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/9 of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/9 of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/9 of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/9 of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/9 of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/9 of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/9 of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Ace of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Ace of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Ace of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Ace of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Ace of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Ace of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Ace of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Ace of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/BackOfCard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/BackOfCard.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Jack of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Jack of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Jack of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Jack of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Jack of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Jack of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Jack of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Jack of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/King of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/King of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/King of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/King of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/King of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/King of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/King of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/King of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Queen of Clubs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Queen of Clubs.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Queen of Diamonds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Queen of Diamonds.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Queen of Hearts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Queen of Hearts.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/Queen of Spades.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/Queen of Spades.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/images/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/images/background.png -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/sounds/cardFlip.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/sounds/cardFlip.wav -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/sounds/cardShuffle.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/sounds/cardShuffle.wav -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/sounds/ding.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/sounds/ding.wav -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/sounds/loser.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/sounds/loser.wav -------------------------------------------------------------------------------- /Chapter_12/HigherOrLower/sounds/push.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_12/HigherOrLower/sounds/push.wav -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/Constants.py: -------------------------------------------------------------------------------- 1 | # Constants 2 | 3 | BLACK = (0, 0, 0) 4 | WHITE = (255, 255, 255) 5 | PURPLE = (200, 0, 200) 6 | YELLOW = (255, 255, 0) 7 | GRAY = (50, 50, 50) 8 | 9 | # Squares and tiles are the same size 10 | SQUARE_HEIGHT = 100 11 | SQUARE_WIDTH = 100 12 | NSQUARES = 16 13 | STARTING_OPEN_SQUARE_NUMBER = NSQUARES - 1 14 | -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/Square.py: -------------------------------------------------------------------------------- 1 | # Square class 2 | 3 | import pygame 4 | from Constants import * 5 | from Tile import * 6 | 7 | class Square(): 8 | ''' 9 | A Square is a square area of the game board, in the application window. 10 | Each square has a location, rectangle, a tuple of legal moves, and a 11 | Tile that is drawn on the Square. For each user move, the game tells 12 | the clicked on Square to exchange its Tile with the blank (empty space) Square. 13 | ''' 14 | 15 | def __init__(self, window, left, top, squareNumber, legalMovesTuple): 16 | self.window = window 17 | self.rect = pygame.Rect(left, top, SQUARE_WIDTH, SQUARE_HEIGHT) 18 | self.squareNumber = squareNumber 19 | self.legalMovesTuple = legalMovesTuple 20 | self.loc = (left, top) 21 | self.reset() 22 | 23 | def reset(self): 24 | # Create starting Tile associated with this Square 25 | self.oTile = Tile(self.window, self.squareNumber) 26 | 27 | def isTileInProperPlace(self): 28 | tileNumber = self.oTile.getTileNumber() 29 | return (self.squareNumber == tileNumber) 30 | 31 | def getLegalMoves(self): 32 | return self.legalMovesTuple 33 | 34 | def clickedInside(self, mouseLoc): 35 | hit = self.rect.collidepoint(mouseLoc) 36 | return hit 37 | 38 | def getSquareNumber(self): 39 | return self.squareNumber 40 | 41 | def switch(self, oOtherSquare): 42 | # Switch the Tiles associated with two Square objects 43 | self.oTile, oOtherSquare.oTile = oOtherSquare.oTile, self.oTile 44 | 45 | def draw(self): 46 | # Tell the Tile to draw at the Square's location 47 | self.oTile.draw(self.loc) 48 | -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/images/restartButtonDown.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_13/SliderPuzzles/images/restartButtonDown.jpg -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/images/restartButtonOver.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_13/SliderPuzzles/images/restartButtonOver.jpg -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/images/restartButtonUp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_13/SliderPuzzles/images/restartButtonUp.jpg -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/sounds/applause.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_13/SliderPuzzles/sounds/applause.wav -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/sounds/buzz.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_13/SliderPuzzles/sounds/buzz.wav -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/sounds/nope.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_13/SliderPuzzles/sounds/nope.wav -------------------------------------------------------------------------------- /Chapter_13/SliderPuzzles/sounds/tick.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_13/SliderPuzzles/sounds/tick.wav -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f1.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f10.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f11.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f12.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f13.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f14.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f15.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f16.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f17.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f2.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f3.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f4.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f5.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f6.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f7.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f8.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/Dinowalk/f9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/Dinowalk/f9.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f1.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f10.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f10.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f2.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f3.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f4.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f5.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f5.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f6.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f6.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f7.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f7.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f8.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f8.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/TRex/f9.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/TRex/f9.gif -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/effect_010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/effect_010.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/male_walkcycle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/male_walkcycle.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB0.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB1.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB2.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB3.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB4.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB5.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB6.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB7.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkB8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkB8.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF0.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF1.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF2.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF3.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF4.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF5.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF6.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF7.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkF8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkF8.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL0.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL1.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL2.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL3.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL4.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL5.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL6.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL7.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkL8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkL8.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR0.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR1.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR2.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR3.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR4.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR5.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR6.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR7.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/player/walkR8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/player/walkR8.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/runLeft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/runLeft.png -------------------------------------------------------------------------------- /Chapter_14/AnimationExample/images/runRight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/AnimationExample/images/runRight.png -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/SimpleAnimation.py: -------------------------------------------------------------------------------- 1 | # SimpleAnimation class 2 | 3 | import pygame 4 | import time 5 | 6 | class SimpleAnimation(): 7 | def __init__(self, window, loc, picPaths, durationPerImage): 8 | self.window = window 9 | self.loc = loc 10 | self.imagesList = [] 11 | for picPath in picPaths: 12 | image = pygame.image.load(picPath) # load an image 13 | image = pygame.Surface.convert_alpha(image) # optimize blitting 14 | self.imagesList.append(image) 15 | 16 | self.playing = False 17 | self.durationPerImage = durationPerImage 18 | self.nImages = len(self.imagesList) 19 | self.index = 0 20 | 21 | def play(self): 22 | if self.playing: 23 | return 24 | self.playing = True 25 | self.imageStartTime = time.time() 26 | self.index = 0 27 | 28 | def update(self): 29 | if not self.playing: 30 | return 31 | 32 | # How much time has elapsed since we started showing this image 33 | self.elapsed = time.time() - self.imageStartTime 34 | 35 | # If enough time has elapsed, move onto the next image 36 | if self.elapsed > self.durationPerImage: 37 | self.index = self.index + 1 38 | 39 | if self.index < self.nImages: # move on to next image 40 | self.imageStartTime = time.time() 41 | else: # animation is finished 42 | self.playing = False 43 | self.index = 0 # reset to the beginning 44 | 45 | def draw(self): 46 | # Assumes that self.index has been set earlier - in update() method. 47 | # It is used as the index into the imagesList to find the current image. 48 | theImage = self.imagesList[self.index] # choose the image to show 49 | 50 | self.window.blit(theImage, self.loc) #show it 51 | 52 | -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f1.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f10.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f10.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f2.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f3.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f4.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f5.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f5.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f6.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f6.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f7.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f7.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f8.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f8.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleAnimation/images/Dinobike/f9.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleAnimation/images/Dinobike/f9.gif -------------------------------------------------------------------------------- /Chapter_14/SimpleSpriteSheetAnimation/Main_SimpleSpriteSheetAnimation.py: -------------------------------------------------------------------------------- 1 | # Shows example of SimpleSpriteSheetAnimation object 2 | 3 | # 1 - Import library 4 | import pygame 5 | from pygame.locals import * 6 | import sys 7 | import pygwidgets 8 | from SimpleSpriteSheetAnimation import * 9 | 10 | # 2 Define constants 11 | SCREEN_WIDTH = 640 12 | SCREEN_HEIGHT = 480 13 | FRAMES_PER_SECOND = 30 14 | BGCOLOR = (0, 128, 128) 15 | 16 | # 3 - Initialize the world 17 | pygame.init() 18 | window = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) 19 | clock = pygame.time.Clock() 20 | 21 | # 4 - Load assets: images(s), sounds, etc. 22 | 23 | # 5 - Initialize variables 24 | oWaterAnimation = SimpleSpriteSheetAnimation(window, (22, 140), 'images/water_003.png', 50, 192, 192, .05) 25 | oPlayButton = pygwidgets.TextButton(window, (60, 320), "Play") 26 | 27 | # 6 - Loop forever 28 | while True: 29 | 30 | # 7 - Check for and handle events 31 | for event in pygame.event.get(): 32 | if event.type == QUIT: 33 | pygame.quit() 34 | sys.exit() 35 | 36 | if oPlayButton.handleEvent(event): 37 | oWaterAnimation.play() 38 | 39 | # 8 - Do any "per frame" actions 40 | oWaterAnimation.update() 41 | 42 | # 9 - Clear the window 43 | window.fill(BGCOLOR) 44 | 45 | # 10 - Draw all window elements 46 | oWaterAnimation.draw() 47 | oPlayButton.draw() 48 | 49 | # 11 - Update the window 50 | pygame.display.update() 51 | 52 | # 12 - Slow things down a bit 53 | clock.tick(FRAMES_PER_SECOND) # make PyGame wait the correct amount 54 | -------------------------------------------------------------------------------- /Chapter_14/SimpleSpriteSheetAnimation/images/numbers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleSpriteSheetAnimation/images/numbers.png -------------------------------------------------------------------------------- /Chapter_14/SimpleSpriteSheetAnimation/images/water_003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_14/SimpleSpriteSheetAnimation/images/water_003.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/Paper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/Paper.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/PaperDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/PaperDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/PaperGray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/PaperGray.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/PaperOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/PaperOver.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/RPS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/RPS.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/Rock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/Rock.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/RockDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/RockDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/RockGray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/RockGray.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/RockOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/RockOver.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/Scissors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/Scissors.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/ScissorsDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/ScissorsDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/ScissorsGray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/ScissorsGray.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/ScissorsOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/ScissorsOver.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/restartButtonDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/restartButtonDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/restartButtonHighlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/restartButtonHighlight.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/restartButtonUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/restartButtonUp.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/startButtonDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/startButtonDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/startButtonHighlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/startButtonHighlight.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/images/startButtonUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/images/startButtonUp.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/sounds/buzz.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/sounds/buzz.wav -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/sounds/ding.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/sounds/ding.wav -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsStateMachine/sounds/push.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsStateMachine/sounds/push.wav -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/Constants.py: -------------------------------------------------------------------------------- 1 | # 2 | # Constants 3 | # 4 | import pygame 5 | from pygame.locals import * 6 | 7 | NEXT_SCENE_EVENT = USEREVENT + 1 8 | 9 | # Scene keys: 10 | SCENE_SPLASH = 'splash' 11 | SCENE_PLAY = 'play' 12 | SCENE_RESULTS = 'results' 13 | 14 | WHITE = (255, 255, 255) 15 | GRAY = (100, 100, 100) 16 | OTHER_GRAY = (150, 150, 150) 17 | 18 | ROCK = 'Rock' 19 | PAPER = 'Paper' 20 | SCISSORS = 'Scissors' 21 | -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/Main_RockPaperScissors.py: -------------------------------------------------------------------------------- 1 | # Demo of Scenes with Scene manager 2 | # Rock, Paper, Scissors 3 | 4 | # 1 - Import packages 5 | 6 | import pygame 7 | import pyghelpers 8 | import sys 9 | from Constants import * 10 | from SceneSplash import * 11 | from ScenePlay import * 12 | from SceneResults import * 13 | 14 | 15 | # 2 - Define constants 16 | WINDOW_WIDTH = 640 17 | WINDOW_HEIGHT = 480 18 | FRAMES_PER_SECOND = 30 19 | 20 | # 3 - Initialize the world 21 | pygame.init() 22 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 23 | clock = pygame.time.Clock() 24 | 25 | # 4 - Load assets: image(s), sounds, etc. 26 | 27 | # 5 - Initialize variables 28 | # Instantiate all scenes and store them into a list 29 | scenesList = [SceneSplash(window), 30 | ScenePlay(window), 31 | SceneResults(window)] 32 | 33 | # Create the Scene Manager, passing in the scenes list, and FPS 34 | oSceneMgr = pyghelpers.SceneMgr(scenesList, FRAMES_PER_SECOND) 35 | 36 | # Tell the scene manager to start running 37 | oSceneMgr.run() 38 | -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/SceneSplash.py: -------------------------------------------------------------------------------- 1 | # 2 | # This is the Splash Scene 3 | # 4 | # This is where the player sees the intro screen 5 | # 6 | 7 | import pygwidgets 8 | import pyghelpers 9 | import pygame 10 | from pygame.locals import * 11 | from Constants import * 12 | 13 | 14 | class SceneSplash(pyghelpers.Scene): 15 | def __init__(self, window): 16 | self.window = window 17 | 18 | self.messageField = pygwidgets.DisplayText(window, (15, 25), 'Welcome to Rock, Paper, Scissors!', \ 19 | fontSize=50, textColor=WHITE, width=610, justified='center') 20 | 21 | self.startButton = pygwidgets.CustomButton(self.window, (210, 300), \ 22 | up='images/startButtonUp.png', \ 23 | down='images/startButtonDown.png', \ 24 | over='images/startButtonHighlight.png') 25 | 26 | self.rockImage = pygwidgets.Image(window, (25, 120), 'images/Rock.png') 27 | self.paperImage = pygwidgets.Image(window, (225, 120), 'images/Paper.png') 28 | self.scissorsImage = pygwidgets.Image(window, (425, 120), 'images/Scissors.png') 29 | 30 | def getSceneKey(self): 31 | return SCENE_SPLASH 32 | 33 | def enter(self, data): 34 | pass 35 | 36 | def handleInputs(self, eventsList, keyPressedList): 37 | for event in eventsList: 38 | if self.startButton.handleEvent(event): 39 | self.goToScene(SCENE_PLAY) 40 | 41 | def update(self): 42 | pass 43 | 44 | def draw(self): 45 | self.window.fill(GRAY) 46 | self.messageField.draw() 47 | self.rockImage.draw() 48 | self.paperImage.draw() 49 | self.scissorsImage.draw() 50 | self.startButton.draw() 51 | 52 | def leave(self): 53 | return None 54 | -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/Paper.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/Paper.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/PaperDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/PaperDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/PaperOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/PaperOver.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/RPS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/RPS.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/Rock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/Rock.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/RockDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/RockDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/RockOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/RockOver.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/Scissors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/Scissors.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/ScissorsDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/ScissorsDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/ScissorsOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/ScissorsOver.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/restartButtonDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/restartButtonDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/restartButtonHighlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/restartButtonHighlight.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/restartButtonUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/restartButtonUp.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/startButtonDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/startButtonDown.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/startButtonHighlight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/startButtonHighlight.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/images/startButtonUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/images/startButtonUp.png -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/sounds/buzz.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/sounds/buzz.wav -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/sounds/ding.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/sounds/ding.wav -------------------------------------------------------------------------------- /Chapter_15/RockPaperScissorsWithScenes/sounds/push.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_15/RockPaperScissorsWithScenes/sounds/push.wav -------------------------------------------------------------------------------- /Chapter_15/SceneDemo/Constants.py: -------------------------------------------------------------------------------- 1 | # 2 | # Constants, shared by all scenes 3 | # 4 | 5 | # Scene keys (any unique values): 6 | SCENE_A = 'scene A' 7 | SCENE_B = 'scene B' 8 | SCENE_C = 'scene C' 9 | 10 | # Message ID's (any unique values): 11 | SEND_MESSAGE = 'send message' 12 | GET_DATA = 'get data' 13 | 14 | WHITE = (255, 255, 255) 15 | GRAYA = (50, 50, 50) 16 | GRAYB = (100, 100, 100) 17 | GRAYC = (150, 150, 150) 18 | -------------------------------------------------------------------------------- /Chapter_15/SceneDemo/Main_SceneDemo.py: -------------------------------------------------------------------------------- 1 | # Scene Demo main program with three scenes 2 | 3 | # This is a demo of a typical main program using the Scene Manager. 4 | # It should start by defining the size of your window, initializing pygame and creating the window. 5 | # Next, you create an instance of each of your scenes. 6 | # Then build a list of all the scenes, which would look like: 7 | # [, , ... ] 8 | # The first scene in the list will be used as the starting scene. 9 | # The ordering of the other scenes does not matter. 10 | # Using that scenesList, and a frames per second, you instantiate the SceneMgr object 11 | # Finally, you tell the SceneMgr object to run. 12 | # The SceneMgr takes over, runs the main loop, and handles navigation and communication between scenes. 13 | 14 | # 1 - Import packages 15 | import pygame 16 | import pyghelpers 17 | from SceneA import * 18 | from SceneB import * 19 | from SceneC import * 20 | 21 | # 2 - Define constants 22 | WINDOW_WIDTH = 640 23 | WINDOW_HEIGHT = 180 24 | FRAMES_PER_SECOND = 30 25 | 26 | # 3 - Initialize the world 27 | pygame.init() 28 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 29 | 30 | # 4 - Load assets: image(s), sound(s), etc. 31 | 32 | # 5 - Initialize variables 33 | # Instantiate all scenes and store them into a list 34 | scenesList = [SceneA(window), 35 | SceneB(window), 36 | SceneC(window)] 37 | 38 | # Create the Scene Manager, passing in the scenes list, and the FPS 39 | oSceneMgr = pyghelpers.SceneMgr(scenesList, FRAMES_PER_SECOND) 40 | 41 | # Tell the Scene Manager to start running 42 | oSceneMgr.run() 43 | -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/addDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/addDisabled.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/addDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/addDown.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/addNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/addNormal.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/addOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/addOver.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/cancelDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/cancelDisabled.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/cancelDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/cancelDown.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/cancelNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/cancelNormal.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/cancelOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/cancelOver.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/controlsBackground.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/controlsBackground.jpg -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/dialog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/dialog.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/highScoresBackground.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/highScoresBackground.jpg -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noDisabled.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noDown.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noNormal.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noOver.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noThanksDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noThanksDisabled.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noThanksDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noThanksDown.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noThanksNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noThanksNormal.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/noThanksOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/noThanksOver.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/okDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/okDisabled.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/okDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/okDown.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/okNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/okNormal.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/okOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/okOver.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/yesDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/yesDisabled.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/yesDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/yesDown.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/yesNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/yesNormal.png -------------------------------------------------------------------------------- /Chapter_16/DialogTester/images/yesOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/DialogTester/images/yesOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/Constants.py: -------------------------------------------------------------------------------- 1 | # Constants - used by multiple Python files 2 | 3 | WINDOW_WIDTH = 600 4 | WINDOW_HEIGHT = 700 5 | GAME_HEIGHT = 560 6 | DIALOG_BOX_OFFSET = 35 7 | DIALOG_BOX_WIDTH = WINDOW_WIDTH - (2 * DIALOG_BOX_OFFSET) 8 | 9 | # Scene keys 10 | SCENE_SPLASH = 'scene splash' 11 | SCENE_PLAY = 'scene play' 12 | SCENE_HIGH_SCORES = 'scene high scores' 13 | 14 | WHITE = (255, 255, 255) 15 | BLACK = (0, 0, 0) 16 | 17 | POINTS_FOR_GOODIE = 25 18 | POINTS_FOR_BADDIE_EVADED = 1 19 | HIGH_SCORES_DATA = 'high scores data' 20 | N_HIGH_SCORES = 10 -------------------------------------------------------------------------------- /Chapter_16/Dodger/HighScores.json: -------------------------------------------------------------------------------- 1 | [["-----", 0], ["-----", 0], ["-----", 0], ["-----", 0], ["-----", 0], ["-----", 0], ["-----", 0], ["-----", 0], ["-----", 0], ["-----", 0]] -------------------------------------------------------------------------------- /Chapter_16/Dodger/Main_Dodger.py: -------------------------------------------------------------------------------- 1 | # Dodger main program 2 | # 3 | # Instantiates 3 scenes, creates and starts the scene manager 4 | # 5 | # Original version by Al Sweigart from his book "Invent With Python" 6 | # (concept, graphics, and sounds used by permission from Al Sweigart) 7 | 8 | # 1 - Import packages 9 | import os 10 | # The next line is here just in case you are running from the command line 11 | os.chdir(os.path.dirname(os.path.abspath(__file__))) 12 | 13 | import pygame 14 | import pyghelpers 15 | from SceneSplash import * 16 | from ScenePlay import * 17 | from SceneHighScores import * 18 | 19 | # 2 - Define constants 20 | FRAMES_PER_SECOND = 40 21 | 22 | # 3 - Initialize the world 23 | pygame.init() 24 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 25 | 26 | # 4 - Load assets: image(s), sounds, etc. 27 | 28 | # 5 - Initialize variables 29 | # Instantiate all scenes and store them in a list 30 | scenesList = [SceneSplash(window), 31 | SceneHighScores(window), 32 | ScenePlay(window)] 33 | 34 | # Create the scene manager, passing in the scenes list and the FPS 35 | oSceneMgr = pyghelpers.SceneMgr(scenesList, FRAMES_PER_SECOND) 36 | 37 | # Tell the Scene Manager to start running 38 | oSceneMgr.run() 39 | -------------------------------------------------------------------------------- /Chapter_16/Dodger/Player.py: -------------------------------------------------------------------------------- 1 | # Player 2 | import pygame 3 | import pygwidgets 4 | from Constants import * 5 | 6 | class Player(): 7 | def __init__(self, window): 8 | self.window = window 9 | self.image = pygwidgets.Image(window, 10 | (-100, -100), 'images/player.png') 11 | playerRect = self.image.getRect() 12 | self.maxX = WINDOW_WIDTH - playerRect.width 13 | self.maxY = GAME_HEIGHT - playerRect.height 14 | 15 | # Every frame, move the Player icon to the mouse position 16 | # Limits the x- and y-coordinates to the game area of the window 17 | def update(self, x, y): 18 | if x < 0: 19 | x = 0 20 | elif x > self.maxX: 21 | x = self.maxX 22 | if y < 0: 23 | y = 0 24 | elif y > self.maxY: 25 | y = self.maxY 26 | 27 | self.image.setLoc((x, y)) 28 | return self.image.getRect() 29 | 30 | def draw(self): 31 | self.image.draw() 32 | -------------------------------------------------------------------------------- /Chapter_16/Dodger/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/__init__.py -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/addDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/addDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/addDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/addDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/addNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/addNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/addOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/addOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/backDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/backDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/backDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/backDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/backNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/backNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/backOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/backOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/baddie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/baddie.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/cancelDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/cancelDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/cancelDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/cancelDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/cancelNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/cancelNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/cancelOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/cancelOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/controlsBackground.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/controlsBackground.jpg -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/dialog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/dialog.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/dodger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/dodger.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/gameOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/gameOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/goodie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/goodie.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/gotoHighScoresDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/gotoHighScoresDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/gotoHighScoresDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/gotoHighScoresDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/gotoHighScoresNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/gotoHighScoresNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/gotoHighScoresOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/gotoHighScoresOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/highScoresBackground.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/highScoresBackground.jpg -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/highScoresDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/highScoresDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/highScoresDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/highScoresDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/highScoresNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/highScoresNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/highScoresOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/highScoresOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/noThanksDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/noThanksDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/noThanksDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/noThanksDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/noThanksNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/noThanksNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/noThanksOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/noThanksOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/okDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/okDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/okDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/okDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/okNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/okNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/okOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/okOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/player.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/player.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/quitDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/quitDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/quitDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/quitDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/quitNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/quitNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/quitOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/quitOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/resetDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/resetDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/resetDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/resetDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/resetNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/resetNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/resetOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/resetOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/splashBackground.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/splashBackground.jpg -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startNewDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startNewDisabled.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startNewDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startNewDown.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startNewNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startNewNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startNewOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startNewOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startNormal.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/images/startOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/images/startOver.png -------------------------------------------------------------------------------- /Chapter_16/Dodger/sounds/background.mid: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/sounds/background.mid -------------------------------------------------------------------------------- /Chapter_16/Dodger/sounds/ding.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/sounds/ding.wav -------------------------------------------------------------------------------- /Chapter_16/Dodger/sounds/gameover.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_16/Dodger/sounds/gameover.wav -------------------------------------------------------------------------------- /Chapter_17/MVC_RollTheDice/BarView.py: -------------------------------------------------------------------------------- 1 | # BarView - Roll The Dice 2 | 3 | from Bin import * 4 | from Constants import * 5 | 6 | class BarView(): 7 | def __init__(self, window, oModel): 8 | self.window = window 9 | self.oModel = oModel 10 | 11 | self.oRollTotal = pygwidgets.DisplayText(self.window, (50, 406), 'Roll total:', 12 | fontName='arial', fontSize=16, justified='right', width=80) 13 | self.oCount = pygwidgets.DisplayText(self.window, (50, 441), 'Count:', 14 | fontName='arial', fontSize=16, justified='right', width=80) 15 | self.oPercent = pygwidgets.DisplayText(self.window, (50, 471), 'Percent:', 16 | fontName='arial', fontSize=16, justified='right', width=80) 17 | 18 | self.oBinsDict = {} 19 | # Possible rolls only go from 2 to 12 20 | for rollTotal in range(MIN_TOTAL, MAX_TOTAL_PLUS_1): 21 | oBin = Bin(self.window, rollTotal) 22 | self.oBinsDict[rollTotal] = oBin 23 | 24 | def update(self): 25 | nRounds, resultsDict, percentsDict = self.oModel.getRoundsRollsPercents() 26 | for rollTotal in range(MIN_TOTAL, MAX_TOTAL_PLUS_1): 27 | thisResult = resultsDict[rollTotal] 28 | thisPercent = percentsDict[rollTotal] 29 | oBin = self.oBinsDict[rollTotal] 30 | oBin.update(nRounds, thisResult, thisPercent) 31 | 32 | def draw(self): 33 | self.oRollTotal.draw() 34 | self.oCount.draw() 35 | self.oPercent.draw() 36 | for oBin in self.oBinsDict.values(): 37 | oBin.draw() 38 | -------------------------------------------------------------------------------- /Chapter_17/MVC_RollTheDice/Bin.py: -------------------------------------------------------------------------------- 1 | # Bin - Roll The Dice 2 | 3 | import pygame 4 | import pygwidgets 5 | 6 | MAX_BAR_HEIGHT = 300 7 | BAR_BOTTOM = 390 8 | BAR_WIDTH = 30 9 | BAR_COLOR = (128, 128, 128) 10 | COLUMN_LEFT_START = 30 11 | COLUMN_OFFSET = 55 12 | 13 | # Bin Class 14 | class Bin(): 15 | def __init__(self, window, binNumber): 16 | self.window = window 17 | self.pixelsPerCount = MAX_BAR_HEIGHT 18 | 19 | self.left = COLUMN_LEFT_START + (binNumber * COLUMN_OFFSET) 20 | self.oBinLabel = pygwidgets.DisplayText(window, 21 | (self.left + 3, BAR_BOTTOM + 12), binNumber, 22 | fontName='arial', fontSize=24, width=25, justified='center') 23 | self.oBinCount = pygwidgets.DisplayText(window, 24 | (self.left - 5, BAR_BOTTOM + 50), '', 25 | fontName='arial', fontSize=18, width=50, justified='center') 26 | self.oBinPercent = pygwidgets.DisplayText(window, 27 | (self.left - 5, BAR_BOTTOM + 80), '', 28 | fontName='arial', fontSize=18, width=50, justified='right') 29 | 30 | def update(self, nRounds, count, percent): 31 | self.oBinCount.setValue(count) 32 | percent = '{:.1%}'.format(percent) 33 | self.oBinPercent.setValue(percent) 34 | 35 | # force float here, use int when drawing rects 36 | # Calculate the real height, multiply by two to make it look better 37 | # All bars will certainly be less than 50% 38 | self.nPixelsPerTrial = float(MAX_BAR_HEIGHT) / nRounds 39 | barHeight = int(count * self.nPixelsPerTrial) * 2 40 | self.rect = pygame.Rect(self.left, BAR_BOTTOM - barHeight, BAR_WIDTH, barHeight) 41 | 42 | def draw(self): 43 | pygame.draw.rect(self.window, BAR_COLOR, self.rect, 0) 44 | self.oBinLabel.draw() 45 | self.oBinCount.draw() 46 | self.oBinPercent.draw() 47 | -------------------------------------------------------------------------------- /Chapter_17/MVC_RollTheDice/Constants.py: -------------------------------------------------------------------------------- 1 | # Constants 2 | 3 | WINDOW_WIDTH = 800 4 | WINDOW_HEIGHT = 650 5 | 6 | VIEW_BAR_VIEW = 'bar' 7 | VIEW_PIE_VIEW = 'pie' 8 | VIEW_TEXT_VIEW = 'text' 9 | 10 | MIN_TOTAL = 2 11 | MAX_TOTAL = 12 12 | MAX_TOTAL_PLUS_1 = MAX_TOTAL + 1 13 | -------------------------------------------------------------------------------- /Chapter_17/MVC_RollTheDice/Main_MVC.py: -------------------------------------------------------------------------------- 1 | # Main MVC Roll The Dice - Irv Kalb 2 | 3 | # 1 - Import packages 4 | # import pygame 5 | import pygwidgets 6 | import sys 7 | from Constants import * 8 | from Controller import * 9 | 10 | # 2 - Define constants 11 | FRAMES_PER_SECOND = 30 12 | 13 | # 3 - Initialize the world 14 | pygame.init() 15 | clock = pygame.time.Clock() 16 | pygame.display.set_caption('Roll Them Dice') 17 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 18 | 19 | # 4 - Load assets: image(s), sounds, etc. 20 | 21 | # 5 - Initialize variables 22 | # Instantiate the Controller object 23 | oController = Controller(window) 24 | 25 | # 6 - Loop forever 26 | while True: 27 | # 7 - Check for and handle events 28 | for event in pygame.event.get(): 29 | if event.type == pygame.QUIT: 30 | pygame.quit() 31 | sys.exit() 32 | # Pass all events to the Controller 33 | oController.handleEvent(event) 34 | 35 | # 8 - Do any "per frame" actions 36 | 37 | # 9 - Clear the window (let the Controller do it) 38 | # 10 - Draw the window elements 39 | oController.draw() 40 | 41 | # 11 - Update the widow 42 | pygame.display.update() 43 | 44 | # 12 - Slow things down 45 | clock.tick(FRAMES_PER_SECOND) # make pygame wait 46 | -------------------------------------------------------------------------------- /Chapter_17/MVC_RollTheDice/Model.py: -------------------------------------------------------------------------------- 1 | # Model - Roll The Dice 2 | 3 | import random 4 | from Constants import * 5 | 6 | SIDES_PER_DIE = 6 7 | SIDES_PER_DIE_PLUS_ONE = SIDES_PER_DIE + 1 8 | 9 | # Model Class 10 | class Model(): 11 | def __init__(self): 12 | self.nRounds = 0 13 | self.rollsDict = {} 14 | self.percentsDict = {} 15 | 16 | def generateRolls(self, nRounds): 17 | self.nRounds = nRounds 18 | self.rollsDict = {} 19 | for total in range(MIN_TOTAL, MAX_TOTAL_PLUS_1): # Initialize all to zero 20 | self.rollsDict[total] = 0 21 | 22 | # Roll two dice, add them up, increment the count in the dict 23 | for roundNumber in range(0, self.nRounds): 24 | die1 = random.randrange(1, SIDES_PER_DIE_PLUS_ONE) 25 | die2 = random.randrange(1, SIDES_PER_DIE_PLUS_ONE) 26 | theSum = die1 + die2 27 | self.rollsDict[theSum] = self.rollsDict[theSum] + 1 28 | 29 | # Calculate and save percentages in a dict 30 | self.percentsDict = {} 31 | for rollTotal, count in self.rollsDict.items(): 32 | thisPercent = count / self.nRounds 33 | self.percentsDict[rollTotal] = thisPercent 34 | 35 | # All current views call this method to get all the data. 36 | def getRoundsRollsPercents(self): 37 | return self.nRounds, self.rollsDict, self.percentsDict 38 | 39 | # The methods below aren't used right now, but are available for new views 40 | def getNumberOfRounds(self): 41 | return self.nRounds 42 | 43 | def getRolls(self): 44 | return self.rollsDict 45 | 46 | def getPercents(self): 47 | return self.percentsDict 48 | -------------------------------------------------------------------------------- /Chapter_17/MVC_RollTheDice/TextView.py: -------------------------------------------------------------------------------- 1 | # TextChart - Roll The Dice 2 | 3 | import pygwidgets 4 | from Constants import * 5 | 6 | class TextView(): 7 | def __init__(self, window, oModel): 8 | self.window = window 9 | self.oModel = oModel 10 | 11 | totalText = ['Roll total', ''] 12 | for rollTotal in range(MIN_TOTAL, MAX_TOTAL_PLUS_1): 13 | totalText.append(rollTotal) 14 | self.oTotalDisplay = pygwidgets.DisplayText(self.window, (200, 135), totalText, 15 | fontSize=36, width=120, justified='right') 16 | self.oCountDisplay = pygwidgets.DisplayText(self.window, (320, 135), 17 | fontSize=36, width=120, justified='right') 18 | self.oPercentDisplay = pygwidgets.DisplayText(self.window, (440, 135), 19 | fontSize=36, width=120, justified='right') 20 | 21 | def update(self): 22 | nRounds, resultsDict, percentsDict = self.oModel.getRoundsRollsPercents() 23 | 24 | countList = ['Count', ''] # extra empty string for a blank line 25 | percentList = ['Percent', ''] 26 | for rollTotal in range(MIN_TOTAL, MAX_TOTAL_PLUS_1): 27 | count = resultsDict[rollTotal] 28 | percent = percentsDict[rollTotal] 29 | 30 | countList.append(count) 31 | # Build percent as a string with one decimal digit 32 | percent = '{:.1%}'.format(percent) 33 | percentList.append(percent) 34 | 35 | self.oCountDisplay.setValue(countList) 36 | self.oPercentDisplay.setValue(percentList) 37 | 38 | def draw(self): 39 | self.oTotalDisplay.draw() 40 | self.oCountDisplay.draw() 41 | self.oPercentDisplay.draw() 42 | -------------------------------------------------------------------------------- /Chapter_17/MVC_RollTheDice/images/twoDice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_17/MVC_RollTheDice/images/twoDice.png -------------------------------------------------------------------------------- /Chapter_2/DimmerSwitch.py: -------------------------------------------------------------------------------- 1 | # DimmerSwitch class 2 | 3 | class DimmerSwitch(): 4 | def __init__(self): 5 | self.switchIsOn = False 6 | self.brightness = 0 7 | 8 | def turnOn(self): 9 | self.switchIsOn = True 10 | 11 | def turnOff(self): 12 | self.switchIsOn = False 13 | 14 | def raiseLevel(self): 15 | if self.brightness < 10: 16 | self.brightness = self.brightness + 1 17 | 18 | def lowerLevel(self): 19 | if self.brightness > 0: 20 | self.brightness = self.brightness - 1 21 | 22 | # Extra method for debugging 23 | def show(self): 24 | print('Switch is on?', self.switchIsOn) 25 | print('Brightness is:', self.brightness) 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter_2/LightSwitch_Procedural.py: -------------------------------------------------------------------------------- 1 | # Procedural light switch 2 | 3 | def turnOn(): 4 | global switchIsOn 5 | # turn the light on 6 | switchIsOn = True 7 | 8 | def turnOff(): 9 | global switchIsOn 10 | # turn the light off 11 | switchIsOn = False 12 | 13 | # Main code 14 | switchIsOn = False # a global Boolean variable 15 | 16 | # Test code 17 | print(switchIsOn) 18 | turnOn() 19 | print(switchIsOn) 20 | turnOff() 21 | print(switchIsOn) 22 | turnOn() 23 | print(switchIsOn) 24 | -------------------------------------------------------------------------------- /Chapter_2/OO_DimmerSwitch_Two_Instances.py: -------------------------------------------------------------------------------- 1 | # DimmerSwitch class with Test Code 2 | 3 | # DimmerSwitch class 4 | 5 | class DimmerSwitch(): 6 | def __init__(self): 7 | self.isOn = False 8 | self.brightness = 0 9 | 10 | def turnOn(self): 11 | self.isOn = True 12 | 13 | def turnOff(self): 14 | self.isOn = False 15 | 16 | def raiseLevel(self): 17 | if self.brightness < 10: 18 | self.brightness = self.brightness + 1 19 | 20 | def lowerLevel(self): 21 | if self.brightness > 0: 22 | self.brightness = self.brightness - 1 23 | 24 | # Extra method for debugging 25 | def showState(self): 26 | print('Switch is on?', self.isOn) 27 | print('Brightness is:', self.brightness) 28 | 29 | # Main code 30 | 31 | oDimmer1 = DimmerSwitch( ) 32 | oDimmer2 = DimmerSwitch( ) 33 | 34 | print(type(oDimmer1)) 35 | 36 | oDimmer1.turnOn() 37 | oDimmer1.raiseLevel() 38 | oDimmer1.raiseLevel() 39 | 40 | oDimmer2.raiseLevel() 41 | oDimmer2.raiseLevel() 42 | oDimmer2.raiseLevel() 43 | oDimmer2.raiseLevel() 44 | oDimmer2.raiseLevel() 45 | 46 | oDimmer1.showState() 47 | oDimmer2.showState() 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Chapter_2/OO_DimmerSwitch_with_Test_Code.py: -------------------------------------------------------------------------------- 1 | # DimmerSwitch class with test code 2 | 3 | class DimmerSwitch(): 4 | def __init__(self): 5 | self.switchIsOn = False 6 | self.brightness = 0 7 | 8 | def turnOn(self): 9 | self.switchIsOn = True 10 | 11 | def turnOff(self): 12 | self.switchIsOn = False 13 | 14 | def raiseLevel(self): 15 | if self.brightness < 10: 16 | self.brightness = self.brightness + 1 17 | 18 | def lowerLevel(self): 19 | if self.brightness > 0: 20 | self.brightness = self.brightness - 1 21 | 22 | # Extra method for debugging 23 | def show(self): 24 | print('Switch is on?', self.switchIsOn) 25 | print('Brightness is:', self.brightness) 26 | 27 | # Main code 28 | oDimmer = DimmerSwitch() 29 | 30 | # Turn switch on, and raise the level 5 times 31 | oDimmer.turnOn() 32 | oDimmer.raiseLevel() 33 | oDimmer.raiseLevel() 34 | oDimmer.raiseLevel() 35 | oDimmer.raiseLevel() 36 | oDimmer.raiseLevel() 37 | oDimmer.show() 38 | 39 | # Lower the level 2 times, and turn off 40 | oDimmer.lowerLevel() 41 | oDimmer.lowerLevel() 42 | oDimmer.turnOff() 43 | oDimmer.show() 44 | 45 | # Turn switch on, and raise the level 3 times 46 | oDimmer.turnOn() 47 | oDimmer.raiseLevel() 48 | oDimmer.raiseLevel() 49 | oDimmer.raiseLevel() 50 | oDimmer.show() 51 | -------------------------------------------------------------------------------- /Chapter_2/OO_LightSwitch_Two_Instances.py: -------------------------------------------------------------------------------- 1 | # OO_LightSwitch 2 | 3 | class LightSwitch(): 4 | def __init__(self): 5 | self.switchIsOn = False 6 | 7 | def turnOn(self): 8 | # turn the switch on 9 | self.switchIsOn = True 10 | 11 | def turnOff(self): 12 | # turn the switch off 13 | self.switchIsOn = False 14 | 15 | def show(self): # added for testing 16 | print(self.switchIsOn) 17 | 18 | # Main code 19 | oLightSwitch1 = LightSwitch() # create a LightSwitch object 20 | oLightSwitch2 = LightSwitch() # create another LightSwitch object 21 | 22 | # Test code 23 | oLightSwitch1.show() 24 | oLightSwitch2.show() 25 | oLightSwitch1.turnOn() # Turn switch 1 on 26 | # Switch 2 should be off at start, but this makes it clearer 27 | oLightSwitch2.turnOff() 28 | oLightSwitch1.show() 29 | oLightSwitch2.show() 30 | 31 | -------------------------------------------------------------------------------- /Chapter_2/OO_LightSwitch_with_Test_Code.py: -------------------------------------------------------------------------------- 1 | # OO_LightSwitch 2 | 3 | class LightSwitch(): 4 | def __init__(self): 5 | self.switchIsOn = False 6 | 7 | def turnOn(self): 8 | # turn the switch on 9 | self.switchIsOn = True 10 | 11 | def turnOff(self): 12 | # turn the switch off 13 | self.switchIsOn = False 14 | 15 | def show(self): # added for testing 16 | print(self.switchIsOn) 17 | 18 | # Main code 19 | oLightSwitch = LightSwitch() # create a LightSwitch object 20 | 21 | # Calls to methods 22 | oLightSwitch.show() # call the show method of oLightSwitch 23 | oLightSwitch.turnOn() # call the turnOn method of oLightSwitch 24 | oLightSwitch.show() 25 | oLightSwitch.turnOff() # call the turnOff method of oLightSwitch 26 | oLightSwitch.show() 27 | oLightSwitch.turnOn() # call the turnOn method of oLightSwitch 28 | oLightSwitch.show() 29 | -------------------------------------------------------------------------------- /Chapter_3/OO_DimmerSwitch_Model1.py: -------------------------------------------------------------------------------- 1 | # Dimmer Switch class 2 | 3 | class DimmerSwitch(): 4 | def __init__(self, label): 5 | self.label = label 6 | self.isOn = False 7 | self.brightness = 0 8 | 9 | def turnOn(self): 10 | self.isOn = True 11 | # turn the light on at self.brightness 12 | 13 | def turnOff(self): 14 | self.isOn = False 15 | # turn the light off 16 | 17 | def raiseLevel(self): 18 | if self.brightness < 10: 19 | self.brightness = self.brightness + 1 20 | 21 | def lowerLevel(self): 22 | if self.brightness > 0: 23 | self.brightness = self.brightness - 1 24 | 25 | # Extra method for debugging 26 | def show(self): 27 | print('Label:', self.label) 28 | print('Light is on?', self.isOn) 29 | print('Brightness is:', self.brightness) 30 | print() 31 | 32 | 33 | # Main code 34 | 35 | # Create first DimmerSwitch, turn on and raise level twice 36 | oDimmer1 = DimmerSwitch('Dimmer1') 37 | oDimmer1.turnOn() 38 | oDimmer1.raiseLevel() 39 | oDimmer1.raiseLevel() 40 | 41 | # Create second DimmerSwitch, turn on and raise level 3 times 42 | oDimmer2 = DimmerSwitch('Dimmer2') 43 | oDimmer2.turnOn() 44 | oDimmer2.raiseLevel() 45 | oDimmer2.raiseLevel() 46 | oDimmer2.raiseLevel() 47 | 48 | # Create third DimmerSwitch, using the default settings 49 | oDimmer3 = DimmerSwitch('Dimmer3') 50 | 51 | # Ask each switch to show itself 52 | oDimmer1.show() 53 | oDimmer2.show() 54 | oDimmer3.show() 55 | -------------------------------------------------------------------------------- /Chapter_3/OO_DimmerSwitch_Model2_Instantiation.py: -------------------------------------------------------------------------------- 1 | # Dimmer Switch class 2 | 3 | class DimmerSwitch(): 4 | def __init__(self, label): 5 | self.label = label 6 | self.isOn = False 7 | self.brightness = 0 8 | 9 | def turnOn(self): 10 | self.isOn = True 11 | # turn the light on at self.brightness 12 | 13 | def turnOff(self): 14 | self.isOn = False 15 | # turn the light off 16 | 17 | def raiseLevel(self): 18 | if self.brightness < 10: 19 | self.brightness = self.brightness + 1 20 | 21 | def lowerLevel(self): 22 | if self.brightness > 0: 23 | self.brightness = self.brightness - 1 24 | 25 | # Extra method for debugging 26 | def show(self): 27 | print('Label:', self.label) 28 | print('Light is on?', self.isOn) 29 | print('Brightness is:', self.brightness) 30 | print() 31 | 32 | 33 | # Main code 34 | 35 | # Create three DimmerSwitch objects 36 | oDimmer1 = DimmerSwitch('Dimmer1') 37 | print(type(oDimmer1)) 38 | print(oDimmer1) 39 | print() 40 | oDimmer2 = DimmerSwitch('Dimmer2') 41 | print(type(oDimmer2)) 42 | print(oDimmer2) 43 | print() 44 | oDimmer3 = DimmerSwitch('Dimmer3') 45 | print(type(oDimmer3)) 46 | print(oDimmer3) 47 | print() 48 | -------------------------------------------------------------------------------- /Chapter_3/OO_DimmerSwitch_Model2_Method_Calls.py: -------------------------------------------------------------------------------- 1 | # Dimmer Switch class 2 | 3 | class DimmerSwitch(): 4 | def __init__(self, label): 5 | self.label = label 6 | self.isOn = False 7 | self.brightness = 0 8 | 9 | def turnOn(self): 10 | self.isOn = True 11 | # turn the light on at self.brightness 12 | 13 | def turnOff(self): 14 | self.isOn = False 15 | # turn the light off 16 | 17 | def raiseLevel(self): 18 | if self.brightness < 10: 19 | self.brightness = self.brightness + 1 20 | 21 | def lowerLevel(self): 22 | if self.brightness > 0: 23 | self.brightness = self.brightness - 1 24 | 25 | # Extra method for debugging 26 | def show(self): 27 | print('Label:', self.label) 28 | print('Light is on?', self.isOn) 29 | print('Brightness is:', self.brightness) 30 | print() 31 | 32 | 33 | # Main code (to demo with Python Tutor) 34 | 35 | # Create two DimmerSwitch objects 36 | oDimmer1 = DimmerSwitch('Dimmer1') 37 | oDimmer2 = DimmerSwitch('Dimmer2') 38 | 39 | # Tell oDimmer1 to raise its level 40 | oDimmer1.raiseLevel() 41 | 42 | # Tell oDimmer2 to raise its level 43 | oDimmer2.raiseLevel() 44 | -------------------------------------------------------------------------------- /Chapter_4/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | 3 | class Account(): 4 | def __init__(self, name, balance, password): 5 | self.name = name 6 | self.balance = int(balance) 7 | self.password = password 8 | 9 | def deposit(self, amountToDeposit, password): 10 | if password != self.password: 11 | print('Sorry, incorrect password') 12 | return None 13 | 14 | if amountToDeposit < 0: 15 | print('You cannot deposit a negative amount') 16 | return None 17 | 18 | self.balance = self.balance + amountToDeposit 19 | return self.balance 20 | 21 | def withdraw(self, amountToWithdraw, password): 22 | if password != self.password: 23 | print('Incorrect password for this account') 24 | return None 25 | 26 | if amountToWithdraw < 0: 27 | print('You cannot withdraw a negative amount') 28 | return None 29 | 30 | if amountToWithdraw > self.balance: 31 | print('You cannot withdraw more than you have in your account') 32 | return None 33 | 34 | self.balance = self.balance - amountToWithdraw 35 | return self.balance 36 | 37 | def getBalance(self, password): 38 | if password != self.password: 39 | print('Sorry, incorrect password') 40 | return None 41 | return self.balance 42 | 43 | # Added for debugging 44 | def show(self): 45 | print(' Name:', self.name) 46 | print(' Balance:', self.balance) 47 | print(' Password:', self.password) 48 | print() 49 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP1_IndividualVariables/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bank 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP1_IndividualVariables/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | /Bank/src 7 | 8 | python 2.7 9 | Default 10 | 11 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP1_IndividualVariables/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | 3 | class Account(): 4 | def __init__(self, name, balance, password): 5 | self.name = name 6 | self.balance = int(balance) 7 | self.password = password 8 | 9 | def deposit(self, amountToDeposit, password): 10 | if password != self.password: 11 | print('Sorry, incorrect password') 12 | return None 13 | 14 | if amountToDeposit < 0: 15 | print('You cannot deposit a negative amount') 16 | return None 17 | 18 | self.balance = self.balance + amountToDeposit 19 | return self.balance 20 | 21 | def withdraw(self, amountToWithdraw, password): 22 | if password != self.password: 23 | print('Incorrect password for this account') 24 | return None 25 | 26 | if amountToWithdraw < 0: 27 | print('You cannot withdraw a negative amount') 28 | return None 29 | 30 | if amountToWithdraw > self.balance: 31 | print('You cannot withdraw more than you have in your account') 32 | return None 33 | 34 | self.balance = self.balance - amountToWithdraw 35 | return self.balance 36 | 37 | def getBalance(self, password): 38 | if password != self.password: 39 | print('Sorry, incorrect password') 40 | return None 41 | return self.balance 42 | 43 | # Added for debugging 44 | def show(self): 45 | print(' Name:', self.name) 46 | print(' Balance:', self.balance) 47 | print(' Password:', self.password) 48 | print() 49 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP1_IndividualVariables/Main_Bank_Version1.py: -------------------------------------------------------------------------------- 1 | # Test program using accounts 2 | # Version 1, using explicit variables for each Account object 3 | 4 | # Bring in all the code from the Account class file 5 | from Account import * 6 | 7 | # Create two accounts 8 | oJoesAccount = Account('Joe', 100, 'JoesPassword') 9 | print("Created an account for Joe") 10 | 11 | oMarysAccount = Account('Mary', 12345, 'MarysPassword') 12 | print("Created an account for Mary") 13 | 14 | oJoesAccount.show() 15 | oMarysAccount.show() 16 | print() 17 | 18 | # Call some methods on the different accounts 19 | print('Calling methods of the two accounts ...') 20 | oJoesAccount.deposit(50, 'JoesPassword') 21 | oMarysAccount.withdraw(345, 'MarysPassword') 22 | oMarysAccount.deposit(100, 'MarysPassword') 23 | 24 | # Show the accounts 25 | oJoesAccount.show() 26 | oMarysAccount.show() 27 | 28 | # Create another account with information from the user 29 | print() 30 | userName = input('What is the name for the new user account? ') 31 | userBalance = input('What is the starting balance for this account? ') 32 | userBalance = int(userBalance) 33 | userPassword = input('What is the password you want to use for this account? ') 34 | oNewAccount = Account(userName, userBalance, userPassword) 35 | 36 | # Show the newly created user account 37 | oNewAccount.show() 38 | 39 | # Let's deposit 100 into the new account 40 | oNewAccount.deposit(100, userPassword) 41 | usersBalance = oNewAccount.getBalance(userPassword) 42 | print() 43 | print("After depositing 100, user's balance is", usersBalance) 44 | 45 | # Show the new account 46 | oNewAccount.show() 47 | 48 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP2_ListOfAccountObjects/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bank 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP2_ListOfAccountObjects/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | /Bank/src 7 | 8 | python 2.7 9 | Default 10 | 11 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP2_ListOfAccountObjects/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | 3 | class Account(): 4 | def __init__(self, name, balance, password): 5 | self.name = name 6 | self.balance = int(balance) 7 | self.password = password 8 | 9 | def deposit(self, amountToDeposit, password): 10 | if password != self.password: 11 | print('Sorry, incorrect password') 12 | return None 13 | 14 | if amountToDeposit < 0: 15 | print('You cannot deposit a negative amount') 16 | return None 17 | 18 | self.balance = self.balance + amountToDeposit 19 | return self.balance 20 | 21 | def withdraw(self, amountToWithdraw, password): 22 | if password != self.password: 23 | print('Incorrect password for this account') 24 | return None 25 | 26 | if amountToWithdraw < 0: 27 | print('You cannot withdraw a negative amount') 28 | return None 29 | 30 | if amountToWithdraw > self.balance: 31 | print('You cannot withdraw more than you have in your account') 32 | return None 33 | 34 | self.balance = self.balance - amountToWithdraw 35 | return self.balance 36 | 37 | def getBalance(self, password): 38 | if password != self.password: 39 | print('Sorry, incorrect password') 40 | return None 41 | return self.balance 42 | 43 | # Added for debugging 44 | def show(self): 45 | print(' Name:', self.name) 46 | print(' Balance:', self.balance) 47 | print(' Password:', self.password) 48 | print() 49 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP2_ListOfAccountObjects/Main_Bank_Version2.py: -------------------------------------------------------------------------------- 1 | # Test program using accounts 2 | # Version 2, using a list of accounts 3 | 4 | # Bring in all the code from the Account class file 5 | from Account import * 6 | 7 | # Start off with empty list of accounts 8 | accountsList = [ ] 9 | 10 | # Create two accounts 11 | oAccount = Account('Joe', 100, 'JoesPassword') 12 | accountsList.append(oAccount) 13 | print("Joe's account number is 0") 14 | 15 | oAccount = Account('Mary', 12345, 'MarysPassword') 16 | accountsList.append(oAccount) 17 | print("Mary's account number is 1") 18 | 19 | accountsList[0].show() 20 | accountsList[1].show() 21 | print() 22 | 23 | # Call some methods on the different accounts 24 | print('Calling methods of the two accounts ...') 25 | accountsList[0].deposit(50, 'JoesPassword') 26 | accountsList[1].withdraw(345, 'MarysPassword') 27 | accountsList[1].deposit(100, 'MarysPassword') 28 | 29 | # Show the accounts 30 | accountsList[0].show() 31 | accountsList[1].show() 32 | 33 | # Create another account with information from the user 34 | print() 35 | userName = input('What is the name for the new user account? ') 36 | userBalance = input('What is the starting balance for this account? ') 37 | userBalance = int(userBalance) 38 | userPassword = input('What is the password you want to use for this account? ') 39 | oAccount = Account(userName, userBalance, userPassword) 40 | accountsList.append(oAccount) # append to list of accounts 41 | 42 | # Show the newly created user account 43 | print('Created new account, account number is 2') 44 | accountsList[2].show() 45 | 46 | # Let's deposit 100 into the new account 47 | accountsList[2].deposit(100, userPassword) 48 | usersBalance = accountsList[2].getBalance(userPassword) 49 | print() 50 | print("After depositing 100, user's balance is:", usersBalance) 51 | 52 | # Show the new account 53 | accountsList[2].show() 54 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP3_DictionaryOfAcountObjects/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bank 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP3_DictionaryOfAcountObjects/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | /Bank/src 7 | 8 | python 2.7 9 | Default 10 | 11 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP3_DictionaryOfAcountObjects/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | 3 | class Account(): 4 | def __init__(self, name, balance, password): 5 | self.name = name 6 | self.balance = int(balance) 7 | self.password = password 8 | 9 | def deposit(self, amountToDeposit, password): 10 | if password != self.password: 11 | print('Sorry, incorrect password') 12 | return None 13 | 14 | if amountToDeposit < 0: 15 | print('You cannot deposit a negative amount') 16 | return None 17 | 18 | self.balance = self.balance + amountToDeposit 19 | return self.balance 20 | 21 | def withdraw(self, amountToWithdraw, password): 22 | if password != self.password: 23 | print('Incorrect password for this account') 24 | return None 25 | 26 | if amountToWithdraw < 0: 27 | print('You cannot withdraw a negative amount') 28 | return None 29 | 30 | if amountToWithdraw > self.balance: 31 | print('You cannot withdraw more than you have in your account') 32 | return None 33 | 34 | self.balance = self.balance - amountToWithdraw 35 | return self.balance 36 | 37 | def getBalance(self, password): 38 | if password != self.password: 39 | print('Sorry, incorrect password') 40 | return None 41 | return self.balance 42 | 43 | # Added for debugging 44 | def show(self): 45 | print(' Name:', self.name) 46 | print(' Balance:', self.balance) 47 | print(' Password:', self.password) 48 | print() 49 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP4_InteractiveMenu/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bank 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP4_InteractiveMenu/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | /Bank/src 7 | 8 | python 2.7 9 | Default 10 | 11 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP4_InteractiveMenu/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | 3 | class Account(): 4 | def __init__(self, name, balance, password): 5 | self.name = name 6 | self.balance = int(balance) 7 | self.password = password 8 | 9 | def deposit(self, amountToDeposit, password): 10 | if password != self.password: 11 | print('Sorry, incorrect password') 12 | return None 13 | 14 | if amountToDeposit < 0: 15 | print('You cannot deposit a negative amount') 16 | return None 17 | 18 | self.balance = self.balance + amountToDeposit 19 | return self.balance 20 | 21 | def withdraw(self, amountToWithdraw, password): 22 | if password != self.password: 23 | print('Incorrect password for this account') 24 | return None 25 | 26 | if amountToWithdraw < 0: 27 | print('You cannot withdraw a negative amount') 28 | return None 29 | 30 | if amountToWithdraw > self.balance: 31 | print('You cannot withdraw more than you have in your account') 32 | return None 33 | 34 | self.balance = self.balance - amountToWithdraw 35 | return self.balance 36 | 37 | def getBalance(self, password): 38 | if password != self.password: 39 | print('Sorry, incorrect password') 40 | return None 41 | return self.balance 42 | 43 | # Added for debugging 44 | def show(self): 45 | print(' Name:', self.name) 46 | print(' Balance:', self.balance) 47 | print(' Password:', self.password) 48 | print() 49 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP5_SeparateBankClass/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | 3 | class Account(): 4 | def __init__(self, name, balance, password): 5 | self.name = name 6 | self.balance = int(balance) 7 | self.password = password 8 | 9 | def deposit(self, amountToDeposit, password): 10 | if password != self.password: 11 | print('Sorry, incorrect password') 12 | return None 13 | 14 | if amountToDeposit < 0: 15 | print('You cannot deposit a negative amount') 16 | return None 17 | 18 | self.balance = self.balance + amountToDeposit 19 | return self.balance 20 | 21 | def withdraw(self, amountToWithdraw, password): 22 | if password != self.password: 23 | print('Incorrect password for this account') 24 | return None 25 | 26 | if amountToWithdraw < 0: 27 | print('You cannot withdraw a negative amount') 28 | return None 29 | 30 | if amountToWithdraw > self.balance: 31 | print('You cannot withdraw more than you have in your account') 32 | return None 33 | 34 | self.balance = self.balance - amountToWithdraw 35 | return self.balance 36 | 37 | def getBalance(self, password): 38 | if password != self.password: 39 | print('Sorry, incorrect password') 40 | return None 41 | return self.balance 42 | 43 | # Added for debugging 44 | def show(self): 45 | print(' Name:', self.name) 46 | print(' Balance:', self.balance) 47 | print(' Password:', self.password) 48 | print() 49 | 50 | 51 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP5_SeparateBankClass/Main_Bank_Version5.py: -------------------------------------------------------------------------------- 1 | # Main program for controlling a Bank made up of Accounts 2 | 3 | # Bring in all the code of the Bank class 4 | from Bank import * 5 | 6 | # Create an instance of the Bank 7 | oBank = Bank() 8 | 9 | # Main code 10 | # Create two test accounts 11 | joesAccountNumber = oBank.createAccount('Joe', 100, 'JoesPassword') 12 | print("Joe's account number is:", joesAccountNumber) 13 | 14 | marysAccountNumber = oBank.createAccount('Mary', 12345, 'MarysPassword') 15 | print("Mary's account number is:", marysAccountNumber) 16 | 17 | while True: 18 | print() 19 | print('To get an account balance, press b') 20 | print('To close an account, press c') 21 | print('To make a deposit, press d') 22 | print('To get bank information, press i') 23 | print('To open a new account, press o') 24 | print('To quit, press q') 25 | print('To show all accounts, press s') 26 | print('To make a withdrawal, press w') 27 | print() 28 | 29 | action = input('What do you want to do? ') 30 | action = action.lower() 31 | action = action[0] # grab the first letter 32 | print() 33 | 34 | if action == 'b': 35 | oBank.balance() 36 | 37 | elif action == 'c': 38 | oBank.closeAccount() 39 | 40 | elif action == 'd': 41 | oBank.deposit() 42 | 43 | elif action == 'i': 44 | oBank.bankInfo() 45 | 46 | elif action == 'o': 47 | oBank.openAccount() 48 | 49 | elif action == 's': 50 | oBank.show() 51 | 52 | elif action == 'q': 53 | break 54 | 55 | elif action == 'w': 56 | oBank.withdraw() 57 | 58 | else: 59 | print('Sorry, that was not a valid action. Please try again.') 60 | 61 | print('Done') 62 | 63 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP6_UsingExceptions/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bank 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP6_UsingExceptions/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | /Bank/src 7 | 8 | python 2.7 9 | Default 10 | 11 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP6_UsingExceptions/Account.py: -------------------------------------------------------------------------------- 1 | # Account class 2 | # Errors indicated by "raise" statements 3 | 4 | # Define a custom exception 5 | class AbortTransaction(Exception): 6 | '''raise this exception to abort a bank transaction''' 7 | pass 8 | 9 | class Account(): 10 | def __init__(self, name, balance, password): 11 | self.name = name 12 | self.balance = self.validateAmount(balance) 13 | self.password = password 14 | 15 | def validateAmount(self, amount): 16 | try: 17 | amount = int(amount) 18 | except ValueError: 19 | raise AbortTransaction('Amount must be an integer') 20 | if amount <= 0: 21 | raise AbortTransaction('Amount must be positive') 22 | return amount 23 | 24 | def checkPasswordMatch(self, password): 25 | if password != self.password: 26 | raise AbortTransaction('Incorrect password for this account') 27 | 28 | def deposit(self, amountToDeposit): 29 | amountToDeposit = self.validateAmount(amountToDeposit) 30 | self.balance = self.balance + amountToDeposit 31 | return self.balance 32 | 33 | def getBalance(self): 34 | return self.balance 35 | 36 | def withdraw(self, amountToWithdraw): 37 | amountToWithdraw = self.validateAmount(amountToWithdraw) 38 | if amountToWithdraw > self.balance: 39 | raise AbortTransaction('You cannot withdraw more than you have in your account') 40 | 41 | self.balance = self.balance - amountToWithdraw 42 | return self.balance 43 | 44 | # Added for debugging 45 | def show(self): 46 | print(' Name:', self.name) 47 | print(' Balance:', self.balance) 48 | print(' Password:', self.password) 49 | -------------------------------------------------------------------------------- /Chapter_4/BankOOP6_UsingExceptions/Main_Bank_Version6.py: -------------------------------------------------------------------------------- 1 | # Main program for controlling a Bank made up of Accounts 2 | from Bank import * 3 | 4 | # Create an instance of the Bank 5 | oBank = Bank('9 to 5', '123 Main Street, Anytown, USA', '(650) 555-1212') 6 | 7 | #Main code 8 | while True: 9 | print() 10 | print('To get an account balance, press b') 11 | print('To close an account, press c') 12 | print('To make a deposit, press d') 13 | print('To get bank information, press i') 14 | print('To open a new account, press o') 15 | print('To quit, press q') 16 | print('To show all accounts, press s') 17 | print('To make a withdrawal, press w ') 18 | print() 19 | 20 | action = input('What do you want to do? ') 21 | action = action.lower() 22 | action = action[0] # grab the first letter 23 | print() 24 | 25 | try: 26 | if action == 'b': 27 | oBank.balance() 28 | elif action == 'c': 29 | oBank.closeAccount() 30 | elif action == 'd': 31 | oBank.deposit() 32 | elif action == 'i': 33 | oBank.getInfo() 34 | elif action == 'o': 35 | oBank.openAccount() 36 | elif action == 'q': 37 | break 38 | elif action == 's': 39 | oBank.show() 40 | elif action == 'w': 41 | oBank.withdraw() 42 | except AbortTransaction as error: 43 | # Print out the text of the error message 44 | print(error) 45 | 46 | print('Done') 47 | -------------------------------------------------------------------------------- /Chapter_4/MonsterExample.py: -------------------------------------------------------------------------------- 1 | # Main program for creating monsters 2 | 3 | import random 4 | 5 | class Monster(): 6 | def __init__(self, nRows, nCols, maxSpeed): 7 | self.nRows = nRows # save away 8 | self.nCols = nCols # save away 9 | self.myRow = random.randrange(self.nRows) #chooses a random row 10 | self.myCol = random.randrange(self.nCols) #chooses a random col 11 | self.mySpeedX = random.randrange(-maxSpeed, maxSpeed) + 1 # chooses an X speed 12 | self.mySpeedY = random.randrange(-maxSpeed, maxSpeed + 1) # chooses a Y speed 13 | # Set other instance variables like health, power, etc. 14 | 15 | def move(self): 16 | self.myRow = (self.myRow + self.mySpeedY) % self.nRows 17 | self.myCol = (self.myCol + self.mySpeedX) % self.nCols 18 | 19 | 20 | N_MONSTERS = 20 21 | N_ROWS = 100 # could be any size 22 | N_COLS = 200 # could be any size 23 | MAX_SPEED = 4 24 | 25 | 26 | monsterList = [] # start with an empty list 27 | for i in range(N_MONSTERS): 28 | oMonster = Monster(N_ROWS, N_COLS, MAX_SPEED) # create a Monster 29 | monsterList.append(oMonster) # add Monster to our list 30 | 31 | # Later, when playing the game ... 32 | 33 | for oMonster in monsterList: 34 | oMonster.move() 35 | -------------------------------------------------------------------------------- /Chapter_5/PygameDemo0_WindowOnly/PygameWindowOnly.py: -------------------------------------------------------------------------------- 1 | # pygame demo 0 - window only 2 | 3 | # 1 - Import packages 4 | import pygame 5 | from pygame.locals import * 6 | import sys 7 | 8 | # 2 - Define constants 9 | BLACK = (0, 0, 0) 10 | WINDOW_WIDTH = 640 11 | WINDOW_HEIGHT = 480 12 | FRAMES_PER_SECOND = 30 13 | 14 | # 3 - Initialize the world 15 | pygame.init() 16 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 17 | clock = pygame.time.Clock() 18 | 19 | # 4 - Load assets: image(s), sound(s), etc. 20 | 21 | # 5 - Initialize variables 22 | 23 | # 6 - Loop forever 24 | while True: 25 | 26 | # 7 - Check for and handle events 27 | for event in pygame.event.get(): 28 | # Clicked the close button? Quit pygame and end program 29 | if event.type == pygame.QUIT: 30 | pygame.quit() 31 | sys.exit() 32 | 33 | # 8 - Do any "per frame" actions 34 | 35 | # 9 - Clear the window 36 | window.fill(BLACK) 37 | 38 | # 10 - Draw all window elements 39 | 40 | # 11 - Update the window 41 | pygame.display.update() 42 | 43 | # 12 - Slow things down a bit 44 | clock.tick(FRAMES_PER_SECOND) # make pygame wait 45 | -------------------------------------------------------------------------------- /Chapter_5/PygameDemo1_OneImage/PygameOneImage.py: -------------------------------------------------------------------------------- 1 | # pygame demo 1 - draw one image 2 | 3 | # 1 - Import packages 4 | import pygame 5 | from pygame.locals import * 6 | import sys 7 | 8 | # 2 - Define constants 9 | BLACK = (0, 0, 0) 10 | WINDOW_WIDTH = 640 11 | WINDOW_HEIGHT = 480 12 | FRAMES_PER_SECOND = 30 13 | 14 | # 3 - Initialize the world 15 | pygame.init() 16 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 17 | clock = pygame.time.Clock() 18 | 19 | # 4 - Load assets: image(s), sound(s), etc. 20 | ballImage = pygame.image.load('images/ball.png') 21 | 22 | # 5 - Initialize variables 23 | 24 | # 6 - Loop forever 25 | while True: 26 | 27 | # 7 - Check for and handle events 28 | for event in pygame.event.get(): 29 | # Clicked the close button? Quit pygame and end the program 30 | if event.type == pygame.QUIT: 31 | pygame.quit() 32 | sys.exit() 33 | 34 | # 8 Do any "per frame" actions 35 | 36 | # 9 - Clear the window 37 | window.fill(BLACK) 38 | 39 | # 10 - Draw all window elements 40 | # draw ball at position 100 across (x) and 200 down (y) 41 | window.blit(ballImage, (100, 200)) 42 | 43 | # 11 - Update the window 44 | pygame.display.update() 45 | 46 | # 12 - Slow things down a bit 47 | clock.tick(FRAMES_PER_SECOND) # make pygame wait 48 | -------------------------------------------------------------------------------- /Chapter_5/PygameDemo1_OneImage/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/PygameDemo1_OneImage/images/ball.png -------------------------------------------------------------------------------- /Chapter_5/PygameDemo2_ImageClickAndMove/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/PygameDemo2_ImageClickAndMove/images/ball.png -------------------------------------------------------------------------------- /Chapter_5/PygameDemo3_MoveByKeyboard/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/PygameDemo3_MoveByKeyboard/images/ball.png -------------------------------------------------------------------------------- /Chapter_5/PygameDemo3_MoveByKeyboard/images/target.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/PygameDemo3_MoveByKeyboard/images/target.jpg -------------------------------------------------------------------------------- /Chapter_5/PygameDemo4_OneBallBounce/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/PygameDemo4_OneBallBounce/images/ball.png -------------------------------------------------------------------------------- /Chapter_5/PygameDemo4_OneBallBounce/sounds/background.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/PygameDemo4_OneBallBounce/sounds/background.mp3 -------------------------------------------------------------------------------- /Chapter_5/PygameDemo4_OneBallBounce/sounds/boing.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/PygameDemo4_OneBallBounce/sounds/boing.wav -------------------------------------------------------------------------------- /Chapter_5/TwoButtons/bark.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/TwoButtons/bark.wav -------------------------------------------------------------------------------- /Chapter_5/TwoButtons/meow.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_5/TwoButtons/meow.wav -------------------------------------------------------------------------------- /Chapter_6/PygameDemo6_BallBounceObjectOriented/Ball.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.locals import * 3 | import random 4 | 5 | # Ball class 6 | class Ball(): 7 | 8 | def __init__(self, window, windowWidth, windowHeight): 9 | self.window = window # remember the window, so we can draw later 10 | self.windowWidth = windowWidth 11 | self.windowHeight = windowHeight 12 | 13 | self.image = pygame.image.load('images/ball.png') 14 | # A rect is made up of [x, y, width, height] 15 | ballRect = self.image.get_rect() 16 | self.width = ballRect.width 17 | self.height = ballRect.height 18 | self.maxWidth = windowWidth - self.width 19 | self.maxHeight = windowHeight - self.height 20 | 21 | # Pick a random starting position 22 | self.x = random.randrange(0, self.maxWidth) 23 | self.y = random.randrange(0, self.maxHeight) 24 | 25 | # Choose a random speed between -4 and 4, but not zero 26 | # in both the x and y directions 27 | speedsList = [-4, -3, -2, -1, 1, 2, 3, 4] 28 | self.xSpeed = random.choice(speedsList) 29 | self.ySpeed = random.choice(speedsList) 30 | 31 | def update(self): 32 | # Check for hitting a wall. If so, change that direction. 33 | if (self.x < 0) or (self.x >= self.maxWidth): 34 | self.xSpeed = -self.xSpeed 35 | 36 | if (self.y < 0) or (self.y >= self.maxHeight): 37 | self.ySpeed = -self.ySpeed 38 | 39 | # Update the Ball's x and y, using the speed in two directions 40 | self.x = self.x + self.xSpeed 41 | self.y = self.y + self.ySpeed 42 | 43 | def draw(self): 44 | self.window.blit(self.image, (self.x, self.y)) 45 | -------------------------------------------------------------------------------- /Chapter_6/PygameDemo6_BallBounceObjectOriented/Main_BallBounce.py: -------------------------------------------------------------------------------- 1 | # pygame demo 6(a) - using the Ball class, bounce one ball 2 | 3 | # 1 - Import packages 4 | import pygame 5 | from pygame.locals import * 6 | import sys 7 | import random 8 | from Ball import * # bring in the Ball class code 9 | 10 | # 2 - Define constants 11 | BLACK = (0, 0, 0) 12 | WINDOW_WIDTH = 640 13 | WINDOW_HEIGHT = 480 14 | FRAMES_PER_SECOND = 30 15 | 16 | # 3 - Initialize the world 17 | pygame.init() 18 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 19 | clock = pygame.time.Clock() 20 | 21 | # 4 - Load assets: image(s), sounds, etc. 22 | 23 | # 5 - Initialize variables 24 | oBall = Ball(window, WINDOW_WIDTH, WINDOW_HEIGHT) 25 | 26 | # 6 - Loop forever 27 | while True: 28 | 29 | # 7 - Check for and handle events 30 | for event in pygame.event.get(): 31 | if event.type == pygame.QUIT: 32 | pygame.quit() 33 | sys.exit() 34 | 35 | # 8 - Do any "per frame" actions 36 | oBall.update() # tell the Ball to update itself 37 | 38 | # 9 - Clear the window before drawing it again 39 | window.fill(BLACK) 40 | 41 | # 10 - Draw the window elements 42 | oBall.draw() # tell the Ball to draw itself 43 | 44 | 45 | # 11 - Update the window 46 | pygame.display.update() 47 | 48 | # 12 - Slow things down a bit 49 | clock.tick(FRAMES_PER_SECOND) # make pygame wait 50 | 51 | 52 | -------------------------------------------------------------------------------- /Chapter_6/PygameDemo6_BallBounceObjectOriented/Main_BallBounceManyBalls.py: -------------------------------------------------------------------------------- 1 | # pygame demo 6(b) - using the Ball class, bounce many balls 2 | 3 | # 1 - Import packages 4 | import pygame 5 | from pygame.locals import * 6 | import sys 7 | import random 8 | from Ball import * # bring in the Ball class code 9 | 10 | # 2 - Define constants 11 | BLACK = (0, 0, 0) 12 | WINDOW_WIDTH = 640 13 | WINDOW_HEIGHT = 480 14 | FRAMES_PER_SECOND = 30 15 | N_BALLS = 3 16 | 17 | # 3 - Initialize the world 18 | pygame.init() 19 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 20 | clock = pygame.time.Clock() 21 | 22 | # 4 - Load assets: image(s), sounds, etc. 23 | 24 | # 5 - Initialize variables 25 | ballList = [] 26 | for oBall in range(0, N_BALLS): 27 | # Each time through the loop, create a Ball object 28 | oBall = Ball(window, WINDOW_WIDTH, WINDOW_HEIGHT) 29 | ballList.append(oBall) # append the new Ball to the list of Balls 30 | 31 | # 6 - Loop forever 32 | while True: 33 | 34 | # 7 - Check for and handle events 35 | for event in pygame.event.get(): 36 | if event.type == pygame.QUIT: 37 | pygame.quit() 38 | sys.exit() 39 | 40 | # 8 - Do any "per frame" actions 41 | for oBall in ballList: 42 | oBall.update() # tell each Ball to update itself 43 | 44 | # 9 - Clear the window before drawing it again 45 | window.fill(BLACK) 46 | 47 | # 10 - Draw the window elements 48 | for oBall in ballList: 49 | oBall.draw() # tell each Ball to draw itself 50 | 51 | # 11 - Update the window 52 | pygame.display.update() 53 | 54 | # 12 - Slow things down a bit 55 | clock.tick(FRAMES_PER_SECOND) # make pygame wait 56 | 57 | 58 | -------------------------------------------------------------------------------- /Chapter_6/PygameDemo6_BallBounceObjectOriented/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo6_BallBounceObjectOriented/images/ball.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/Main_SimpleButton.py: -------------------------------------------------------------------------------- 1 | # pygame demo 7 SimpleButton test 2 | 3 | # 1 - Import packages 4 | import pygame 5 | from pygame.locals import * 6 | from SimpleButton import * 7 | import sys 8 | 9 | # Define constants 10 | GRAY = (200, 200, 200) 11 | WINDOW_WIDTH = 400 12 | WINDOW_HEIGHT = 100 13 | FRAMES_PER_SECOND = 30 14 | 15 | # 2 - Initialize the world 16 | pygame.init() 17 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 18 | clock = pygame.time.Clock() 19 | 20 | # 4 - Load assets: image(s), sounds, etc. 21 | 22 | # 5 - Initialize variables 23 | # Create an instance of SimpleButton 24 | oButton = SimpleButton(window, (150, 30), 25 | 'images/buttonUp.png', 26 | 'images/buttonDown.png') 27 | 28 | # 6 - Loop forever 29 | while True: 30 | 31 | # 7 - Check for and handle events 32 | for event in pygame.event.get(): 33 | if event.type == pygame.QUIT: 34 | pygame.quit() 35 | sys.exit() 36 | 37 | # Pass the event to the button, see if it has been clicked on 38 | if oButton.handleEvent(event): 39 | print('User has clicked the button.') 40 | 41 | # 8 - Do any "per frame" actions 42 | 43 | # 9 - Clear the window 44 | window.fill(GRAY) 45 | 46 | # 10 - Draw all window elements 47 | oButton.draw() # draw the button 48 | 49 | # 11 - Update the window 50 | pygame.display.update() 51 | 52 | # 12 - Slow things down a bit 53 | clock.tick(FRAMES_PER_SECOND) # make pygame wait 54 | -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonADown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonADown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonAUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonAUp.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonBDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonBDown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonBUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonBUp.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonCDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonCDown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonCUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonCUp.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonDown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo7_SimpleButton/images/buttonUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo7_SimpleButton/images/buttonUp.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo8_SimpleTextDisplay/Ball.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | from pygame.locals import * 3 | import random 4 | 5 | # BALL CLASS 6 | class Ball(): 7 | 8 | def __init__(self, window, windowWidth, windowHeight): 9 | self.window = window # remember the window, so we can draw later 10 | self.windowWidth = windowWidth 11 | self.windowHeight = windowHeight 12 | 13 | self.image = pygame.image.load('images/ball.png') 14 | # A rect is made up of [x, y, width, height] 15 | ballRect = self.image.get_rect() 16 | self.width = ballRect.width 17 | self.height = ballRect.height 18 | self.maxWidth = windowWidth - self.width 19 | self.maxHeight = windowHeight - self.height 20 | 21 | # Pick a random starting position 22 | self.x = random.randrange(0, self.maxWidth) 23 | self.y = random.randrange(0, self.maxHeight) 24 | 25 | # Choose a random speed between -4 and 4, but not zero 26 | # in both the x and y directions 27 | speedsList = [-4, -3, -2, -1, 1, 2, 3, 4] 28 | self.xSpeed = random.choice(speedsList) 29 | self.ySpeed = random.choice(speedsList) 30 | 31 | def update(self): 32 | # check for hitting a wall. If so, change that direction 33 | if (self.x < 0) or (self.x >= self.maxWidth): 34 | self.xSpeed = -self.xSpeed 35 | 36 | if (self.y < 0) or (self.y >= self.maxHeight): 37 | self.ySpeed = -self.ySpeed 38 | 39 | # update the balls x and y, based on the speed in two directions 40 | self.x = self.x + self.xSpeed 41 | self.y = self.y + self.ySpeed 42 | 43 | def draw(self): 44 | self.window.blit(self.image, (self.x, self.y)) 45 | -------------------------------------------------------------------------------- /Chapter_6/PygameDemo8_SimpleTextDisplay/SimpleText.py: -------------------------------------------------------------------------------- 1 | # SimpleText class 2 | 3 | import pygame 4 | from pygame.locals import * 5 | 6 | class SimpleText(): 7 | 8 | def __init__(self, window, loc, value, textColor): 9 | pygame.font.init() 10 | self.window = window 11 | self.loc = loc 12 | self.font = pygame.font.SysFont(None, 30) 13 | self.textColor = textColor 14 | self.text = None # so that the call to setText below will force the creation of the text image 15 | self.setValue(value) # set the initial text for drawing 16 | 17 | def setValue(self, newText): 18 | if self.text == newText: 19 | return # nothing to change 20 | 21 | self.text = newText # save the new text 22 | self.textSurface = self.font.render(self.text, True, self.textColor) 23 | 24 | def draw(self): 25 | self.window.blit(self.textSurface, self.loc) 26 | -------------------------------------------------------------------------------- /Chapter_6/PygameDemo8_SimpleTextDisplay/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo8_SimpleTextDisplay/images/ball.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo8_SimpleTextDisplay/images/restartDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo8_SimpleTextDisplay/images/restartDown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo8_SimpleTextDisplay/images/restartUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo8_SimpleTextDisplay/images/restartUp.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonADown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonADown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonAUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonAUp.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonBDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonBDown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonBUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonBUp.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonCDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonCDown.png -------------------------------------------------------------------------------- /Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonCUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_6/PygameDemo9_SimpleButtonWithCallback/images/buttonCUp.png -------------------------------------------------------------------------------- /Chapter_7/OrderPizzaWithNone.py: -------------------------------------------------------------------------------- 1 | def orderPizza(size, style='regular', topping=None): 2 | # Do some calculations based on the size and style 3 | # Check if a topping was specified 4 | PRICE_OF_TOPPING = 1.50 # price for any topping 5 | 6 | if size == 'small': 7 | price = 10.00 8 | elif size == 'medium': 9 | price = 14.00 10 | else: # large 11 | price = 18.00 12 | 13 | if style == 'deepdish': 14 | price = price + 2.00 # charge extra for deepdish 15 | 16 | line = 'You have ordered a ' + size + ' ' + style + ' pizza with ' 17 | if topping is None: # check if no topping was passed in 18 | print(line +'no topping') 19 | else: 20 | print(line + topping) 21 | price = price + PRICE_OF_TOPPING 22 | 23 | print('The price is $', price) 24 | print() 25 | 26 | # You could order a pizza in the following ways: 27 | orderPizza('large') # large, defaults to regular, no topping 28 | 29 | orderPizza('large', style='regular') # same as above 30 | 31 | orderPizza('medium', style='deepdish', topping='mushrooms') 32 | 33 | orderPizza('small', topping='mushrooms') # style defaults to regular 34 | -------------------------------------------------------------------------------- /Chapter_7/PygameDemo10_ImageTextButtonBall/images/background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_7/PygameDemo10_ImageTextButtonBall/images/background.jpg -------------------------------------------------------------------------------- /Chapter_7/PygameDemo10_ImageTextButtonBall/images/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_7/PygameDemo10_ImageTextButtonBall/images/ball.png -------------------------------------------------------------------------------- /Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartDisabled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartDisabled.png -------------------------------------------------------------------------------- /Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartDown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartDown.png -------------------------------------------------------------------------------- /Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartOver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartOver.png -------------------------------------------------------------------------------- /Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartUp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_7/PygameDemo10_ImageTextButtonBall/images/restartUp.png -------------------------------------------------------------------------------- /Chapter_7/PygameDemo10_ImageTextButtonBall/sounds/blip.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IrvKalb/Object-Oriented-Python-Code/72ad867ba3f1a93941144a72b4e9184a3ffff9a1/Chapter_7/PygameDemo10_ImageTextButtonBall/sounds/blip.wav -------------------------------------------------------------------------------- /Chapter_8/PersonGettersSettersAndDirectAccess/Main_PersonDirectAccess.py: -------------------------------------------------------------------------------- 1 | # Person example main program using direct access 2 | 3 | from Person import * 4 | 5 | oPerson1 = Person('Joe Schmoe', 90000) 6 | oPerson2 = Person('Jane Smith', 99000) 7 | 8 | # Get the values of the salary variable directly 9 | print(oPerson1.salary) 10 | print(oPerson2.salary) 11 | 12 | # Change the salary variable directly 13 | oPerson1.salary = 100000 14 | oPerson2.salary = 111111 15 | 16 | # Get the updated salaries and print again 17 | print(oPerson1.salary) 18 | print(oPerson2.salary) 19 | 20 | -------------------------------------------------------------------------------- /Chapter_8/PersonGettersSettersAndDirectAccess/Main_PersonGetterSetter.py: -------------------------------------------------------------------------------- 1 | # Person example main program using getters and setters 2 | 3 | from Person import * 4 | 5 | oPerson1 = Person('Joe Schmoe', 90000) 6 | oPerson2 = Person('Jane Smith', 99000) 7 | 8 | # Get the salaries using getter and print 9 | print(oPerson1.getSalary()) 10 | print(oPerson2.getSalary()) 11 | 12 | # Change the salaries using setter 13 | oPerson1.setSalary(100000) 14 | oPerson2.setSalary(111111) 15 | 16 | # Get the salaries and print again 17 | print(oPerson1.getSalary()) 18 | print(oPerson2.getSalary()) 19 | -------------------------------------------------------------------------------- /Chapter_8/PersonGettersSettersAndDirectAccess/Person.py: -------------------------------------------------------------------------------- 1 | # Person class 2 | 3 | class Person(): 4 | 5 | def __init__(self, name, salary): 6 | self.name = name 7 | self.salary = salary 8 | 9 | # Allow the caller to retrieve the salary 10 | def getSalary(self): 11 | return self.salary 12 | 13 | # Allow the caller to set a new salary 14 | def setSalary(self, salary): 15 | self.salary = salary 16 | -------------------------------------------------------------------------------- /Chapter_8/PrivatePersonWIthNameMangling/Main_PrivatePerson.py: -------------------------------------------------------------------------------- 1 | # PrivatePerson Example main program 2 | 3 | from PrivatePerson import * 4 | 5 | oPrivatePerson1 = PrivatePerson('Joe Schmoe', 'Data for Joe Schmoe') 6 | oPrivatePerson2 = PrivatePerson('Jane Smith', 'Data for Jane Smith') 7 | 8 | # Using getter and setter - works fine 9 | print(oPrivatePerson1.getName()) 10 | 11 | oPrivatePerson1.setName('Joseph Schmoe') 12 | print(oPrivatePerson1.getName()) 13 | 14 | 15 | # Attempted use of direct access would fail 16 | #print(oPrivatePerson1.__privateData) 17 | 18 | 19 | # Using mangled name - works 20 | print(oPrivatePerson1._PrivatePerson__privateData) 21 | oPrivatePerson1._PrivatePerson__privateData = 'Modified data for Joeseph Schmoe' 22 | print(oPrivatePerson1._PrivatePerson__privateData) 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter_8/PrivatePersonWIthNameMangling/PrivatePerson.py: -------------------------------------------------------------------------------- 1 | # PrivatePerson class 2 | 3 | class PrivatePerson(): 4 | 5 | def __init__(self, name, privateData): 6 | self.name = name 7 | self.__privateData = privateData 8 | 9 | def getName(self): 10 | return self.name 11 | 12 | def setName(self, name): 13 | self.name = name 14 | 15 | -------------------------------------------------------------------------------- /Chapter_8/PropertyDecorator/Main_Property.py: -------------------------------------------------------------------------------- 1 | # Main Student property example 2 | 3 | from Student import * 4 | 5 | oStudent1 = Student('Joe Schmoe') 6 | oStudent2 = Student('Jane Smith') 7 | 8 | # Get the students' grades using the 'grade' property and print 9 | print(oStudent1.grade) 10 | print(oStudent2.grade) 11 | print() 12 | 13 | # Set new values using the 'grade' property 14 | oStudent1.grade = 85 15 | oStudent2.grade = 92 16 | 17 | oStudent1.grade = 'abc' 18 | 19 | 20 | print(oStudent1.grade) 21 | print(oStudent2.grade) 22 | -------------------------------------------------------------------------------- /Chapter_8/PropertyDecorator/Student.py: -------------------------------------------------------------------------------- 1 | # Using a property to (indirectly) access data in an object 2 | 3 | class Student(): 4 | 5 | def __init__(self, name, startingGrade=0): 6 | self.__name = name 7 | self.grade = startingGrade 8 | 9 | @property 10 | def grade(self): 11 | return self.__grade 12 | 13 | @grade.setter 14 | def grade(self, newGrade): 15 | try: 16 | newGrade = int(newGrade) 17 | except (TypeError, ValueError) as e: 18 | raise type(e)('New grade: ' + str(newGrade) + ', is an invalid type.') 19 | if (newGrade < 0) or (newGrade > 100): 20 | raise ValueError('New grade: ' + str(newGrade) + ', must be between 0 and 100.') 21 | self.__grade = newGrade 22 | -------------------------------------------------------------------------------- /Chapter_8/Stack/Main_StackTest.py: -------------------------------------------------------------------------------- 1 | # Test code for basic push and pop on a stack 2 | 3 | from Stack import * 4 | 5 | oStack = Stack() 6 | oStack.push(5) 7 | oStack.push(12) 8 | oStack.push('Some data') 9 | oStack.push('Some more data') 10 | oStack.push(True) 11 | oStack.show() 12 | 13 | while True: 14 | print() 15 | item = oStack.pop() 16 | print('Next value from the stack is:', item) 17 | if oStack.getSize() == 0: 18 | break 19 | oStack.show() 20 | 21 | print('Stack is empty') 22 | -------------------------------------------------------------------------------- /Chapter_8/Stack/Stack.py: -------------------------------------------------------------------------------- 1 | # Stack class 2 | 3 | class Stack(): 4 | ''' Stack class implements a last in first out LIFO algorithm''' 5 | def __init__(self, startingStackAsList=None): 6 | if startingStackAsList is None: 7 | self.dataList = [ ] 8 | else: 9 | self.dataList = startingStackAsList[:] # make a copy 10 | 11 | def push(self, item): 12 | self.dataList.append(item) 13 | 14 | def pop(self): 15 | if len(self.dataList) == 0: 16 | raise IndexError 17 | element = self.dataList.pop() 18 | return element 19 | 20 | def peek(self): 21 | # Retrieve the top item, without removing it 22 | item = self.dataList[-1] 23 | return item 24 | 25 | def getSize(self): 26 | nElements = len(self.dataList) 27 | return nElements 28 | 29 | def show(self): 30 | # Show the stack in a vertical orientation 31 | print('Stack is:') 32 | for value in reversed(self.dataList): 33 | print(' ', value) 34 | -------------------------------------------------------------------------------- /Chapter_8/ValidatingData_ClubExample/Club.py: -------------------------------------------------------------------------------- 1 | # Club class 2 | 3 | class Club(): 4 | 5 | def __init__(self, clubName, maxMembers): 6 | self.clubName = clubName 7 | self.maxMembers = maxMembers 8 | self.membersList = [] 9 | 10 | def addMember(self, name): 11 | # Make sure that there is enough room left 12 | if len(self.membersList) < self.maxMembers: 13 | self.membersList.append(name) 14 | print('OK.', name, 'has been added to the', self.clubName, 'club') 15 | else: 16 | print('Sorry, but we cannot add', name, 'to the', self.clubName, 'club.') 17 | print('This club already has the maximum of', self.maxMembers, 'members.') 18 | 19 | def report(self): 20 | print() 21 | print('Here are the', len(self.membersList), 'members of the', self.clubName, 'club:') 22 | for name in self.membersList: 23 | print(' ' + name) 24 | print() 25 | 26 | -------------------------------------------------------------------------------- /Chapter_8/ValidatingData_ClubExample/Main_Club.py: -------------------------------------------------------------------------------- 1 | # Club example main program 2 | 3 | from Club import * 4 | 5 | # Create a club with at most 5 members 6 | oProgrammingClub = Club('Programming', 5) 7 | 8 | oProgrammingClub.addMember('Joe Schmoe') 9 | oProgrammingClub.addMember('Cindy Lou Hoo') 10 | oProgrammingClub.addMember('Dino Richmond') 11 | oProgrammingClub.addMember('Susie Sweetness') 12 | oProgrammingClub.addMember('Fred Farkle') 13 | 14 | oProgrammingClub.report() 15 | 16 | # Attempt to add additional member 17 | 18 | oProgrammingClub.addMember('Iwanna Join') 19 | -------------------------------------------------------------------------------- /Chapter_9/MagicMethods/Vectors/Main_Vectors.py: -------------------------------------------------------------------------------- 1 | # Vector test code 2 | 3 | from Vector import * 4 | 5 | v1 = Vector(3, 4) 6 | v2 = Vector(2, 2) 7 | v3 = Vector(3, 4) 8 | 9 | # These lines print Boolean or numeric values 10 | print(v1 == v2) # False 11 | print(v1 == v3) # True 12 | print(abs(v1)) # 5 13 | print(abs(v2)) # 2.8284... 14 | print(v1 < v2) # False 15 | print(v1 > v2) # True 16 | print() # Spacing 17 | 18 | # These lines print the vectors (calls the __str__() method) 19 | print('Vector 1:', v1) # 3, 4 20 | print('Vector 2:', v2) # 2, 2 21 | print('Vector 1 + Vector 2:', v1 + v2) # 5, 6 22 | print('Vector 1 - Vector 2:', v1 - v2) # 1, 2 23 | print('Vector 1 times Vector 2:', v1 * v2) # 6, 8 24 | print('Vector 1 times 5:', v1 * 5) # 15, 20 25 | -------------------------------------------------------------------------------- /Chapter_9/PetsPolymorphism.py: -------------------------------------------------------------------------------- 1 | # Pets polymorphism 2 | # Three classes, all with a different "speak" method 3 | 4 | class Dog(): 5 | def __init__(self, name): 6 | self.name = name 7 | 8 | def speak(self): 9 | print(self.name, 'says bark, bark, bark!') 10 | 11 | class Cat(): 12 | def __init__(self, name): 13 | self.name = name 14 | 15 | def speak(self): 16 | print(self.name, 'says meeeoooow') 17 | 18 | class Bird(): 19 | def __init__(self, name): 20 | self.name = name 21 | 22 | def speak(self): 23 | print(self.name, 'says tweet') 24 | 25 | oDog1 = Dog('Rover') 26 | oDog2 = Dog('Fido') 27 | oCat1 = Cat('Fluffy') 28 | oCat2 = Cat('Spike') 29 | oBird = Bird('Big Bird') 30 | 31 | petsList = [oDog1, oDog2, oCat1, oCat2, oBird] 32 | 33 | # Send the same message (call the same method) of all pets 34 | for oPet in petsList: 35 | oPet.speak() 36 | -------------------------------------------------------------------------------- /Chapter_9/Shapes/Circle.py: -------------------------------------------------------------------------------- 1 | # Circle class 2 | 3 | import pygame 4 | import random 5 | import math 6 | 7 | # Set up the colors 8 | RED = (255, 0, 0) 9 | GREEN = (0, 255, 0) 10 | BLUE = (0, 0, 255) 11 | 12 | class Circle(): 13 | 14 | def __init__(self, window, maxWidth, maxHeight): 15 | self.window = window 16 | 17 | self.color = random.choice((RED, GREEN, BLUE)) 18 | self.x = random.randrange(1, maxWidth - 100) 19 | self.y = random.randrange(25, maxHeight - 100) 20 | self.radius = random.randrange(10, 50) 21 | self.centerX = self.x + self.radius 22 | self.centerY = self.y + self.radius 23 | self.rect = pygame.Rect(self.x, self.y, 24 | self.radius * 2, self.radius * 2) 25 | self.shapeType = 'Circle' 26 | 27 | def clickedInside(self, mousePoint): 28 | distance = math.sqrt(((mousePoint[0] - self.centerX) ** 2) + 29 | ((mousePoint[1] - self.centerY) ** 2)) 30 | if distance <= self.radius: 31 | return True 32 | else: 33 | return False 34 | 35 | def getArea(self): 36 | theArea = math.pi * (self.radius ** 2) 37 | return theArea 38 | 39 | def getType(self): 40 | return self.shapeType 41 | 42 | def draw(self): 43 | pygame.draw.circle(self.window, self.color, 44 | (self.centerX, self.centerY), 45 | self.radius, 0) 46 | -------------------------------------------------------------------------------- /Chapter_9/Shapes/Main_ShapesExample.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import sys 3 | from pygame.locals import * 4 | from Square import * 5 | from Circle import * 6 | from Triangle import * 7 | import pygwidgets 8 | 9 | # Set up the constants 10 | WHITE = (255, 255, 255) 11 | WINDOW_WIDTH = 640 12 | WINDOW_HEIGHT = 480 13 | FRAMES_PER_SECOND = 30 14 | N_SHAPES = 10 15 | 16 | # Set up the window 17 | pygame.init() 18 | window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) 19 | clock = pygame.time.Clock() 20 | 21 | shapesList = [] 22 | shapeClassesTuple = (Square, Circle, Triangle) 23 | for i in range(0, N_SHAPES): 24 | randomlyChosenClass = random.choice(shapeClassesTuple) 25 | oShape = randomlyChosenClass(window, WINDOW_WIDTH, WINDOW_HEIGHT) 26 | shapesList.append(oShape) 27 | 28 | oStatusLine = pygwidgets.DisplayText(window, (4,4), 29 | 'Click on shapes', fontSize=28) 30 | 31 | # Main loop 32 | while True: 33 | for event in pygame.event.get(): 34 | if event.type == QUIT: 35 | pygame.quit() 36 | sys.exit() 37 | 38 | if event.type == MOUSEBUTTONDOWN: 39 | # Reverse order to check last drawn shape first 40 | for oShape in reversed(shapesList): 41 | if oShape.clickedInside(event.pos): 42 | area = oShape.getArea() 43 | area = str(area) 44 | theType = oShape.getType() 45 | newText = 'Clicked on a ' + theType + ' whose area is ' + area 46 | oStatusLine.setValue(newText) 47 | break # only deal with topmost shape 48 | 49 | # Tell each shape to draw itself 50 | window.fill(WHITE) 51 | for oShape in shapesList: 52 | oShape.draw() 53 | oStatusLine.draw() 54 | 55 | pygame.display.update() 56 | clock.tick(FRAMES_PER_SECOND) 57 | -------------------------------------------------------------------------------- /Chapter_9/Shapes/Square.py: -------------------------------------------------------------------------------- 1 | # Square class 2 | 3 | import pygame 4 | import random 5 | 6 | # Set up the colors 7 | RED = (255, 0, 0) 8 | GREEN = (0, 255, 0) 9 | BLUE = (0, 0, 255) 10 | 11 | class Square(): 12 | 13 | def __init__(self, window, maxWidth, maxHeight): 14 | self.window = window 15 | self.widthAndHeight = random.randrange(10, 100) 16 | self.color = random.choice((RED, GREEN, BLUE)) 17 | self.x = random.randrange(1, maxWidth - 100) 18 | self.y = random.randrange(25, maxHeight - 100) 19 | self.rect = pygame.Rect(self.x, self.y, self.widthAndHeight, 20 | self.widthAndHeight) 21 | self.shapeType = 'Square' 22 | 23 | def clickedInside(self, mousePoint): 24 | clicked = self.rect.collidepoint(mousePoint) 25 | return clicked 26 | 27 | def getType(self): 28 | return self.shapeType 29 | 30 | def getArea(self): 31 | theArea = self.widthAndHeight * self.widthAndHeight 32 | return theArea 33 | 34 | def draw(self): 35 | pygame.draw.rect(self.window, self.color, (self.x, self.y, self.widthAndHeight, self.widthAndHeight)) 36 | -------------------------------------------------------------------------------- /Chapter_9/Shapes/Triangle.py: -------------------------------------------------------------------------------- 1 | # Triangle class 2 | 3 | import pygame 4 | import random 5 | 6 | # Set up the colors 7 | RED = (255, 0, 0) 8 | GREEN = (0, 255, 0) 9 | BLUE = (0, 0, 255) 10 | 11 | class Triangle(): 12 | 13 | def __init__(self, window, maxWidth, maxHeight): 14 | self.window = window 15 | self.width = random.randrange(10, 100) 16 | self.height = random.randrange(10, 100) 17 | self.triangleSlope = -1 * (self.height / self.width) 18 | self.color = random.choice((RED, GREEN, BLUE)) 19 | self.x = random.randrange(1, maxWidth - 100) 20 | self.y = random.randrange(25, maxHeight - 100) 21 | self.rect = pygame.Rect(self.x, self.y, 22 | self.width, self.height) 23 | self.shapeType = 'Triangle' 24 | 25 | def clickedInside(self, mousePoint): 26 | inRect = self.rect.collidepoint(mousePoint) 27 | if not inRect: 28 | return False 29 | 30 | # Do some math to see if the point is inside the triangle 31 | xOffset = mousePoint[0] - self.x 32 | yOffset = mousePoint[1] - self.y 33 | if xOffset == 0: 34 | return True 35 | 36 | # Calculate the slope (rise over run) 37 | pointSlopeFromYIntercept = (yOffset - self.height) / xOffset 38 | if pointSlopeFromYIntercept < self.triangleSlope: 39 | return True 40 | else: 41 | return False 42 | 43 | def getType(self): 44 | return self.shapeType 45 | 46 | def getArea(self): 47 | theArea = .5 * self.width * self.height 48 | return theArea 49 | 50 | def draw(self): 51 | pygame.draw.polygon(self.window, self.color, 52 | ((self.x, self.y + self.height), 53 | (self.x, self.y), 54 | (self.x + self.width, self.y))) 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2020, Irv Kalb 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | --------------------------------------------------------------------------------