-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace.py
119 lines (105 loc) · 4.45 KB
/
space.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import random
import dolp
# Image and animation definitions.
#
# Images are taken from https://github.com/ModusCreateOrg/evade-arduboy-game (Copyright (C) 2016 Modus Create Inc.)
spaceship = bytearray([0x10, 0x10, 0x00, 0x00, 0xb4, 0x00, 0x00, 0xc0, 0xfc, 0x15, 0x15, 0xfc, 0xc0, 0x00, 0x00, 0xb4, 0x00, 0x00, 0x40, 0xfc, 0x7f, 0x20, 0x38, 0x98, 0x49, 0xef, 0xef, 0x49, 0x98, 0x38, 0x20, 0x7f, 0xfc, 0x40])
enemy1 = bytearray([0x10, 0x10, 0x00, 0x80, 0xc0, 0x60, 0xf5, 0x35, 0x60, 0xc8, 0xc8, 0x60, 0x35, 0xf5, 0x60, 0xc0, 0x80, 0x00, 0x00, 0x36, 0x02, 0x00, 0x1d, 0x26, 0x4c, 0x93, 0x93, 0x4c, 0x26, 0x1d, 0x00, 0x02, 0x36, 0x00])
enemy1mask = bytearray([0x10, 0x10, 0x00, 0x80, 0xc0, 0x60, 0xff, 0xff, 0xf0, 0xf8, 0xf8, 0xf0, 0xff, 0xff, 0x60, 0xc0, 0x80, 0x00, 0x00, 0x3f, 0x03, 0x00, 0x1d, 0x3f, 0x7f, 0xff, 0xff, 0x7f, 0x3f, 0x1d, 0x00, 0x03, 0x3f, 0x00])
enemy2 = bytearray([0x10, 0x10, 0x00, 0xfc, 0x46, 0x23, 0x16, 0xdc, 0xa1, 0x6e, 0x6e, 0xa1, 0xdc, 0x16, 0x23, 0x46, 0xfc, 0x00, 0x00, 0x55, 0x10, 0x0a, 0x00, 0x02, 0x08, 0x10, 0x10, 0x08, 0x02, 0x00, 0x0a, 0x10, 0x55, 0x00])
enemy2mask = bytearray([0x10, 0x10, 0x00, 0xfc, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfc, 0x00, 0x00, 0x7f, 0x1f, 0x0f, 0x07, 0x07, 0x0f, 0x1f, 0x1f, 0x0f, 0x07, 0x07, 0x0f, 0x1f, 0x7f, 0x00])
explosion1 = bytearray([0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x90, 0x40, 0x40, 0x20, 0x40, 0x40, 0x84, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x05, 0x01, 0x02, 0x01, 0x21, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00])
explosion2 = bytearray([0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x88, 0x60, 0x24, 0x10, 0x20, 0x60, 0x80, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x03, 0x02, 0x24, 0x02, 0x03, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00])
explosion3 = bytearray([0x10, 0x10, 0x00, 0x00, 0x60, 0x44, 0x84, 0x40, 0xa6, 0x14, 0x10, 0x20, 0x42, 0x82, 0x00, 0x08, 0x40, 0x00, 0x00, 0x08, 0x00, 0x08, 0x01, 0x22, 0x04, 0x4b, 0x48, 0x05, 0x02, 0x01, 0x00, 0x60, 0x06, 0x00])
explosion4 = bytearray([0x10, 0x10, 0x00, 0x00, 0x00, 0x18, 0xa8, 0xc8, 0xf0, 0xf0, 0xf0, 0xe0, 0xe8, 0xb8, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39, 0x17, 0x17, 0x1f, 0x0f, 0x17, 0x13, 0x21, 0x78, 0x20, 0x00, 0x00])
explosionAnim = dolp.Animation([explosion1, explosion2, explosion3, explosion4], [100, 100, 100, 100])
# Entity types
BULLET_TYPE = 0
ENEMY_TYPE = 1
################
# Explosion
LIVE_TIMEOUT = 0
def update_explosion(obj):
t = obj.get_timeout(LIVE_TIMEOUT)
if dolp.check_timeout(t):
dolp.delete_entity(obj)
def explosion(x, y):
obj = dolp.new_entity()
obj.configure(x, y, 0, 0)
t = obj.new_timeout(LIVE_TIMEOUT)
dolp.set_timeout(t, 400, False)
obj.set_animation(explosionAnim)
obj.update(update_explosion)
################
# Bullet
def update_bullet(obj):
if not obj.is_moving():
dolp.delete_entity(obj)
def spawn_bullet(x, y):
w, h = 2, 4
speed = 10
obj = dolp.new_entity()
obj.set_type(BULLET_TYPE)
obj.configure(x, y, w, h)
obj.move_to(x, dolp.height() - 10, speed)
obj.collision()
obj.update(update_bullet)
################
# Space ship
def update_ship(obj):
w, h = 16, 16
if dolp.pressed(dolp.BUTTON_A):
spawn_bullet(obj.get_x(), obj.get_y() + int(h/2))
def spawn_ship():
w, h = 16, 16
obj = dolp.new_entity()
obj.configure(int(dolp.width() / 2), int(w/2), w, h)
obj.set_image(spaceship)
obj.update(update_ship)
################
# Enemy
class Enemy:
ENEMY_HIT_STATE = 0
w, h = 16, 16
count = 0
def collision(obj, other):
if other.get_type() != BULLET_TYPE:
return
if obj.get_state(Enemy.ENEMY_HIT_STATE) == 1:
return
dolp.delete_entity(obj)
dolp.delete_entity(other)
explosion(obj.get_x(), obj.get_y())
obj.set_state(Enemy.ENEMY_HIT_STATE, 1)
Enemy.count -= 1
def update(obj):
if not obj.is_moving():
randX = random.randint(10, 100)
randY = random.randint(16 + 8, dolp.height() - 8)
obj.move_to(randX, randY, 5)
def spawn():
if Enemy.count > 4:
return
obj = dolp.new_entity()
Enemy.count += 1
obj.configure(0, dolp.height() - 8, 16, 16)
obj.set_type(ENEMY_TYPE)
obj.set_image(enemy2, enemy2mask)
if random.randint(0, 1) == 1:
obj.set_image(enemy1, enemy1mask)
obj.update(Enemy.update)
obj.collision(Enemy.collision)
obj.set_state(Enemy.ENEMY_HIT_STATE, 0)
################
# Setup
dolp.begin()
spawn_timeout = dolp.new_timeout()
dolp.set_timeout(spawn_timeout, 1000, True)
spawn_ship()
################
# Loop
while True:
dolp.loop_begin()
if dolp.check_timeout(spawn_timeout):
Enemy.spawn()
dolp.loop_end()