Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed Chapter 1.zip
Binary file not shown.
Binary file removed Chapter 10.zip
Binary file not shown.
Binary file removed Chapter 11.zip
Binary file not shown.
Binary file removed Chapter 2.zip
Binary file not shown.
Binary file removed Chapter 3.zip
Binary file not shown.
Binary file removed Chapter 4.zip
Binary file not shown.
Binary file removed Chapter 5.zip
Binary file not shown.
Binary file removed Chapter 6.zip
Binary file not shown.
Binary file removed Chapter 7.zip
Binary file not shown.
Binary file removed Chapter 8.zip
Binary file not shown.
Binary file removed Chapter 9.zip
Binary file not shown.
19 changes: 19 additions & 0 deletions Chapter_01/Listing 1_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Hello world! in Python and pygame."""

# Import and initialize pygame.
import pygame
pygame.init()

# Annotate variable
screen: pygame.Surface

# Display "Hello world!" as the window title.
screen = pygame.display.set_mode((400, 200))
pygame.display.set_caption("Hello world!")

# Display "Hello world!" in the console.
print("Hello world!")

# Quit when the user presses enter.
input("Press enter to quit...")
pygame.quit()
18 changes: 18 additions & 0 deletions Chapter_01/Listing 1_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Calculate the angle between stars in an aerial firework."""

import random

# Annotate variables
DEGREES_CIRCLE: int = 360
num_stars: int
angle: float

# Randomly generate a number of stars.
num_stars = random.randint(2, 360)

# Calculate the angle between the stars.
angle = DEGREES_CIRCLE / num_stars

# Tell the user the angle between stars.
print("You will need an angle of " + str(angle)
+ "° between " + str(num_stars) + " stars.")
24 changes: 24 additions & 0 deletions Chapter_01/Listing 1_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Play a number guessing game."""

import random

# Annotate variables
number: int
guess: int

# Randomly generate a number between 1 and 50.
number = random.randint(1, 50)

# Set up the game.
print("I'm thinking of a number between 1 and 50.")

# Obtain the user's guess until they guess correctly.
guess = 0
while guess != number:
guess = int(input("What is your guess? "))
if guess < number:
print("Higher...")
elif guess > number:
print("Lower...")
else:
print("You got it!")
45 changes: 45 additions & 0 deletions Chapter_01/Listing 1_12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Draw firework stars with pygame."""

import math

# Import and initialize pygame.
import pygame
pygame.init()

# Define some size constants to make drawing easier.
PI_OVER_4: float = math.radians(45)
RADIUS: int = 120
SIZE: int = 480
CENTER: float = (SIZE - 40) / 2

# Annotate variables
screen: pygame.Surface
star: pygame.Surface
star_num: int
x: float
y: float

# Create a pygame window.
screen = pygame.display.set_mode((SIZE, SIZE))
pygame.display.set_caption("Stars!")

# Load the star image.
star = pygame.image.load("star.png")

# Draw eight stars at 45° angles.
star_num = 0
while star_num < 8:
# Compute the x and y coordinates of a star.
star_num = star_num + 1
x = CENTER + RADIUS * math.cos(star_num * PI_OVER_4)
y = CENTER + RADIUS * math.sin(star_num * PI_OVER_4)

# Add the star to the drawing.
screen.blit(star, (x,y))

# Show the drawing.
pygame.display.flip()

# Quit when the user presses enter.
input("Press enter to quit...")
pygame.quit()
15 changes: 15 additions & 0 deletions Chapter_01/Listing 1_13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Validate fireworks input."""

# Annotate variables
num_stars: int

# Obtain the number of stars from the user.
num_stars = int(input("How many stars? "))

# Check that the value is between 2 and 360.
while num_stars < 2 or num_stars > 360:
print("The number must be between 2 and 360.")
num_stars = int(input("How many stars? "))

# The input is now valid, print a message.
print("Your input is valid.")
35 changes: 35 additions & 0 deletions Chapter_01/Listing 1_14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Manage a library summer reading program."""

# Annotate and initialize variables.
max_minutes: int = -1
total_minutes: int = 0
max_name: str = None
instructions: str
num_children: int
name: str
num_minutes: int

# Display instructions to the user.
instructions = "Enter each child's name and the number of "
instructions += "minutes they read this week."
print(instructions)

# Obtain information for five children.
num_children = 0
while num_children < 5:
name = input("What is the child's name? ")
num_minutes = int(input("How many minutes did "
+ name + " read this week? "))
# Find the max so far and total.
if num_minutes > max_minutes:
max_minutes = num_minutes
max_name = name
total_minutes += num_minutes
# Update the counter.
num_children += 1

