[PYTHON] Programming education game with SenseHAT

So, again I made a game, so I will post it. It's getting fun.

I made a popular programming education game for children recently. That is to instruct (program) the route to move to the specified location. Move from orange to blue. However, if you just move it, it will be in a straight line vertically and horizontally, so I also added an obstacle (green).

I'm not using an event handler because this is a completely sequential process. However, it may be an ant to move by inertia.

For the time being, my elementary school daughter liked it. Yokatta. It has not been verified whether programming learning can be done with this. ..

IMG_5736.JPG

from sense_hat import SenseHat
from time import sleep
from random import randint

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)

num_disturbs = 20

#Random setting of start block (orange)
row_init=randint(0, 7)
col_init=randint(0, 7)
color_init=orange
sense.set_pixel(col_init, row_init, color_init)

#Random setting of goal block (blue)
row_target = randint(0, 7)
col_target = randint(0, 7)

#Make sure the goal is not the same as the start position
while row_target == row_init and col_target == col_init:
    row_target = randint(0, 7)
    col_target = randint(0, 7)

color_target = blue
sense.set_pixel(col_target, row_target, color_target)

#Randomly set jamming blocks
disturbs = list()
for i in range(0,num_disturbs):
    while True:
        row = randint(0, 7)
        col = randint(0, 7)
        if row == row_init and col == col_init:
            continue
        if row == row_target and col == col_target:
            continue
        duplicated = False
        for j in disturbs:
            if col == j[0] and row == j[1]:
                duplicated = True
                break
        if duplicated:
            continue
        disturbs.append((col, row))
        break

color_disturb = green
for i in disturbs:
    sense.set_pixel(i[0], i[1], color_disturb)

#Programming the direction of movement
#Complete with middle button
commands = list()
set_com=True
while set_com:
    for event in sense.stick.get_events():
        if event.action == "pressed":
            if event.direction == "middle":
                set_com = False
            else:
                commands.append(str(event.direction))

row=row_init
col=col_init
color=red

#Move from the start block as programmed (red)
for com in commands:
    sense.set_pixel(col, row, black)
    sense.set_pixel(col_init, row_init, color_init)
    if com == "up":
        row = row - 1
    elif com == "down":
        row = row + 1
    elif com == "left": 
        col = col - 1
    elif com == "right":
        col = col + 1
    elif com == "middle":
        row = row_init
        col = col_init
    if row > 7: row = 7
    if row < 0: row = 0
    if col > 7: col = 7
    if col < 0: col = 0
    sense.set_pixel(col, row, color)
    sleep(0.5)
    disturbed = False
    for i in disturbs:
        if col == i[0] and row == i[1]:
            disturbed = True
            break
    if disturbed:
        break

#Judge success or failure
if row == row_target and col == col_target:
    sense.show_letter("O")
else:
    sense.show_letter("X")

sleep(3)
sense.clear()

Recommended Posts

Programming education game with SenseHAT
Asynchronous programming with libev # 2
3. 3. AI programming with Python
Python programming with Atom
Competitive programming with python
Shader programming with pyOpenGL
Asynchronous programming with libev
Linear Programming with PuLP
Programming with Python Flask
Asynchronous programming with libev # 3
Programming with Python and Tkinter
Try programming with a shell!
Try GUI programming with Hy
Games using IMU with SenseHat
Balloon splitting game with pygame
Simple typing game with DragonRuby
Network programming with Python Scapy
[Python] Object-oriented programming learned with Pokemon
Easy Python + OpenCV programming with Canopy
Othello game development made with Python
Life game with Python! (Conway's Game of Life)
Solving game theory with combinatorial optimization
Sugoroku game and addition game with python