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. .. ..
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