Skip to content
Open

hi #7

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
10 changes: 5 additions & 5 deletions lessons/01_Physics_for_Games/01_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ def main():
d_y = 0

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

if keys[pygame.K_RIGHT]:
if keys[pygame.K_d]:
d_x = v * d_t

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

if keys[pygame.K_DOWN]:
if keys[pygame.K_s]:
d_y = v * d_t

# Update the position of the square
Expand All @@ -83,7 +83,7 @@ def main():
screen.fill(BACKGROUND_COLOR)

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

# 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
2 changes: 1 addition & 1 deletion lessons/01_Physics_for_Games/03_acceleration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
d_t = 1 / FPS # Time step for physics calculations

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

# Movement direction: 1 for right, -1 for left
direction = 1
Expand Down
17 changes: 11 additions & 6 deletions lessons/01_Physics_for_Games/04_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class GameSettings:
player_size: int = 10
player_x: int = 100 # Initial x position of the player

jump_velocity: int = 200
jump_velocity: int = 100
white: tuple = (255, 255, 255)
black: tuple = (0, 0, 0)

gravity: float = 60.0 # acceleration, the change in velocity per frame
gravity: float = 100.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

Expand All @@ -52,6 +52,7 @@ class GameSettings:
# Main game loop
running = True
clock = pygame.time.Clock()
d_v_y = 0

while running:

Expand All @@ -61,13 +62,17 @@ class GameSettings:
running = False

# Continuously jump. If the player is not jumping, initialize a new jump
if is_jumping is False:
keys = pygame.key.get_pressed()

if keys[pygame.K_SPACE]:
if is_jumping is False:
# 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
d_v_y = -settings.jump_velocity
is_jumping = True

is_jumping = True
d_v_y = -settings.jump_velocity
if is_jumping is False:
d_v_y = settings.jump_velocity
# acelleration in sht y direction
a_y = settings.gravity

Expand Down
8 changes: 5 additions & 3 deletions lessons/01_Physics_for_Games/05_gravity_bounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ class GameSettings:
if event.type == pygame.QUIT:
running = False

key = pygame.key.get_pressed()
# Continuously jump. If the square is not jumping, make it jump
if is_jumping is False:
if key[pygame.K_SPACE]:
if is_jumping is False:
# Jumping means that the square 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

velocity_y = -settings.jump_velocity_y
velocity_x = settings.jump_velocity_x * x_direction
velocity_y = -settings.jump_velocity_y
velocity_x = settings.jump_velocity_x * x_direction

is_jumping = True

Expand Down
83 changes: 83 additions & 0 deletions lessons/02_Classes_and_Objects/00.3_Parent and Child.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Run Me!

class Person:
"""Person represents a person in our system."""

# This is the initializer, it gets run when we create a new object
def __init__(self, name: str, age: int):
"""Initializes a new Person object."""
self.name = name
self.age = age

def say_hello(self, message: str):
"""Prints a greeting to the console."""
print(f"Hello, my name is {self.name} and I am {self.age} years old. {message}")


class Parent(Person):
"""Parent represents a parent in our system."""

def __init__(self, name: str, age: int, spouse=None):
"""Initializes a new Parent object."""
super().__init__(name, age,) # Call Person.__init__ to initialize the name and age attributes
self.children = []

# Set our spose but also set the spouse's spouse to us
if spouse:
self.spouse = spouse
spouse.spouse = self

self.spouse = None

def add_child(self, child: Person):
"""Adds a child to the parent's list of children."""
self.children.append(child)


def say_hello(self, message: str):
"""Prints a greeting to the console."""

super().say_hello(message)
if self.spouse:
print(f"My spouse is {self.spouse.name}")

print(f"I have {len(self.children)} children.")

if len(self.children) > 0:
print("Their names are:")
for child in self.children:
print(f" {child.name} {child.age}")


class Child(Person):
"""Child represents a child in our system."""

def __init__(self, name: str, age: int, parents: list):
"""Initializes a new Child object."""
super().__init__(name, age) # Call Person.__init__ to initialize the name and age attributes
self.parents = parents

def say_hello(self, message: str):
"""Prints a greeting to the console."""
super().say_hello(message)
print(f"My parents are {', '.join([parent.name for parent in self.parents])}")


# Now lets make a family
mom = Parent("Alice Grot", 35)
dad = Parent("Bob Grot", 40, mom)

charlie = Child("Charlie Grot", 10, [mom, dad])
dahlia = Child("Dahlia Grot", 8, [mom, dad])

# Connect the children to the parents
mom.add_child(charlie)
mom.add_child(dahlia)
dad.add_child(charlie)
dad.add_child(dahlia)

dad.say_hello("Great to meet you!")
mom.say_hello("Hello!") # Call the say_hello method of the mom object
print()
dahlia.say_hello("Yo!")
charlie.say_hello("Hi.")
46 changes: 34 additions & 12 deletions lessons/02_Classes_and_Objects/00_Classes_and_Objects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-09T17:58:52.281983Z",
Expand Down Expand Up @@ -170,12 +170,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This alien is Red and has 25 eyes and 43 legs\n"
]
}
],
"source": [
"# Test Yourself by writing a class for an alien\n",
"\n"
"class Alien:\n",
"\n",
" def __init__(self, eyes: int, legs: int, color: str):\n",
" self.eyes = eyes\n",
" self.legs = legs\n",
" self.color = color\n",
" def description(self):\n",
" print(f\"This alien is {self.color} and has {self.eyes} eyes and {self.legs} legs\") \n",
"\n",
"al_The_Alien = Alien(25, 43, \"Red\")\n",
"\n",
"al_The_Alien.description()\n"
]
},
{
Expand Down Expand Up @@ -424,15 +443,18 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alice is 35 years old.\n",
"Charlie is 10 years old.\n"
"ename": "NameError",
"evalue": "name 'mom' is not defined",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 7\u001b[39m\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m get_name_and_age(person: Person):\n\u001b[32m 4\u001b[39m \u001b[33m\"\"\"Prints the name and age of a person.\"\"\"\u001b[39m\n\u001b[32m 5\u001b[39m print(f\"{person.name} is {person.age} years old.\")\n\u001b[32m 6\u001b[39m \n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m get_name_and_age(mom)\n\u001b[32m 8\u001b[39m get_name_and_age(charlie)\n\u001b[32m 9\u001b[39m \n\u001b[32m 10\u001b[39m \n",
"\u001b[31mNameError\u001b[39m: name 'mom' is not defined"
]
}
],
Expand Down Expand Up @@ -475,7 +497,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python-Games",
"display_name": ".venv (3.12.11.final.0)",
"language": "python",
"name": "python3"
},
Expand All @@ -489,7 +511,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.3"
"version": "3.12.11"
}
},
"nbformat": 4,
Expand Down
40 changes: 29 additions & 11 deletions lessons/02_Classes_and_Objects/01_Tom_the_Turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import pygame



def event_loop():
"""Wait until user closes the window"""
while True:
Expand All @@ -46,12 +47,28 @@ def __init__(self, screen, x: int, y: int):
self.screen = screen
self.angle = 0 # Angle in degrees, starting facing right

def forward(self, distance):


def left(self, angle):
# Turn left by adjusting the angle counterclockwise
self.angle = (self.angle + angle) % 360



class More_Turtles(Turtle):
def __init__(self, screen, x:int, y:int):
self.screen = screen
self.x = x
self.y = y
self.angle = 0

def forward(self, distance, color:str):
# Calculate new position based on current angle
radian_angle = math.radians(self.angle)

start_x = self.x # Save the starting position
start_y = self.y
self.color = color

# Calculate the new position displacement
dx = math.cos(radian_angle) * distance
Expand All @@ -62,14 +79,7 @@ def forward(self, distance):
self.y -= dy

# Draw line to the new position
pygame.draw.line(self.screen, black, (start_x, start_y), (self.x, self.y), 2)

def left(self, angle):
# Turn left by adjusting the angle counterclockwise
self.angle = (self.angle + angle) % 360


# Main loop
pygame.draw.line(self.screen, color, (start_x, start_y), (self.x, self.y), 2)

# Initialize Pygame
pygame.init()
Expand All @@ -84,12 +94,20 @@ def left(self, angle):
black = (0, 0, 0)

screen.fill(white)
turtle = Turtle(screen, screen.get_width() // 2, screen.get_height() // 2) # Start at the center of the screen
turtle = More_Turtles(screen, screen.get_width() // 2, screen.get_height() // 2) # Start at the center of the screen

# Draw a square using turtle-style commands

def Get_X_and_Y():
print(turtle.x)
print(turtle.y)

for _ in range(4):
turtle.forward(100) # Move forward by 100 pixels
turtle.forward(100, "red") # Move forward by 100 pixels
turtle.left(90) # Turn left by 90 degrees
Get_X_and_Y()



# Display the drawing
pygame.display.flip()
Expand Down
Loading