1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Sense HAT で落ちモノゲームを作った

Last updated at Posted at 2020-09-27

子供用に、Raspberry Piでゲームを作ってみました。Sense HAT練習も兼ねて。
クラックスの上半分、みたいなゲームです。

本当にただ「作った」ことを見せたかっただけの記事です。。。

IMG_5732.jpg

from sense_hat import SenseHat, ACTION_PRESSED, ACTION_HELD, ACTION_RELEASED
from time import sleep
from random import randint
from copy import copy

# Sense HAT 初期化
sense = SenseHat()
sense.clear()

red = (255, 0, 0)
blue = (0, 0, 255)
yellow=(255,255,0)
purple=(128,0,128)
green=(0,255,0)
indigg=(75,0,130)
orange=(255,128,0)
black=(0,0,0)

# 向きの固定
sense.set_rotation(0)

# パラメータ設定
fall_interval = 3
fall_speed = 0.5
fall_color = orange
fall_num = 8

# 落下物を生成し、配列に格納
blocks = list()
for i in range(0,fall_num):
    blocks.append((randint(0,7), 0-i*fall_interval))

bar_init = (3, 6)
bar_color = blue
sense.set_pixel(bar_init[0], bar_init[1], bar_color)
bar = copy(bar_init)

# ジョイスティックを左に倒したときの処理(受けブロックを左に進める)
def moveleft(event):
    global bar
    if event.action != ACTION_PRESSED:
        return
    sense.set_pixel(bar[0], bar[1], black)
    bar_moved_x = bar[0] - 1
    if bar_moved_x < 0:
        bar_moved_x = 0
    elif bar_moved_x > 7:
        bar_moved_x = 7
    bar = (bar_moved_x, bar[1])
    sense.set_pixel(bar[0], bar[1], bar_color)

# ジョイスティックを右に倒したときの処理(受けブロックを右に進める)
def moveright(event):
    global bar
    if event.action != ACTION_PRESSED:
        return
    sense.set_pixel(bar[0], bar[1], black)
    bar_moved_x = bar[0] + 1
    if bar_moved_x < 0:
        bar_moved_x = 0
    elif bar_moved_x > 7:
        bar_moved_x = 7
    bar = (bar_moved_x, bar[1])
    sense.set_pixel(bar[0], bar[1], bar_color)

# ジョイスティックのイベントハンドラに関数を設定
sense.stick.direction_left = moveleft
sense.stick.direction_right = moveright

# ゲーム開始
# 都度、落下物配列の各ブロックを一つずつ下に移動する。
# 落下物が一番下にたどり着いた(失敗)か、受けブロックが受け止めたか(セーフ)を判定。
# 全てのブロックが受け止められたら、成功。
blocks_tmp = copy(blocks)
failed = False
while blocks_tmp:
    tmp = list()
    for blk in blocks_tmp:
        blk_moved = (blk[0], blk[1] + 1)
        if blk_moved[1] > 7:
            failed = True
            break
            #sense.set_pixel(blk[0], blk[1], black)
            #continue
        if blk_moved == bar:
            sense.set_pixel(blk[0], blk[1], black)
            continue
        if blk[1] >= 0 and blk[1] < 8:
            sense.set_pixel(blk[0], blk[1], black)
        if blk_moved[1] >= 0 and blk_moved[1] < 8:
            sense.set_pixel(blk_moved[0], blk_moved[1], fall_color)
        blk = blk_moved
        tmp.append(blk_moved)
    print(tmp)
    blocks_tmp = copy(tmp)
    sleep(fall_speed)

# 結果表示
if not failed:
    sense.show_letter("O")
else:
    sense.show_letter("X")

sleep(3)
sense.clear()

del bar
del sense

では!

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?