<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 7: Bug Zapper :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/index.html</link><description>Bugs fall down from the top. You shoot up to hit them. In this session you learn OOP: you write your first class of your own (Bug), and then two subclasses: one that swings and one that dives at you. Classes are the building blocks of almost every big program.
What you’ll learn today Writing a class with __init__ and your own methods Inheritance: a subclass that extends an existing class Keeping track of bullets in a list Collision detection between bullets and bugs Step 0: Setup Open Thonny. Download the starter via this link, unpack the ZIP and open main.py in Thonny. Click Run. You see a screen with a player at the bottom, but no bugs and no bullets yet. Problems? Ask a coach. Step 1: Spawning bugs and shooting The Bug class</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 7: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/mentor-notes/index.html</guid><description>Goal of this session At the end of the Basic checkpoint a ninja can:
Write a class with __init__, attributes and an update() method. Instantiate bugs and keep them in a list. Apply collision detection between bullets and bugs. What is not expected after session 7: subclasses, math.sin movement, DuikBug. Those are Stretch/Expert material.
Preparation (30 min before the session) Explain classes in 5 minutes on the projector. This is the most important new concept. Use a simple example: class Hond: with self.naam and a blaf() method. Ask ninjas for analogies. import math is already in the starter, so ninjas don’t have to look that up themselves for Step 3. Check the spawn timer in the starter (by default every 90 frames). Change it if the difficulty isn’t right for your group. Schedule (3 hours) Time Activity 0:00 to 0:10 Welcome, looking back at session 6 0:10 to 0:25 Projector: explaining classes and instances 0:25 to 1:30 Working on your own: steps 1 and 2 1:30 to 2:15 Stretch (SlingerBug) 2:15 to 2:45 Expert (DuikBug) + buffer 2:45 to 3:00 Showcase + cliffhanger Common questions “AttributeError: ‘Bug’ object has no attribute ‘alive’” self.alive = True is missing from __init__. Have the ninja check __init__: is the indentation right? Does alive have self. in front of it?</description></item><item><title>Session 7: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/cheatsheet/index.html</guid><description>Writing a class class Bug: def __init__(self, x, y): self.x = x self.y = y self.alive = True def update(self): self.y += 2 def draw(self, screen): pygame.draw.circle(screen, GREEN, (int(self.x), int(self.y)), 15) Using a class mijn_bug = Bug(200, 0) # create mijn_bug.update() # call a method mijn_bug.draw(screen) Subclass (inheritance) class SlingerBug(Bug): def __init__(self, x, y): super().__init__(x, y) # calls Bug.__init__ self.timer = 0 def update(self): # overrides Bug.update self.timer += 0.05 self.x = self.start_x + math.sin(self.timer) * 60 self.y += 1.5 math.sin for swinging import math self.x = start_x + math.sin(timer) * amplitude # increase the timer every frame: timer += 0.05 Keeping track of bullets bullets = [] # Create: bullets.append(pygame.Rect(x - 3, y - 20, 6, 12)) # Move: for b in bullets[:]: b.y -= 10 if b.y &lt; 0: bullets.remove(b) Collision bullet ↔ bug bug_rect = pygame.Rect(bug.x - 15, bug.y - 15, 30, 30) for bullet in bullets[:]: if bullet.colliderect(bug_rect): bug.alive = False bullets.remove(bullet) score += 1 break Spawn timer with USEREVENT SPAWN = pygame.USEREVENT + 1 pygame.time.set_timer(SPAWN, 1500) # every 1.5 seconds for event in pygame.event.get(): if event.type == SPAWN: bugs.append(Bug(random.randint(30, WIDTH - 30), -20))</description></item><item><title>Session 7: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/07-bug-zapper/demo/index.html</guid><description>Session 7 uses pure pygame. Show a GIF of the solution on the session page.
How to record Run the solution: cd code/sessions/07-bug-zapper/solution &amp;&amp; python main.py. Use a screen recorder: Linux: Peek or SimpleScreenRecorder. Windows: ScreenToGif. macOS: QuickTime → export to GIF via ffmpeg or Kap. Record ~15 seconds: show a normal Bug, a SlingerBug and a DuikBug being shot down. Save as bug-zapper.gif in static/sessions/07-bug-zapper/. Link to it in the worksheet via: ![Demo](/sessions/07-bug-zapper/bug-zapper.gif). Target size: ~800×500 pixels, ≤ 2 MB.</description></item></channel></rss>