""" Space invaders - without killing aliens :) Code Toolkit: Python, Fall 2020 """ global fighterX fighterX = 350 global bulletY bulletY = -5 def setup(): size(700, 800) global swarmX, swarmY global swarmXDirection swarmX = 100 swarmY = 100 swarmXDirection = 2 def draw(): global swarmX, swarmY, swarmXDirection, bulletY background(0) # Draw swarm of aliens with a nested loop noStroke() i = 0 while i < 10: j = 0 while j < 5: fill(100, 100, 255) rect( swarmX + i * 40, swarmY + j * 80, 20, 40) j = j + 1 i = i + 1 # Draw fighter ship fill(150) noStroke() triangle( fighterX, 650, fighterX+25, 700, fighterX-25, 700) # Draw bullet stroke(255, 255, 150) strokeWeight(5) line(fighterX, bulletY, fighterX, bulletY-25) # Move alien swarm swarmX = swarmX + swarmXDirection # Check alien swarm direction for "bounce" if swarmX > 200: swarmXDirection = -2 swarmY = swarmY + 2 if swarmX < 100: swarmXDirection = 2 swarmY = swarmY + 2 # Move bullet bulletY = bulletY - 2 def keyPressed(): global bulletY, fighterX if key == ' ': bulletY = 625 if key == 'a': fighterX = fighterX - 2 if key == 'd': fighterX = fighterX + 2