# Display a summary of reading information.
print(max_name + " is our star reader, with a total of "
+ str(max_minutes) + " minutes.")
print("The total number of minutes read by all of the"
+ " children was " + str(total_minutes) + ".")
30 changes: 30 additions & 0 deletions Chapter_01/Listing 1_15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Manage a library summer reading program."""

# Annotate and initialize variables.
max_minutes: int = -1
total_minutes: int = 0
max_name: str = None
name: str
num_minutes: int

# Display instructions to the user.
print("Enter each child's name and the number of minutes ")
print("they read this week. Enter 'quit' when done.")

# Obtain child information until done.
name = input("Enter a child's name or 'quit': ")
while name != "quit":
num_minutes = int(input("How many minutes did "
+ name + " read this week? "))
if num_minutes > max_minutes:
max_minutes = num_minutes
max_name = name
total_minutes += num_minutes
name = input("Enter a child's name or 'quit': ")

# Display a summary of reading information.
if total_minutes != 0:
print("The child who read the most is " + max_name
+ ", who read " + str(max_minutes) + " minutes.")
print("The total number of minutes read was "
+ str(total_minutes))
12 changes: 12 additions & 0 deletions Chapter_01/Listing 1_16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Manage a library summer reading program."""

# Annotate variables
num_children: int
name: str
num_minutes: int

# Obtain information for five children.
for num_children in range(5):
name = input("What is the child's name? ")
num_minutes = int(input("How many minutes did "
+ name + " read this week? "))
45 changes: 45 additions & 0 deletions Chapter_01/Listing 1_17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Draw firework stars with pygame."""

import math

# Import and initialize pygame.
import pygame
pygame.init()

# Define some size constants to make drawing easier.
PI_OVER_4: float = math.radians(45)
RADIUS: int = 120
SIZE: int = 480
CENTER: float = (SIZE - 40) / 2

# Annotate variables
screen: pygame.Surface
star: pygame.Surface
star_num: int
x: float
y: float

# Create a pygame window.
screen = pygame.display.set_mode((SIZE, SIZE))
pygame.display.set_caption("Stars!")

# Load the star image.
star = pygame.image.load("star.png")

# Draw eight stars at 45° angles.
for star_num in range(8):
# Compute the x and y coordinates of a star.
x = CENTER + RADIUS * math.cos(star_num * PI_OVER_4)
y = CENTER + RADIUS * math.sin(star_num * PI_OVER_4)

# Add the star to the drawing.
screen.blit(star, (x,y))

# Show the drawing.
pygame.display.flip()

# Quit when the user presses enter.
input("Press enter to quit...")
pygame.quit()


51 changes: 51 additions & 0 deletions Chapter_01/Listing 1_18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Draw firework stars with pygame."""

import math

# Import and initialize pygame.
import pygame
pygame.init()

# Define some size constants to make drawing easier.
PI_OVER_4: float = math.radians(45)
RADIUS: int = 120
SIZE: int = 480
CENTER: float = (SIZE - 40) / 2

# Annotate variables
screen: pygame.Surface
star: pygame.Surface
star_num: int
x: float
y: float
user_quit: bool
event: pygame.event.Event

# Create a pygame window.
screen = pygame.display.set_mode((SIZE, SIZE))
pygame.display.set_caption("Stars!")

# Load the star image.
star = pygame.image.load("star.png")

# Draw eight stars at 45° angles.
for star_num in range(8):
# Compute the x and y coordinates of a star.
x = CENTER + RADIUS * math.cos(star_num * PI_OVER_4)
y = CENTER + RADIUS * math.sin(star_num * PI_OVER_4)

# Add the star to the drawing.
screen.blit(star, (x,y))

# Show the drawing.
pygame.display.flip()

# Quit when the user closes the window.
user_quit = False
while not user_quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
user_quit = True

pygame.quit()

45 changes: 45 additions & 0 deletions Chapter_01/Listing 1_19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Draw a star at a random location for each user click."""

import random

# Import and initialize pygame.
import pygame
pygame.init()

# Define constants and annotate variables
SIZE: int = 480
screen: pygame.Surface
star: pygame.Surface
offset_w: float
offset_h: float
user_quit: bool
event: pygame.event.Event
x: int
y: int

# Create a pygame window.
screen = pygame.display.set_mode((SIZE, SIZE))
pygame.display.set_caption("Click to make a star!")

# Load the star image.
star = pygame.image.load("star.png")
offset_w = star.get_width() / 2
offset_h = star.get_height() / 2

# Draw a star for each click.
user_quit = False
while not user_quit:
for event in pygame.event.get():
# Process a quit choice.
if event.type == pygame.QUIT:
user_quit = True
# Process a click by drawing a star.
elif event.type == pygame.MOUSEBUTTONUP:
x = random.randint(0,SIZE) - offset_w
y = random.randint(0,SIZE) - offset_h
screen.blit(star, (x,y))
# Show the drawing.
pygame.display.flip()

pygame.quit()

16 changes: 16 additions & 0 deletions Chapter_01/Listing 1_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Calculate the angle between stars in an aerial firework."""

# Annotate variables
DEGREES_CIRCLE: int = 360
num_stars: int
angle: float

# Obtain the number of stars from the user.
num_stars = int(input("How many stars? "))

# Calculate the angle between the stars.
angle = DEGREES_CIRCLE / num_stars

# Tell the user the angle between stars.
print("You will need an angle of " + str(angle)
+ "\u00B0 between each star.")
Loading