-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmicrobit.py
More file actions
87 lines (83 loc) · 2.77 KB
/
microbit.py
File metadata and controls
87 lines (83 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from microbit import *
import random
class Game():
def __init__(self):
self.frog = Frog(self, 2)
self.cars = []
car = Car(self)
self.cars.append(car)
self.counter_spawn = 0
self.counter_move = 0
self.score = 0
self.isStarted = False
def start(self):
self.isStarted = True
while self.isStarted:
if button_a.was_pressed():
self.frog.strafe("left")
if button_b.was_pressed():
self.frog.strafe("right")
if int(running_time()/600) > self.counter_move:
self.counter_move += 1
for i in self.cars:
i.move_down()
if int(running_time()/2000) > self.counter_spawn:
self.counter_spawn += 1
#display.scroll(len(cars))
if len(self.cars) < 20:
car = Car(self)
self.cars.append(car)
for i in self.cars:
for j in i.parts:
if (self.frog.x, self.frog.y) == (j[0], j[1]):
self.frog.die()
self.isStarted = False
@staticmethod
def try_set_pixel(x, y, b):
if x in range(0, 5) and y in range(0, 5):
display.set_pixel(x, y, b)
class Frog():
def __init__(self, game, x):
self.game = game
self.x = x
self.y = 4
self.brightness = 7
self.isAlive = True
self.game.try_set_pixel(self.x, self.y, self.brightness)
def strafe(self, pos):
self.game.try_set_pixel(self.x, self.y, 0)
if self.x < 4 and pos == "right":
self.x += 1
if self.x > 0 and pos == "left":
self.x -=1
self.game.try_set_pixel(self.x, self.y, self.brightness)
def die(self):
self.isAlive = False
display.scroll("u ded fam")
display.scroll("score: " + str(self.game.score))
display.clear()
class Car():
def __init__(self, game):
self.game = game
self.x = random.randint(0,4)
self.y = 0
self.parts = [[self.x, self.y], [self.x, self.y-1]]
self.brightness = 4
self.game.try_set_pixel(self.x, self.y, self.brightness)
def move_down(self):
if self.y != 6:
self.update(self.y+1)
else:
self.reset()
def update(self, y):
self.y = y
self.parts = [[self.x, self.y], [self.x, self.y-1]]
self.game.try_set_pixel(self.x, self.y-2, 0)
self.game.try_set_pixel(self.x, self.y, self.brightness)
def reset(self):
self.game.try_set_pixel(self.x, self.y-1, 0)
self.game.score += 1
self.x = random.randint(0,4)
self.update(self.y+1)
game = Game()
game.start()