Coding Club

Contents:

  • The Big Book of Small Python Projects
    • Bagels
    • Birthday Paradox
    • Bitmap Message
    • Blackjack
    • Bouncing DVD Logo
    • Caesar Cipher
    • Caesar Hacker
    • Calendar Maker
    • Carrot in a Box
    • CHO HAN
    • Clickbait Headline Generator
    • Collatz Sequence
    • Conways Game of Life
    • Countdown
    • Deep Cave
    • Diamonds
    • Dice Math
    • Dice Roller
    • Digital Clock
    • Digital Stream
    • DNA Visualization
    • Ducklings
    • Etching Drawer
    • Factor Finder
    • Fast Draw
    • Fibonacci
    • Fish Tank
    • Flooder
      • How It Works
    • Forest Fire Sim
    • Four in a Row
    • Guess the Number
    • Gullible
    • Hacking Minigame
    • Hangman and Guillotine
    • HEX Grid
    • Hourglass
    • Hungry Robots
    • J’accuse
    • Langtons Ant
    • L3375P34]<
    • Lucky Stars
    • Magic Fortune Ball
    • Mancala
    • Maze Runner 2D
    • Maze Runner 3D
    • Million Dice Statistics
    • Mondrian Art Generator
    • Monty Hall
    • Multiplication Table
    • Ninety Nine Bottles
    • Ninety Nniine Boottels
    • Numeral Systems
    • Periodic Table of the Elements
    • Pig Latin
    • Powerball Lottery
    • Prime Numbers
    • Progress Bar
    • Rainbow
    • Rock Paper Scissors
    • Rock Paper Scissors (Always Win Version)
    • ROT13 Cipher
    • Rotating Cube
    • Royal Game of Ur
    • Seven Segment Display Module
    • Shining Carpet
    • Simple Substitution Cipher
    • Sine Message
    • Sliding Tile Puzzle
    • Snail Race
    • Soroban Japanese Abacus
    • Sound Mimic
    • Spongecase
    • Sudoku Puzzle
    • Text to Speech Talker
    • Three Card Monte
    • Tic-Tac-Toe
    • Tower of Hanoi
    • Trick Questions
    • Twenty Forty Eight
    • Vigenere Cipher
    • Water Bucket Puzzle
  • Cracking Codes with Python
    • Reverse Cipher
    • Caesar Cipher
    • Transposition Cipher
    • Affine Cipher
    • Simple Substitution Cipher
    • Vigenere Cipher
  • Coding Math
    • 1 - Introduction
    • 2 - Intro to Trigonometry
    • 3 - More Trigonometry
    • 4 - Circles, Ellipses and Lissajous Curves
    • 5 - Arctangent
    • 6 - Vectors, Part I
    • 7 - Vectors Part 2
    • 8 - Velocity
    • 9 - Acceleration
    • 10 - Advanced Acceleration
    • 11 - Gravity
    • 12 - Edge Handling
    • 13 - Friction
    • 14 - Collision Detection
    • 15 - Springs Part 1
    • 16 - Springs Part 2
    • 17 - Particles - Optimization
    • 18 - Particles - Enhancements
    • 19 - Bezier Curves
    • 20 - More on Bezier Curves
    • 21 - Bitmap Collision Detection
    • 22 - 3D - Postcards in Space
    • 23 - 3D Carousel
    • 24: 3D Points and Lines
    • 25 - 3D Modeling, Points and Lines Part 2
    • 26 - 2D and 3D Coordinate Rotation
    • 27 - Easing and Tweening
    • 28 - More on Easing
    • 29 - Tweening Part 1
    • 30 - Tweening Part II
    • 31 - Tweening Part III
    • 32 - Line Intersections Part I
    • 33 - Line Intersections Part II
    • 34 - Line Intersections Part III
    • 35 - Intro to Fractals
    • 36 - Verlet Integration Part I
    • 37 - Verlet Integration Part II
    • 38 - Verlet Integration Part III
    • 39 - Verlet Integration Part IV
    • 40 - Fractal Trees
    • 41 - Isometric 3D Part I
    • 42 - Isometric 3D Part II
    • 42 - Isometric 3D Part II
    • 43 - Kinematics Part I
    • 44 - Kinematics Part II
    • 45 - Kinematics Part III
    • 46 - Kinematics Part IV
    • 47 - Weighted Random
    • 48 - Matrix Math Part I
    • 49 - Matrix Math Part II
    • 50 - IFS Fractals
    • 51 - Pseudo Random Number Generators Part I
    • 52 - Pseudo Random Number Generators, Part II
    • 53 - Random Circle Packing
    • 54 - Dot Product
    • 55 - Aspect Ratio
    • 56 - Box Layout
    • 57 - Grid Layout
    • 58 - Array Math
  • Shaders
  • Coding Train Challenge
  • The Nature of Code
  • Recursive Book of Recursion
    • What Is Recursion?
    • Recursion vs. Iteration
    • Classic Recursion Algorithms
    • Backtracking and Tree Traversal Algorithms
    • Divide-and-Conquer Algorithms
    • Permutations and Combinations
    • Memoization and Dynamic Programming
    • Tail Call Optimization
    • Drawing Fractals
    • File Finder
    • Maze Generator
    • Sliding-Tile Solver
    • Fractal Art Maker
    • Droste Maker
  • Games
    • Asteroids
    • Bird
    • Blackjack
    • Blocks
    • Eyes
    • Fifteen
    • Flowers
    • Ghoulash
    • Life
    • Snake
    • Sokoban
    • Rock Paper Scissors
    • Hangman
    • Poker Dice
  • Music
    • Supercollider
      • Intro
        • Lead Sheets
        • Patch Sheets
  • Math Adventures with Python
    • Guessing and Checking with Conditionals
    • Transforming and Storing Numbers with Algebra
    • Transforming Shapes with Geometry
    • Creating Oscillations with Trigonometry
    • Complex Numbers
    • Using Matrices for Computer Graphics and Systems of Equations
    • Building Objects with Classes
    • Creating Fractals Using Recursion
    • Cellular Automata
      • CA
      • Game of Life
      • Rule 30
    • Solving Problems Using Genetic Algorithms
  • Doing Maths with Python
    • Working with Numbers
    • Visualizing Data with Graphs
    • Describing Data with Statistics
    • Algebra and Symbolic Math with SymPy
    • Playing with Sets and Probability
    • Drawing Geometric Shapes and Fractals
    • Solving Calculus Problems
  • Python Playground
    • The Koch Snowflake
    • Spirographs
    • Conway’s Game of Life
    • Musical Overtones with the Karplus-Strong Algorithm
    • Flocking Boids
    • ASCII Art
    • Photomosaics
    • Autostereograms
    • Understanding OpenGL
    • Conway’s Game of Life on a Torus
    • Volume Rendering
    • The Karplus-Strong Algorithm on a Raspberry Pi Pico
    • Laser Audio Display with a Raspberry Pi
    • IoT Garden
    • Audio ML on Pi
