[PYTHON] I made a falling block game with Sense HAT

I made a game with Raspberry Pi for kids. Also serves as Sense HAT practice. Upper half of Cracks It's a game like.

It's an article that I really just wanted to show that I made it. .. ..

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 initialization
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)

#Fixed orientation
sense.set_rotation(0)

#parameter settings
fall_interval = 3
fall_speed = 0.5
fall_color = orange
fall_num = 8

#Generate falling objects and store them in an array
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)

#Processing when the joystick is tilted to the left (advance the receiving block to the left)
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)

#Processing when the joystick is tilted to the right (advance the receiving block to the right)
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)

#Set a function in the joystick event handler
sense.stick.direction_left = moveleft
sense.stick.direction_right = moveright

#game start
#Each time, move each block of the falling object array down one by one.
#Determine whether the fallen object has reached the bottom (failure) or the receiving block has received it (safe).
#Success if all blocks are accepted.
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)

#Result display
if not failed:
    sense.show_letter("O")
else:
    sense.show_letter("X")

sleep(3)
sense.clear()

del bar
del sense

Then!

Recommended Posts

I made a falling block game with Sense HAT
I made a roguelike game with Python
〇✕ I made a game
I made a bin picking game with Python
I made a Christmas tree lighting game with Python
I made a vim learning game "PacVim" with Go
I made a fortune with Python.
I made a daemon with Python
I made a simple typing game with tkinter in Python
I made a puzzle game (like) with Tkinter in Python
I made a Hex map with Python
I made a stamp generator with GAN
I made a poker game server chat-holdem using websocket with python
I made a simple blackjack with Python
I made a configuration file with Python
I made a WEB application with Django
I made a neuron simulator with Python
I made a stamp substitute bot with line
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
[Python] I made a Youtube Downloader with Tkinter.
I made a simple Bitcoin wallet with pycoin
I made a LINE Bot with Serverless Framework!
I made a random number graph with Numpy
I made a Mattermost bot with Python (+ Flask)
I made a QR code image with CuteR
[AWS] I made a reminder BOT with LINE WORKS
I made a Twitter BOT with GAE (python) (with a reference)
I made a household account book bot with LINE Bot
I made a ready-to-use syslog server with Play with Docker
I made a window for Log output with Tkinter
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
I made a school festival introduction game using Ren’py
I made blackjack with python!
I made a python text
I made a discord bot
I made blackjack with Python.
I made wordcloud with Python.
A story that stumbled when I made a chatbot with Transformer
I made a LINE BOT that returns parrots with Go
I made a package to filter time series with python
I made a simple book application with python + Flask ~ Introduction ~
I made a Numer0n battle game in Java (I also made AI)
I made a resource monitor for Raspberry Pi with a spreadsheet
I made a rigid Pomodoro timer that works with CUI
I made a surveillance camera with my first Raspberry PI.
I tried a stochastic simulation of a bingo game with Python
I made a plug-in that can "Daruma-san fell" with Minecraft
[AWS] I made a reminder BOT with LINE WORKS (implementation)
Life game with Python [I made it] (on the terminal & Tkinter)
I made a C ++ learning site
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a library to easily read config files with Python
I made a package that can compare morphological analyzers with Python
I made a game called Battle Ship using pygame and tkinter
I made a Line-bot using Python!