Computer/Micro:bit
Updated: 2024. 2. 12. 23:53
hwaya.
빗방울 피하기
반응형
마이크로 비트를 제어하는 기본 문법을 학습하여 게임을 만든다.
# 게임셋팅
def gameSetting():
global start, gameSpeed, sprite, direction
start = False
gameSpeed = 500
sprite = game.create_sprite(2, 4)
# sprite가 처음 배치가 되면 오른쪽을 바라보고 있다. 초기 세팅을 해주는것.
direction = "right"
rainSetting()
# 게임오버 만들기
def spriteDie():
global start
if sprite.is_touching(rain1) or sprite.is_touching(rain2) or sprite.is_touching(rain3):
rain1.delete()
rain2.delete()
rain3.delete()
sprite.delete()
start = False
endingScreen()
# 게임 엔딩 화면
def endingScreen():
led.stop_animation()
basic.clear_screen()
basic.show_leds("""
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
""")
basic.pause(100)
basic.show_leds("""
. . . . .
. . # . .
. # . # .
. . # . .
. . . . .
""")
basic.pause(100)
basic.show_leds("""
. . # . .
. # . # .
# . . . #
. # . # .
. . # . .
""")
basic.pause(100)
basic.show_leds("""
. # . # .
# . . . #
. . . . .
# . . . #
. # . # .
""")
basic.pause(100)
basic.show_leds("""
# . . . #
. . . . .
. . . . .
. . . . .
# . . . #
""")
basic.pause(100)
basic.clear_screen()
def on_button_pressed_a():
global direction
if start == True:
# 왼쪽 90도로 계속 도는걸 방지하기 위한 조건문
if direction != "left":
sprite.turn(Direction.LEFT, 180)
sprite.move(1)
direction = "left"
else:
basic.show_leds("""
. . # . .
. # . # .
. # # # .
. # . # .
. # . # .
""")
input.on_button_pressed(Button.A, on_button_pressed_a)
def gameStartScreen():
for index in range(3):
basic.show_number(3 - index)
basic.pause(100)
# 빗방울 생성하기
def rainCreate():
global rain1, rain2, rain3
if not (rain3.is_deleted()):
if rain3.get(LedSpriteProperty.Y) == 2:
rain1 = game.create_sprite(randint(0, 4), 0)
rain1.turn(Direction.RIGHT, 90)
rain1.set(LedSpriteProperty.BLINK, 100)
if not (rain2.is_deleted()):
if rain2.get(LedSpriteProperty.Y) == 2:
rain2 = game.create_sprite(randint(0, 4), 0)
rain2.turn(Direction.RIGHT, 90)
rain2.set(LedSpriteProperty.BLINK, 100)
if not (rain1.is_deleted()):
if rain1.get(LedSpriteProperty.Y) == 2:
rain3 = game.create_sprite(randint(0, 4), 0)
rain3.turn(Direction.RIGHT, 90)
rain3.set(LedSpriteProperty.BLINK, 100)
# 빗방울 초기설정
def rainSetting():
global rain1, rain2, rain3
rain1 = game.create_sprite(randint(0, 4), 0)
rain1.turn(Direction.RIGHT, 90)
rain1.set(LedSpriteProperty.BLINK, 100)
rain2 = game.create_sprite(randint(0, 4), 0)
rain3 = game.create_sprite(randint(0, 4), 0)
rain2.delete()
rain3.delete()
# 빗방울 삭제하기
def rainDelete():
if rain1.get(LedSpriteProperty.Y) == 4:
rain1.delete()
if rain2.get(LedSpriteProperty.Y) == 4:
rain2.delete()
if rain3.get(LedSpriteProperty.Y) == 4:
rain3.delete()
def on_button_pressed_ab():
global start
if start == False:
led.stop_animation()
basic.clear_screen()
start = True
input.on_button_pressed(Button.AB, on_button_pressed_ab)
# 빗방울 떨어뜨리기
def rainMoving():
if not (rain1.is_deleted()):
rain1.move(1)
if not (rain2.is_deleted()):
rain2.move(1)
if not (rain3.is_deleted()):
rain3.move(1)
def on_button_pressed_b():
global direction
if start == True:
# 오른쪽으로 계속 방향 전환 방지
if direction != "right":
sprite.turn(Direction.RIGHT, 180)
sprite.move(1)
direction = "right"
else:
basic.show_leds("""
. # # . .
. # . # .
. # # . .
. # . # .
. # # . .
""")
input.on_button_pressed(Button.B, on_button_pressed_b)
rain3: game.LedSprite = None
rain2: game.LedSprite = None
rain1: game.LedSprite = None
direction = ""
sprite: game.LedSprite = None
gameSpeed = 0
start = False
gameSetting()
def on_every_interval():
if start:
rainCreate()
basic.pause(gameSpeed)
rainMoving()
basic.pause(gameSpeed)
spriteDie()
rainDelete()
loops.every_interval(gameSpeed, on_every_interval)
설명 생략, 필요시 요청바람.
반응형