Coding Club
  • The Big Book of Small Python Projects
  • Bouncing DVD Logo
  • View page source

Bouncing DVD Logo

Tags: short, artistic, bext

If you are of a certain age, you’ll remember those ancient technological devices called DVD players. When not playing DVDs, they would display a diagonally traveling DVD logo that bounced off the edges of the screen. This program simulates this colorful DVD logo by making it change direction each time it hits an edge. We’ll also keep track of how many times a logo hits a corner of the screen. This creates an interesting visual animation to look at, especially for the magical moment when a logo lines up perfectly with a corner.

You can’t run this program from your integrated development environment (IDE) or editor because it uses the bext module. Therefore, it must be run from the Command Prompt or Terminal in order to display correctly. You can find more information about the bext module at https://pypi.org/project/bext/.

bouncing_dvd_logo.py
  1
  2"""Bouncing DVD Logo, by Al Sweigart al@inventwithpython.com
  3A bouncing DVD logo animation. You have to be "of a certain age" to
  4appreciate this. Press Ctrl-C to stop.
  5
  6NOTE: Do not resize the terminal window while this program is running.
  7This code is available at https://nostarch.com/big-book-small-python-programming
  8Tags: short, artistic, bext"""
  9
 10import sys, random, time
 11
 12try:
 13    import bext
 14except ImportError:
 15    print('This program requires the bext module, which you')
 16    print('can install by following the instructions at')
 17    print('https://pypi.org/project/Bext/')
 18    sys.exit()
 19
 20# Set up the constants:
 21WIDTH, HEIGHT = bext.size()
 22# We can't print to the last column on Windows without it adding a
 23# newline automatically, so reduce the width by one:
 24WIDTH -= 1
 25
 26NUMBER_OF_LOGOS = 5  # (!) Try changing this to 1 or 100.
 27PAUSE_AMOUNT = 0.2  # (!) Try changing this to 1.0 or 0.0.
 28# (!) Try changing this list to fewer colors:
 29COLORS = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
 30
 31UP_RIGHT   = 'ur'
 32UP_LEFT    = 'ul'
 33DOWN_RIGHT = 'dr'
 34DOWN_LEFT  = 'dl'
 35DIRECTIONS = (UP_RIGHT, UP_LEFT, DOWN_RIGHT, DOWN_LEFT)
 36
 37# Key names for logo dictionaries:
 38COLOR = 'color'
 39X = 'x'
 40Y = 'y'
 41DIR = 'direction'
 42
 43
 44def main():
 45    bext.clear()
 46
 47    # Generate some logos.
 48    logos = []
 49    for i in range(NUMBER_OF_LOGOS):
 50        logos.append({COLOR: random.choice(COLORS),
 51                      X: random.randint(1, WIDTH - 4),
 52                      Y: random.randint(1, HEIGHT - 4),
 53                      DIR: random.choice(DIRECTIONS)})
 54        if logos[-1][X] % 2 == 1:
 55            # Make sure X is even so it can hit the corner.
 56            logos[-1][X] -= 1
 57
 58    cornerBounces = 0  # Count how many times a logo hits a corner.
 59    while True:  # Main program loop.
 60        for logo in logos:  # Handle each logo in the logos list.
 61            # Erase the logo's current location:
 62            bext.goto(logo[X], logo[Y])
 63            print('   ', end='')  # (!) Try commenting this line out.
 64
 65            originalDirection = logo[DIR]
 66
 67            # See if the logo bounces off the corners:
 68            if logo[X] == 0 and logo[Y] == 0:
 69                logo[DIR] = DOWN_RIGHT
 70                cornerBounces += 1
 71            elif logo[X] == 0 and logo[Y] == HEIGHT - 1:
 72                logo[DIR] = UP_RIGHT
 73                cornerBounces += 1
 74            elif logo[X] == WIDTH - 3 and logo[Y] == 0:
 75                logo[DIR] = DOWN_LEFT
 76                cornerBounces += 1
 77            elif logo[X] == WIDTH - 3 and logo[Y] == HEIGHT - 1:
 78                logo[DIR] = UP_LEFT
 79                cornerBounces += 1
 80
 81            # See if the logo bounces off the left edge:
 82            elif logo[X] == 0 and logo[DIR] == UP_LEFT:
 83                logo[DIR] = UP_RIGHT
 84            elif logo[X] == 0 and logo[DIR] == DOWN_LEFT:
 85                logo[DIR] = DOWN_RIGHT
 86
 87            # See if the logo bounces off the right edge:
 88            # (WIDTH - 3 because 'DVD' has 3 letters.)
 89            elif logo[X] == WIDTH - 3 and logo[DIR] == UP_RIGHT:
 90                logo[DIR] = UP_LEFT
 91            elif logo[X] == WIDTH - 3 and logo[DIR] == DOWN_RIGHT:
 92                logo[DIR] = DOWN_LEFT
 93
 94            # See if the logo bounces off the top edge:
 95            elif logo[Y] == 0 and logo[DIR] == UP_LEFT:
 96                logo[DIR] = DOWN_LEFT
 97            elif logo[Y] == 0 and logo[DIR] == UP_RIGHT:
 98                logo[DIR] = DOWN_RIGHT
 99
