Skip to content
Merged
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
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,3 @@ Open the file
[lessons/00_Getting_Started/README.md](lessons/00_Getting_Started/README.md) to
begin the lessons.

-------------------

Development of The LEAGUE's curriculum is generously funded by the Itzkowitz Family Foundation.
36 changes: 26 additions & 10 deletions lessons/01_Physics_for_Games/01_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
SQUARE_SIZE = 50
SQUARE_COLOR = (0, 128, 255) # Red-Green-Blue color in the range 0-255
BACKGROUND_COLOR = (255, 255, 255) # White
SQUARE_SPEED = 5
SQUARE_SPEED = 300
FPS = 60

v = SQUARE_SPEED # Speed of the square in pixels per second
d_t = 1 / FPS # Time step for physics calculations

# Initialize the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Move the Square")
Expand All @@ -29,8 +32,8 @@
# Main function
def main():
# Initial position of the square
square_x = SCREEN_WIDTH // 2 - SQUARE_SIZE // 2
square_y = SCREEN_HEIGHT // 2 - SQUARE_SIZE // 2
x = SCREEN_WIDTH // 2 - SQUARE_SIZE // 2
y = SCREEN_HEIGHT // 2 - SQUARE_SIZE // 2

running = True

Expand All @@ -48,27 +51,40 @@ def main():
# with a boolean value of whether they are pressed or not
keys = pygame.key.get_pressed()


# Calculate the change tin the position
d_x = 0
d_y = 0

# Move the square based on arrow keys
if keys[pygame.K_LEFT]:
square_x -= SQUARE_SPEED
d_x = -v * d_t

if keys[pygame.K_RIGHT]:
square_x += SQUARE_SPEED
d_x = v * d_t

if keys[pygame.K_UP]:
square_y -= SQUARE_SPEED
d_y = -v * d_t

if keys[pygame.K_DOWN]:
square_y += SQUARE_SPEED
d_y = v * d_t

# Update the position of the square
x = x + d_x
y = y + d_y


# Prevent the square from going off the screen
square_x = max(0, min(SCREEN_WIDTH - SQUARE_SIZE, square_x))
square_y = max(0, min(SCREEN_HEIGHT - SQUARE_SIZE, square_y))
x = max(0, min(SCREEN_WIDTH - SQUARE_SIZE, x))
y = max(0, min(SCREEN_HEIGHT - SQUARE_SIZE, y))

# This will clear the screen by filling it
# with the background color. If we didn't do this,
# the square would leave a trail behind it.
screen.fill(BACKGROUND_COLOR)

# Draw the square
pygame.draw.rect(screen, SQUARE_COLOR, (square_x, square_y, SQUARE_SIZE, SQUARE_SIZE))
pygame.draw.rect(screen, SQUARE_COLOR, (x, y, SQUARE_SIZE, SQUARE_SIZE))

# Update the display. Imagine that the screen is two different whiteboards. One
# whiteboard is currently visible to the player, and the other whiteboard is being
Expand Down
23 changes: 15 additions & 8 deletions lessons/01_Physics_for_Games/02_no_acceleration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
SCREEN_WIDTH, SCREEN_HEIGHT = 600, 600
SQUARE_SIZE = 50
SQUARE_COLOR = (255, 0, 0) # Red
SQUARE_SPEED = 5
SQUARE_SPEED = 300

FPS = 60 # Frames per second

d_t = 1 / FPS # Time step for physics calculations

# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Moving Red Square")

# Square starting position
x_pos = 0
y_pos = (SCREEN_HEIGHT - SQUARE_SIZE) // 2
x = 0
y = (SCREEN_HEIGHT - SQUARE_SIZE) // 2

# Movement direction: 1 for right, -1 for left
direction = 1
Expand All @@ -31,25 +35,28 @@
running = False

# Move the square, a bit each frame
x_pos += SQUARE_SPEED * direction

d_x = SQUARE_SPEED * direction * d_t

x += d_x

# Check for screen bounds and reverse direction if necessary
if x_pos + SQUARE_SIZE > SCREEN_WIDTH:
if x + SQUARE_SIZE > SCREEN_WIDTH:
direction = -1 # Move left
elif x_pos < 0:
elif x < 0:
direction = 1 # Move right

# Fill the screen with black (clears previous frame)
screen.fill((0, 0, 0))

# Draw the red square
pygame.draw.rect(screen, SQUARE_COLOR, (x_pos, y_pos, SQUARE_SIZE, SQUARE_SIZE))
pygame.draw.rect(screen, SQUARE_COLOR, (x, y, SQUARE_SIZE, SQUARE_SIZE))

