TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

What is it?


In this project we are going to make a Photo Booth in Minecraft that when the player Steve, enters, the Pi Camera is triggered and this takes a picture of the person playing the game and saves the image to view later.  This was an concept that came to me whilst at Picademy in the Minecraft workshop, a photo booth within a game that reacts in the real world. (TIOT)

1. Getting Started


Before you get started you will be required to install the Python Pi-Camera Module and the Raspberry Pi  Minecraft edition.

In the LX Terminal type:
sudo apt-get upgrade
sudo apt-get update


apt-get install python-picamera or apt-get install python3-picamera

Next install the Minecraft files:
wget http://goo.gl/o2aene -O mcpi.tar.gz --no-check-certificate

Once the file has downloaded you will need to extract the file,  this command will create a new directory in your home folder called mcpi, which is where the Minecraft installation files will be set up.

Type:
tar xzf mcpi.tar.gz
then to run the game type:
cd mcpi 
./minecraft-pi

2. Set up the PiCamera


Once your PiCamera is attached we need to create a function to capture the picture, this is very simple and can be achieved in a few lines of code.  The picture can be previewed using the code, camera.start_preview().  To take the picture use the code camera.capture('selfie.jpg').  Where selfie is replaced with the name you wish to call the file, the filename.  You will be required to run the Python code as a super user, so open the LX Terminal and type sudo IDLE, this opens Python, then create the the function below.

import time
import picamera

def take_a_selfie():
    with picamera.PiCamera() as camera:
            camera.start_preview()
            time.sleep(2)
            camera.capture('selfie.jpg')
take_a_selfie()



Save the file into the pi.mcpi/api/python folder.  Press F5 or run to test that the Pi Camera takes a picture.

3. Find the Players Location


The next part of the program is to identify where exactly the player is within the Minecraft world. There are three coordinates X, Y and Z that can be used to reference the position of the player.  Once we know the position of the player we can then compare that with the position of the Photo-booth, if they match then you are in the photo-booth.  Remember this code needs to be run as a super user.

The code to find your location and print the co-ordinates is:
import mcpi.minecraft as minecraft
import time
mc = minecraft.Minecraft.create()
while True:
    pos = mc.player.getPos()
    x = pos.x
    y = pos.y
    z = pos.z
    print x, y, z
    time.sleep(2)

"Say Cheese!"




4. Are you in the Photo-Booth?


The final stage is to create the Photo-Booth and then run the code to compare the player's position and the position of the Photo-Booth, if they match that means that player is in the booth and therefor the Pi Camera will be triggered and a take a picture.  A simple IF, ELIF statement makes this easy.  In the example below the post to chat command is used to notify the player that they are in the booth. 

import mcpi.minecraft as minecraft
import time
mc = minecraft.Minecraft.create()

while True:
    pos = mc.player.getPos()
    x = pos.x
    y = pos.y
    z = pos.z
    print x, y, z
    time.sleep(2)
    
    if x == -49.7 and y == 8.0 and z <= -79.7:
        print "You are at the photobooth!"
        mc.postToChat("You are in the Photobooth!")
    else:
        print ""


5. Putting it all Together


Now add the PiCamera code from step 2 into the if statement so that if the player's position matches the Photo-booth then take a picture.  Remember that the program needs to be saved into the mcpi/api/python folder.  Start a new MC game, when it is loaded then run the Python program.  Locate your Photo-booth walk in and Smile!   You could adapt the code so that it saves anew picture or takes only one picture and then pauses before taking another, even better see if your friends on multi-player can be papped!


Code Links


Picture
Final Code
File Size: 0 kb
File Type: py
Download File

Copyright 2020 TeCoEd @dan_aldred