100            # See if the logo bounces off the bottom edge:
101            elif logo[Y] == HEIGHT - 1 and logo[DIR] == DOWN_LEFT:
102                logo[DIR] = UP_LEFT
103            elif logo[Y] == HEIGHT - 1 and logo[DIR] == DOWN_RIGHT:
104                logo[DIR] = UP_RIGHT
105
106            if logo[DIR] != originalDirection:
107                # Change color when the logo bounces:
108                logo[COLOR] = random.choice(COLORS)
109
110            # Move the logo. (X moves by 2 because the terminal
111            # characters are twice as tall as they are wide.)
112            if logo[DIR] == UP_RIGHT:
113                logo[X] += 2
114                logo[Y] -= 1
115            elif logo[DIR] == UP_LEFT:
116                logo[X] -= 2
117                logo[Y] -= 1
118            elif logo[DIR] == DOWN_RIGHT:
119                logo[X] += 2
120                logo[Y] += 1
121            elif logo[DIR] == DOWN_LEFT:
122                logo[X] -= 2
123                logo[Y] += 1
124
125        # Display number of corner bounces:
126        bext.goto(5, 0)
127        bext.fg('white')
128        print('Corner bounces:', cornerBounces, end='')
129
130        for logo in logos:
131            # Draw the logos at their new location:
132            bext.goto(logo[X], logo[Y])
133            bext.fg(logo[COLOR])
134            print('DVD', end='')
135
136        bext.goto(0, 0)
137
138        sys.stdout.flush()  # (Required for bext-using programs.)
139        time.sleep(PAUSE_AMOUNT)
140
141
142# If this program was run (instead of imported), run the game:
143if __name__ == '__main__':
144    try:
145        main()
146    except KeyboardInterrupt:
147        print()
148        print('Bouncing DVD Logo, by Al Sweigart')
149        sys.exit()  # When Ctrl-C is pressed, end the program.

https://inventwithpython.com/bigbookpython/project5.html

Previous Next

© Copyright 2023, Marcel Hrdina.

Built with Sphinx using a theme provided by Read the Docs.