# Update the display
pygame.display.flip()

# Frame rate control
pygame.time.Clock().tick(60)
pygame.time.Clock().tick(FPS)

# Quit Pygame
pygame.quit()
20 changes: 14 additions & 6 deletions lessons/01_Physics_for_Games/03_acceleration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
SCREEN_WIDTH, SCREEN_HEIGHT = 600, 600
SQUARE_SIZE = 50
SQUARE_COLOR = (255, 0, 0) # Red
K = .0004
FPS = 60
K = 3 # Spring constant, controls how strong the spring force is

# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
Expand All @@ -16,6 +17,10 @@
# Square starting position
x_pos = 20
y_pos = (SCREEN_HEIGHT - SQUARE_SIZE) // 2

d_t = 1 / FPS # Time step for physics calculations

mass = 2.0 # Mass of the square, used to calculate acceleration
velocity = 0

# Movement direction: 1 for right, -1 for left
Expand All @@ -27,22 +32,25 @@
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:

running = False

# Calculate the spring force, which is the force that pulls the square back
# to the center, as if it was attached to a spring
a = -K * (x_pos - (SCREEN_WIDTH-SQUARE_SIZE) // 2)


# Calculate the spring force, accounting for mass (F = -k*x)
# Force divided by mass gives acceleration (F = ma → a = F/m)
a = (-K * (x_pos - (SCREEN_WIDTH-SQUARE_SIZE) // 2)) / mass

# Update the velocity with the acceleration. Notice that we change
# the velocity by adding the acceleration, not setting it to the acceleration,
# and we change it a bit each frame.
velocity += a
velocity += a * d_t

# Update the position with the velocity. Like with the velocity, we change
# the position by adding the velocity, not setting it to the velocity, and
# we change it a bit each frame.
x_pos += velocity
x_pos += velocity * d_t

# Fill the screen with black (clears previous frame)
screen.fill((0, 0, 0))
Expand All @@ -54,7 +62,7 @@
pygame.display.flip()

# Frame rate control
pygame.time.Clock().tick(60)
pygame.time.Clock().tick(FPS)

# Quit Pygame
pygame.quit()
33 changes: 19 additions & 14 deletions lessons/01_Physics_for_Games/04_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ class GameSettings:
screen_height: int = 500
player_size: int = 10
player_x: int = 100 # Initial x position of the player
gravity: float = 0.3 # acelleration, the change in velocity per frame
jump_velocity: int = 15

jump_velocity: int = 200
white: tuple = (255, 255, 255)
black: tuple = (0, 0, 0)
tick_rate: int = 30 # Frames per second

gravity: float = 60.0 # acceleration, the change in velocity per frame
d_t: float = 1.0/30
m: float = 2.0 # mass of the player, used to calculate acceleration

# Initialize game settings
settings = GameSettings()



# Initialize screen
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))

Expand All @@ -45,7 +47,6 @@ class GameSettings:
settings.screen_height - settings.player_size,
settings.player_size, settings.player_size)

player_y_velocity = 0
is_jumping = False

# Main game loop
Expand All @@ -64,15 +65,19 @@ class GameSettings:
# Jumping means that the player is going up. The top of the
# screen is y=0, and the bottom is y=SCREEN_HEIGHT. So, to go up,
# we need to have a negative y velocity
player_y_velocity = -settings.jump_velocity
d_v_y = -settings.jump_velocity
is_jumping = True

# Update player position. Gravity is always pulling the player down,
# which is the positive y direction, so we add GRAVITY to the y velocity
# to make the player go up more slowly. Eventually, the player will have
# a positive y velocity, and gravity will pull the player down.
player_y_velocity += settings.gravity
player.y += player_y_velocity
# acelleration in sht y direction
a_y = settings.gravity

# Change in the velocity due to accelleration
d_v_y += a_y * settings.d_t

# Change in the position due to the velocity
d_y = d_v_y * settings.d_t

player.y += d_y

# If the player hits the ground, stop the player from falling.
# The player's position is measured from the top left corner, so the
Expand All @@ -82,14 +87,14 @@ class GameSettings:
# and stop the player from falling
if player.bottom >= settings.screen_height:
player.bottom = settings.screen_height
player_y_velocity = 0
d_v_y = 0
is_jumping = False

# Draw everything
screen.fill(settings.white)
pygame.draw.rect(screen, settings.black, player)

pygame.display.flip()
clock.tick(settings.tick_rate)
clock.tick( int(1/settings.d_t))

pygame.quit()
Loading