Skip to content
Merged
18 changes: 18 additions & 0 deletions lib/tasks/project_components/dont-collide-starter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/python3

from p5 import *
from random import randint, seed

# Include global variables here


def setup():
# Put code to run once here


def draw():
# Put code to run every frame here


# Keep this to run your code
run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME: "Don't Collide!"
IDENTIFIER: "dont-collide-starter"
COMPONENTS:
- name: "main"
extension: "py"
location: "main.py"
index: 0
default: true
122 changes: 122 additions & 0 deletions lib/tasks/project_components/dont_collide_avoid_germs_example/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/python3

from p5 import *
from random import randint, seed

level = 1
score = 0

def safe_player():

global player_y

# Face
fill(200, 134, 145)
ellipse(mouse_x, player_y, 60, 60)

# Eyes
fill(178, 200, 145)
ellipse(mouse_x - 10, player_y - 10, 20, 20)
ellipse(mouse_x + 10, player_y - 10, 20, 20)
fill(0)
ellipse(mouse_x - 10, player_y - 10, 10, 10)
ellipse(mouse_x + 10, player_y - 10, 10, 10)
fill(255)
ellipse(mouse_x - 12, player_y - 12, 5, 5)
ellipse(mouse_x + 12, player_y - 12, 5, 5)

# Mouth
fill(0)
ellipse(mouse_x, player_y + 10, 15, 10)
fill(200, 134, 145)
ellipse(mouse_x, player_y + 5, 10, 10)

def crashed_player():

global player_y

# Face
fill(178, 200, 145)
ellipse(mouse_x, player_y, 60, 60)

# Eyes
fill(149, 161, 195)
ellipse(mouse_x - 10, player_y - 10, 20, 20)
ellipse(mouse_x + 10, player_y - 10, 20, 20)
fill(0)
ellipse(mouse_x - 10, player_y - 10, 10, 10)
ellipse(mouse_x + 10, player_y - 10, 10, 10)
fill(255)
ellipse(mouse_x - 12, player_y - 12, 5, 5)
ellipse(mouse_x + 12, player_y - 12, 5, 5)

# Mouth
fill(0)
ellipse(mouse_x, player_y + 15, 15, 10)
fill(178, 200, 145)
ellipse(mouse_x, player_y + 20, 10, 10)

def draw_player():

global player_y, safe, score, level

player_y = int(height * 0.8)

collide = get(mouse_x, player_y)
collide2 = get(mouse_x, player_y + 30)
collide3 = get(mouse_x + 30, player_y)
collide4 = get(mouse_x, player_y - 30)

if mouse_x < width: # off the left of the screen
collide2 = safe

if mouse_x > width: # off the right of the screen
collide3 = safe

#print(collide, collide2, collide3, collide4)

if collide == safe and collide2 == safe and collide3 == safe and collide4 == safe:
safe_player()
score += level
else: # Collided
crashed_player()
level = 0

def draw_obstacles():

global level

seed(41143644)

if frame_count & height == height - 1 and level < 5:
level += 1
print('You reached level', level)

for i in range(9 + level):
ob_x = randint(0, width)
ob_y = randint(0, height) + frame_count
ob_y %= height
text('🦠', ob_x, ob_y)

def setup():
# Put code to run once here
size(400, 400) # width and height
no_stroke()
text_size(40)
text_align(CENTER, TOP)

def draw():
# Put code to run every frame here
global safe, score, level

safe = Color(149, 161, 195)

if level > 0:
background(safe)
fill(145, 134, 126)
text('Score: ' + str(score), width/2, 20)
draw_obstacles()
draw_player()

# Keep this to run your code
run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME: "Don't Collide: Avoid the Germs"
IDENTIFIER: "avoid-germs-example"
COMPONENTS:
- name: "main"
extension: "py"
location: "main.py"
index: 0
default: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/python3

# Import library code
from p5 import *
from random import randint, seed

level = 1
score = 0

# The draw_obstacle function goes here
def draw_obstacles():

global level

seed(123456789)

if frame_count % width == width - 1 and level < 10:
level += 1
print('You reached level', level)

for i in range(6 + level):
ob_x = randint(0, width) - (frame_count * level)
ob_y = randint(0, height)
ob_x %= width # wrap around
text('💩', ob_x, ob_y)

# The draw_player function goes here
def draw_player():

global score, level

player_x = int(width * 0.2)
player_y = mouse_y

