scudFighter - Listing complet

Le programme complet

1
# The MIT License (MIT)
2
# Copyright (c) 2016 British Broadcasting Corporation.
3
# This software is provided by Lancaster University by arrangement with the BBC.
4
# Permission is hereby granted, free of charge, to any person obtaining a
5
# copy of this software and associated documentation files (the "Software"),
6
# to deal in the Software without restriction, including without limitation
7
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
# and/or sell copies of the Software, and to permit persons to whom the
9
# Software is furnished to do so, subject to the following conditions:
10
# The above copyright notice and this permission notice shall be included in
11
# all copies or substantial portions of the Software.
12
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
# DEALINGS IN THE SOFTWARE.
19
20
# Olivier Lecluse
21
# Avril 2019
22
23
from microbit import *
24
from time import ticks_us
25
import radio
26
27
radio.on()
28
29
SCUDFRIENDCOLOR=4
30
SCUDENMYCOLOR=6
31
32
speed=1
33
accel=0.2
34
score=0
35
position=2
36
scuds = []
37
lastLoop = ticks_us()
38
39
def position_move(p):
40
    global position
41
    display.set_pixel(position,4,0)
42
    position += p
43
    position = max(min(4,position),0)
44
    display.set_pixel(position,4,9)
45
46
def fire():
47
    scuds.append((position,3,-1))
48
    display.set_pixel(position,3,SCUDFRIENDCOLOR)
49
50
def process_fire():
51
    setpos=set()
52
    removeScuds=set()
53
    for i,s in enumerate(scuds):
54
        display.set_pixel(s[0],max(0,s[1]),0)
55
        s1 = (s[0],s[1]+s[2],s[2]) # nouvelle position
56
        s2 = (s1[0],s1[1],-s1[2])  # missile en collision eventuelle
57
        if s2 in setpos:
58
            # collision  :autodestruction des 2 scuds
59
            removeScuds.add(s1)
60
            removeScuds.add(s2)
61
            removeScuds.add(s)
62
            removeScuds.add((s[0],s[1],-s[2]))
63
        else :
64
            if 0<=s1[1]<4:
65
                setpos.add(s1)
66
                # deplacement du scud
67
                display.set_pixel(s1[0],s1[1],SCUDFRIENDCOLOR if s1[2]<0 else SCUDENMYCOLOR)
68
                scuds[i]=s1
69
            elif s1[1]==4:
70
                removeScuds.add(s)
71
                if s1[0] == position :
72
                    perdu()
73
            else :
74
                removeScuds.add(s)
75
                # envoi radio
76
                radio.send(str(s[0]))
77
78
    # Nettoyage de la liste des scuds
79
    for sr in removeScuds:
80
        display.set_pixel(sr[0],max(0,sr[1]),0)
81
        try:
82
            scuds.remove(sr)
83
        except:
84
            pass
85
86
def refresh():
87
    # On redessine l'affichage
88
    display.clear()
89
    display.set_pixel(position,4,9)
90
    for s in scuds:
91
        display.set_pixel(s[0],max(0,s[1]),SCUDFRIENDCOLOR if s[2]<0 else SCUDENMYCOLOR)
92
    
93
def perdu():
94
    global speed
95
    speed += accel # Le jeu s'accelere !
96
    
97
    # envoi radio "HIT"
98
    radio.send("HIT")
99
    
100
    # animation
101
    display.clear()
102
    anim = [
103
    Image("99999:90009:90009:90009:99999"),
104
    Image("00000:06660:06060:06660:00000"),
105
    Image("00000:00000:00300:00000:00000"),
106
    Image("00000:06660:06060:06660:00000"),
107
    Image("99999:90009:90009:90009:99999")]
108
    display.show(anim, delay=100, loop=False, wait=True)
109
    refresh()
110
111
def gagne():
112
    global speed,score
113
    speed += accel # Le jeu s'accelere !
114
    score += 1
115
    # animation
116
    display.clear()
117
    display.scroll(score)
118
    sleep(500)
119
    refresh()
120
    
121
def process_button():
122
    # Gesion des boutons
123
    ba = button_a.was_pressed()
124
    bb = button_b.was_pressed()
125
    if ba and bb :
126
        fire()
127
    else:
128
        if ba:
129
            position_move(-1)
130
        if bb:
131
            position_move(1)
132
133
def process_radio():
134
    global lastLoop
135
    # gestion des messages radio
136
    incoming = radio.receive()
137
    if incoming:
138
        if incoming == "HIT":
139
            gagne()
140
        elif len(incoming)==1:
141
            # arrivee d'un missile
142
            lastLoop=0
143
            scuds.append((int(incoming),-1,1))
144
            display.set_pixel(int(incoming),0,SCUDENMYCOLOR)
145
146
display.set_pixel(position,4,9)
147
while True:
148
    delay = 1000000/speed
149
    
150
    process_button()
151
    process_radio()
152
    
153
    # Gestion du timing
154
    loop = ticks_us()
155
    if(loop-lastLoop < delay):
156
        # attente 200ms pour la detection tir
157
        sleep(min((loop-lastLoop)//1000,200))
158
    else :
159
        lastLoop = loop
160
        process_fire()
161