TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
  • Python
    • Learn Python >
      • Python Modules
    • 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
    • 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
    • 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
    • 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
  • 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
  • Downloading
  • Contact Me
    • Drone Hacks
    • Hologram Machine
    • AI

Minecraft Mine-Sweeper


You may remember or have even played the classic Minesweeper game which dates back to the 1960's as one of the earliest mainframe computer games.  Over the years it has been bundled with most operating systems and even featured as a mini game variation on the New Super Mario Bros.  This project will walk you through how you can create a simple version in Minecraft, yes a Minecraft Mine-Sweeper!  The program sets out a arena of blocks, one of the blocks is a Mine, each time you stand on a block you turn it to gold and collect points, but, watch out for the Mine as it will end the game, (and cover you in lava!).

1. Getting Started


The first step is to ensure that your Raspberry Pi is up to date,

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

Next load up the Python editor, IDLE and start a new window.  You need to import the following modules by typing,

import random
import time
from mcpi import minecraft
mc = minecraft.Minecraft.create()



2. Create the Board


To create the arena or board which will contain the Mine is a simple step, firstly you find your players currently location in the world using the code, 

x, y, z = mc.player.getPos() 

Then use the mc.setBlocks  code to to place blocks,

mc.setBlocks(x, y-1, z, x+20, y-1, z+20, 58).  

The 58, is the ID of the block which is a crafting table. You can chose your own material and change it by alerting this number, a list of block IDs can be found here:  You can also increase or decrease the size of the board by changing the + 20.  In the code example above the board size is 20 x 20 blocks which gives you a 400 hundred block arena to play within.

3. Creating the Mine


To create the Mine you use a code to generate a random number between 1 and 10, mine = random.randrange(0, 11, 1)

In step 2 you found the player's location on the board, this data, the X, Y, and Z position can be used to place the Mine on the board.  Take the player's position and add the random number to the position - this creates a random Mine block anywhere on the board.  

mine_x = int(x+mine)
mine_y = int(y-1)
mine_z = int(z+mine)


The last step is to use the Set Block to place the Mine,  use the code below,
mc.setBlock(mine_x, mine_y, mine_z,58) The 58 is the block ID which you can change if your wish to see where the Mine has been placed.  This useful for testing that the rest of the code is working correctly.

Minecraft Mine-Sweeper



4. Check the Player's Position


The last part of the game is to create a while loop which checks whether you are standing on the Mine, if you are then it is Game Over, if not, then the block changes to gold.  Because the player's position is used to build the original board and place the Mine you are required to find the players position again and store it as new variable (otherwise the board shifts around).   To change the block to gold when the player is standing on the block, use the code,  

mc.setBlock(x1, y1-1, z1, 41), 

41 is the ID for gold.  You can customise the block to change to any block you wish.  However, if you trigger the Mine then you can flood the board with Lava:

mc.setBlocks(x-5, y+1, z-5, x+5, y+2, z+5, 10) 

while True:      
    x1, y1, z1 = mc.player.getTilePos()
    time.sleep(0.1)
    
    if (x1, y1-1, z1) == (mine_x, mine_y, mine_z):
        mc.setBlocks(x-5, y+1, z-5, x+5, y+2, z+5, 10) 
        mc.postToChat("G A M E  O V E R")
        break
    else:
        mc.setBlock(x1, y1-1, z1, 41)
        
mc.postToChat("GAME OVER")  
 

Use the mc.postToChat to send and display a message in the Minecraft World updating you that the game is over - it will also display a score which is based on the number of seconds that you remained alive before your triggered to Mine - The full and complete program can be download below form GitHub, feel free to develop the program further, maybe add more mines, make the board bigger or even indicate to the player how close to the mine they are.


5. Download the Code


Picture
✕