collide = get(player_x + 50, player_y + 15)
collide2 = get(player_x + 50, player_y - 15)
collide3 = get(player_x, player_y + 15)
collide4 = get(player_x, player_y - 15)
collide5 = get(player_x - 50, player_y + 15)
collide6 = get(player_x - 50, player_y - 15)

if player_y > height - 18: # Off the bottom of the screen
collide = safe
collide3 = safe
collide5 = safe

elif player_y < 18: # Off the top of the screen
collide2 = safe
collide4 = safe
collide6 = safe

if collide == safe and collide2 == safe and collide3 == safe and collide4 == safe:
image(car, player_x, player_y, 100, 31)
score += level
else:
text('💥', player_x, player_y)
level = 0


def setup():
# Setup your animation here
global car

size(400, 400)
car = load_image('car.png')
image_mode(CENTER)


def draw():
# Things to do in every frame
global score, safe, level
safe = Color(128)

if level > 0:
background(safe)
fill(255)
text_size(16)
text_align(RIGHT, TOP)
text('Score', width * 0.45, 10, width * 0.5, 20)
text(str(score), width * 0.45, 25, width * 0.5, 20)
text_size(20)
text_align(CENTER, TOP) # position around the centre, top
draw_obstacles()
draw_player()

run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME: "Don't Collide: Clean Car"
IDENTIFIER: "clean-car-example"
COMPONENTS:
- name: "main"
extension: "py"
location: "main.py"
index: 0
default: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/bin/python3

# Import library code
from p5 import *
from random import randint, seed

level = 1
score = 0
lives = 3
invun = 0

# The draw_obstacle function goes here
def draw_obstacles():

global level

seed(random_seed)

if frame_count % height == height - 1 and level < 8:
level += 1
print('You reached level', level)

for i in range(6 + level):
ob_x = randint(0, width)
ob_y = randint(0, height) + (frame_count * level)
ob_y %= height # wrap around
push_matrix()
translate(ob_x, ob_y)
rotate(degrees(randint(1, 359)+frame_count / 1000))
image(rock, 0, 0, randint(18,24), randint(18,24))
pop_matrix()


# The draw_player function goes here
def draw_player():

global score, level, lives, invun

player_y = int(height * 0.8)
player_x = mouse_x

collide = get(player_x, player_y)
collide2 = get(player_x - 18, player_y + 17)
collide3 = get(player_x + 18, player_y + 17)
collide4 = get(player_x, player_y + 25)

if player_x < width: # off the left of the screen
collide2 = safe

if player_x > width: # off the right of the screen
collide3 = safe

if (collide == safe and collide2 == safe and collide3 == safe and collide4 == safe) or invun > 0:
if lives == 0 and frame_count % 12 == 0:
tint(200, 0, 0)

image(rocket, player_x, player_y + 25, 64, 64)
score += level
invun -= 1
no_tint()

if invun > 0:
stroke(220)
fill(220, 220, 220, 60)
ellipse(player_x, player_y + 18, 47, 47)

elif lives > 0:
lives -= 1
invun = 50
tint(200, 0, 0)
image(rocket, player_x, player_y + 25, 64, 64)
no_tint()
score += level
else:
text('💥', player_x + 10, player_y + 5)
level = 0


def display_score():
global level

fill(255)
text_size(16)
text_align(RIGHT, TOP)
text('Score', width * 0.45, 10, width * 0.5, 20)
text(str(score), width * 0.45, 25, width * 0.5, 20)

if score > 10000:
level = 0
print('🎉🎉 You win! 🎉🎉')


def display_lives():
fill(255)
text_size(16)
text_align(LEFT, TOP)
text('Lives', width * 0.05, 10, 30, 20)

for i in range(lives):
image(rocket, width * 0.05 + i * 25, 40, 20, 20)


def setup():
# Setup your animation here
global rocket, rock, random_seed

text_size(40)
text_align(CENTER, TOP) # position around the centre, top
size(400, 400)

rocket = load_image('rocket.png')
rock = load_image('moon.png')
random_seed = randint(0, 1000000)

def draw():
# Things to do in every frame
global score, safe, level
safe = Color(0)

if level > 0:
background(safe)
fill(255)
image_mode(CENTER)
draw_obstacles()
draw_player()
display_score()
display_lives()

run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME: "Don't Collide: Dodge Asteroids"
IDENTIFIER: "dodge-asteroids-example"
COMPONENTS:
- name: "main"
extension: "py"
location: "main.py"
index: 0
default: true
Loading