100 Days of Python: Days 21-25

Day 21

  • Learned about Class Inheritance.
  • Classes can inherit appearance and behavior from parent classes.  Simply include it in the parentheses of the class call (e.g. class Fish(Animal)) and add super().__init__() to the definition method.
  • If methods of the subclass add to the methods of the superclass, the super().method() must be called first.
  • Continued snake game
    • Added food-collision logic
    • Added scoreboard that updates with food collisions
    • Added logic for wall collisions to end the game
    • Added snake-growing logic and tail-collision handling.
    • Simplified code by using list slicing.
Optimizing code on the backend of Snake will yield no appreciation from end users.

Day 22

  • Reviewed project of the day – PONG.
  • Did pre-code evaluation of what objects exist in the game and may need their own class and how the code may be structured
    • Main.py
      • Initiate game objects
      • Set screen size and trace mode.
      • Create while loop to run the game.
    • Class – Screen
    • Class – Turtle
      • Superclass to Paddles, Scoreboard and Ball
    • Class – Paddles
      • Event listener should move up / down with user input.
    • Class – Scoreboard
      • Tracks score
      • Draws dividing line.
      • Refreshes during score changes.
    • Ball
      • Initial “serve”
      • Logic for paddle collisions (bounce, L/R wall collisions (score change) and T/B wall collisions (bounce)
  • Centerline ended up not being included in the project
There are so many scenarios to account for in game environments. Making PONG a bug-free could take hours.

Day 23

  • Turtle Crossing (a la Frogger / Crossy Road)
  • Created entire project from given requirements, no “follow along” guide.
  • Confirmed results and compared against Angela’s code.
Accidents caused by turtle sprites are not covered by your rectangle’s insurance policy.

Day 24

  • Modified snake game to include high scores.  Discussed how the script-based high score variable relies on the Python session and it is wiped the next time the script runs.
  • Reviewed opening and reading from files.
  • With open()  as varname is preferable way of working with files to reduce system resources impact.
  • Open modes (a=append, w=write, r=read)
  • Writing to file that doesn’t exist creates new file.
  • Used these methods to use local storage to track snake high scores
  • Reviewed absolute (relative to root) vs relative (relative to working directory) file paths
  • Apps Created
    • Created mail merge app that takes a template letter file and a contact list and generates personalized copies of the letter.  The personalized letters are automatically saved in a specified location in the file system.
Nothing more personal than a mail merge.

Day 25

  • Reviewed working with CSV
  • Manipulated CSV data manually with csv library import and for loops.
  • As a much better alternative, reviewed PANDAS package.
  • Two data structures with PANDAS.  Dataframes (tables) and series (column / list)
  • Reviewed how to retrieve / manipulate datasets.
  • Panda data structures act as object.  Dataframes carry their series as attributes.
  • Created Dataframes with a dictionary object and exported to CSV
  • Used Pandas to read from 2018 Central Park study on squirrels.
  • Built a table that counted the number of each color squirrel observed in the study.
  • Apps Created:
    • State Game – App shows user a map of the United States and prompts them to begin naming states.  State names begin appearing on the map as they are answered.  At the end of the game, any states that were not named by the user are transferred into a CSV file as a study worksheet.
Personally, I prefer the Animaniacs method of learning the U.S States…
Tagged in:,

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *