diff --git a/lessons/01_Physics_for_Games/01_move.py b/lessons/01_Physics_for_Games/01_move.py index 9ebc459..b2f5359 100644 --- a/lessons/01_Physics_for_Games/01_move.py +++ b/lessons/01_Physics_for_Games/01_move.py @@ -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 @@ -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 diff --git a/lessons/01_Physics_for_Games/03_acceleration.py b/lessons/01_Physics_for_Games/03_acceleration.py index 95b36b5..8c2bc91 100644 --- a/lessons/01_Physics_for_Games/03_acceleration.py +++ b/lessons/01_Physics_for_Games/03_acceleration.py @@ -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 diff --git a/lessons/01_Physics_for_Games/04_gravity.py b/lessons/01_Physics_for_Games/04_gravity.py index 0a96361..39e7611 100644 --- a/lessons/01_Physics_for_Games/04_gravity.py +++ b/lessons/01_Physics_for_Games/04_gravity.py @@ -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 @@ -52,6 +52,7 @@ class GameSettings: # Main game loop running = True clock = pygame.time.Clock() +d_v_y = 0 while running: @@ -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 diff --git a/lessons/01_Physics_for_Games/05_gravity_bounce.py b/lessons/01_Physics_for_Games/05_gravity_bounce.py index 15d2166..fc66b5c 100644 --- a/lessons/01_Physics_for_Games/05_gravity_bounce.py +++ b/lessons/01_Physics_for_Games/05_gravity_bounce.py @@ -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 diff --git a/lessons/02_Classes_and_Objects/00.3_Parent and Child.py b/lessons/02_Classes_and_Objects/00.3_Parent and Child.py new file mode 100644 index 0000000..b0f35d7 --- /dev/null +++ b/lessons/02_Classes_and_Objects/00.3_Parent and Child.py @@ -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.") \ No newline at end of file diff --git a/lessons/02_Classes_and_Objects/00_Classes_and_Objects.ipynb b/lessons/02_Classes_and_Objects/00_Classes_and_Objects.ipynb index 9a9a69b..7b60c2b 100644 --- a/lessons/02_Classes_and_Objects/00_Classes_and_Objects.ipynb +++ b/lessons/02_Classes_and_Objects/00_Classes_and_Objects.ipynb @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2024-09-09T17:58:52.281983Z", @@ -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" ] }, { @@ -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" ] } ], @@ -475,7 +497,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python-Games", + "display_name": ".venv (3.12.11.final.0)", "language": "python", "name": "python3" }, @@ -489,7 +511,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.13.3" + "version": "3.12.11" } }, "nbformat": 4, diff --git a/lessons/02_Classes_and_Objects/01_Tom_the_Turtle.py b/lessons/02_Classes_and_Objects/01_Tom_the_Turtle.py index ec38e80..5688b47 100644 --- a/lessons/02_Classes_and_Objects/01_Tom_the_Turtle.py +++ b/lessons/02_Classes_and_Objects/01_Tom_the_Turtle.py @@ -32,6 +32,7 @@ import pygame + def event_loop(): """Wait until user closes the window""" while True: @@ -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 @@ -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() @@ -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() diff --git a/lessons/02_Classes_and_Objects/03_gravity_bounce_obj.py b/lessons/02_Classes_and_Objects/03_gravity_bounce_obj.py index 1b7e2f7..2efd244 100644 --- a/lessons/02_Classes_and_Objects/03_gravity_bounce_obj.py +++ b/lessons/02_Classes_and_Objects/03_gravity_bounce_obj.py @@ -21,9 +21,12 @@ """ import pygame +import random class Colors: + def __init__(self, colors:str): + self.colors = colors """Constants for Colors""" WHITE = (255, 255, 255) BLACK = (0, 0, 0) @@ -58,6 +61,7 @@ class Game: def __init__(self, settings: GameSettings): pygame.init() + self.settings = settings self.running = True @@ -93,21 +97,26 @@ def run(self): class Player: """Player class, just a bouncing rectangle""" - def __init__(self, game: Game): + def __init__(self, game: Game, posx, posy, velx, vely, color): self.game = game settings = game.settings + self.posx = posx + self.posy = posy + self.velx = velx + self.vely = vely + self.color = color self.width = settings.player_width self.height = settings.player_height - self.is_jumping = False + self.is_jumping = True self.v_jump = settings.jump_v_y - self.y = settings.player_start_y if settings.player_start_y is not None else settings.height - self.height - self.x = settings.player_start_x + self.y = posy + self.x = posx - self.v_x = settings.v_0_x # X Velocity - self.v_y = settings.v_0_y # Y Velocity + self.v_x = velx # X Velocity + self.v_y = vely # Y Velocity def update(self): """Update player position, continuously jumping""" @@ -144,14 +153,19 @@ def update_jump(self): self.is_jumping = True def draw(self, screen): - pygame.draw.rect(screen, Colors.BLACK, (self.x, self.y, self.width, self.height)) + pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height)) settings = GameSettings() game = Game(settings) -p1 = Player(game) -game.add_player(p1) +for i in range(10000): + p = Player(game, random.randint(0, 250), random.randint(0, 250), random.randint(0, 1000), random.randint(250, 1000), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) + game.add_player(p) +for i in range(3000): + p = Player(game, random.randint(0, 250), random.randint(0, 250), random.randint(0, 1000), random.randint(0, 250), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) + game.add_player(p) + game.run() diff --git a/lessons/03_Vectors/00_Vectors.ipynb b/lessons/03_Vectors/00_Vectors.ipynb index 9ab73f5..13d390e 100644 --- a/lessons/03_Vectors/00_Vectors.ipynb +++ b/lessons/03_Vectors/00_Vectors.ipynb @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -487,7 +487,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python-Games", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -501,7 +501,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.13.3" + "version": "3.12.11" } }, "nbformat": 4,