2026年1月15日 星期四

[Q霸小車] 第一堂 Q霸小車九種能力

 


nakecode:


Python程式:


  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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def on_received_number(receivedNumber):
    global mode
    basic.show_number(receivedNumber)
    mode = receivedNumber
radio.on_received_number(on_received_number)

def on_button_pressed_a():
    global mode
    if select == 0:
        if mode == 1:
            cuteBot.forward()
        elif mode == 6:
            for index in range(5):
                right.show_color(neopixel.colors(NeoPixelColors.YELLOW))
                basic.pause(500)
                cuteBot.color_light(cuteBot.RGBLights.RGB_L, 0xffff00)
                basic.pause(500)
                right.show_color(neopixel.colors(NeoPixelColors.BLACK))
                cuteBot.closeheadlights()
                basic.pause(500)
    else:
        mode = (mode + 1) % 11
        basic.show_number(mode)
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_logo_pressed():
    global select
    basic.show_icon(IconNames.ANGRY)
    select = 1
    basic.show_number(mode)
input.on_logo_event(TouchButtonEvent.PRESSED, on_logo_pressed)

def on_button_pressed_b():
    global select, init
    if select == 0:
        if mode == 1:
            cuteBot.backforward()
        elif mode == 6:
            for index2 in range(5):
                left.show_color(neopixel.colors(NeoPixelColors.YELLOW))
                basic.pause(500)
                cuteBot.color_light(cuteBot.RGBLights.RGB_R, 0xffff00)
                basic.pause(500)
                right.show_color(neopixel.colors(NeoPixelColors.BLACK))
                cuteBot.closeheadlights()
                basic.pause(500)
    else:
        select = 0
        init = 0
        basic.show_icon(IconNames.YES)
input.on_button_pressed(Button.B, on_button_pressed_b)

sonar = 0
strip: neopixel.Strip = None
right_speed = 0
left_speed = 0
init = 0
left: neopixel.Strip = None
right: neopixel.Strip = None
select = 0
mode = 0
mode = 0
select = 0
speed = 0
id2 = 1
radio.set_group(212)
basic.show_icon(IconNames.HEART)

def on_forever():
    global speed, left_speed, right_speed, strip, left, right, init, sonar
    if select == 0:
        if mode == 0:
            basic.show_number(id2)
        elif mode == 1:
            basic.show_icon(IconNames.HEART)
        elif mode == 2:
            basic.show_icon(IconNames.HEART)
            if speed > 100:
                speed = 0
            cuteBot.motors(speed, speed)
            speed += 1
        elif mode == 3:
            basic.show_icon(IconNames.HOUSE)
            cuteBot.forward()
            basic.pause(500)
            cuteBot.motors(100, 40)
            basic.pause(1000)
            cuteBot.forward()
            basic.pause(500)
            cuteBot.motors(40, 100)
            basic.pause(1000)
        elif mode == 4:
            basic.show_icon(IconNames.HEART)
            left_speed = randint(-100, 100)
            right_speed = randint(-100, 100)
            cuteBot.motors(left_speed, right_speed)
            basic.pause(1000)
        elif mode == 5:
            cuteBot.forward()
            if input.light_level() < 10:
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_L, 255, 255, 255)
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_R, 255, 255, 255)
            else:
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_R, 0, 0, 0)
                cuteBot.singleheadlights(cuteBot.RGBLights.RGB_L, 0, 0, 0)
        elif mode == 6:
            strip = neopixel.create(DigitalPin.P15, 2, NeoPixelMode.RGB)
            left = strip.range(0, 1)
            right = strip.range(1, 1)
        elif mode == 7:
            if not (cuteBot.tracking(cuteBot.TrackingState.L_R_LINE)):
                cuteBot.motors(20, 20)
            else:
                cuteBot.motors(-50, -50)
                basic.pause(300)
                cuteBot.motors(0, randint(50, 100))
                basic.pause(100)
        elif mode == 8:
            if cuteBot.tracking(cuteBot.TrackingState.L_UNLINE_R_LINE):
                cuteBot.motors(25, 10)
            elif cuteBot.tracking(cuteBot.TrackingState.L_LINE_R_UNLINE):
                cuteBot.motors(10, 25)
            elif cuteBot.tracking(cuteBot.TrackingState.L_R_LINE):
                cuteBot.motors(25, 25)
        elif mode == 9:
            if init == 0:
                init = 1
            else:
                sonar = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)
                cuteBot.motors(30, 30)
                if sonar > 2 and sonar < 10:
                    cuteBot.motors(0, -25)
                    basic.pause(1000)
                else:
                    cuteBot.motors(30, 30)
        else:
            if init == 0:
                init = 1
            else:
                sonar = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)
                cuteBot.motors(30, 30)
                if sonar > 5 and sonar < 10:
                    cuteBot.motors(0, 0)
                elif sonar < 5:
                    cuteBot.motors(-30, -30)
                else:
                    cuteBot.motors(30, 30)
basic.forever(on_forever)


沒有留言:

張貼留言