Showing posts with label raspberry pi. Show all posts
Showing posts with label raspberry pi. Show all posts

Friday, 1 January 2016

Simple X-Wing in Minecraft

figure 1
This post is co-authored by my son Thomas as a joint project to get a X-Wing into Minecraft. The goal was get Python to build and move the X-Wing.

It builds on ideas from the book Adventures in Minecraft (see link at the bottom) on using Python and Minecraft using a Raspberry Pi.


1. Static X-Wing

To start with we just placed the X-Wing above the player by placing blocks in the shape (roughly) of the X-Wing. 

  • Find the position of the player;
  • To avoid building on top the player the starting position of the X-Wing is set by:
    • add 5 to the x position of the player;
    • add 10 to the y position of the player(The bit I have to keep reminding myself is the y-axis is vertical.);
    • add 5 to the z position of the player;
  • Using these values build using, Wool blocks, the X-Wing - 0 for white, and 14 for red blocks.
It is admittedly quite a simple code (see below) and the finished X-Wing can be seen in figure 1.

from mcpi.minecraft import Minecraft
from mcpi import block


mc=Minecraft.create()
pos=mc.player.getTilePos()
x=pos.x+5
y=pos.y+10
z=pos.z+5

mc.setBlock(x,y,z,block.WOOL.id,0)
mc.setBlock(x-1,y,z,block.WOOL.id,0)
mc.setBlock(x-2,y,z,block.WOOL.id,14)
mc.setBlock(x-3,y,z,block.WOOL.id,0)
mc.setBlock(x+1,y,z,block.WOOL.id,0)
mc.setBlock(x,y+1,z,block.WOOL.id,0)
mc.setBlock(x+1,y+1,z,block.WOOL.id,0)

mc.setBlock(x+2,y,z,block.WOOL.id,0)
mc.setBlock(x+2,y+1,z,block.WOOL.id,0)

#wings
mc.setBlock(x+1,y+2,z-1,block.WOOL.id,14)
mc.setBlock(x+1,y+2,z+1,block.WOOL.id,14)
mc.setBlock(x+1,y-1,z-1,block.WOOL.id,14)
mc.setBlock(x+1,y-1,z+1,block.WOOL.id,14)
mc.setBlock(x+1,y+3,z-2,block.WOOL.id,0)
mc.setBlock(x+1,y+3,z+2,block.WOOL.id,0)
mc.setBlock(x+1,y-2,z-2,block.WOOL.id,0)
mc.setBlock(x+1,y-2,z+2,block.WOOL.id,0)



2. Get it to move.

We want it to move across the screen, for the moment, happy for it to just be animated.


The block approach is ok for the static version, but could lead to mistakes with getting the co-ordinates right if we were not careful. There is an other option to be found in chapter 8 of the book of using MinecraftShape were the shape is defined as relative values to a starting block, so for what we were doing they don't change but the shape can move in across the world.



  • Essentially the same as before but using shape;
  • place the shape in the world;
  • move it vertical (looks like it is taking off);
  • move it horizontal (this bit can be seen in the video).

from mcpi.minecraft import Minecraft
from mcpi import block
import mcpi.minecraftstuff as minecraftstuff
import time

mc=Minecraft.create()
xPos=mc.player.getTilePos()
xPos.x=xPos.x+5
xPos.y=xPos.y+5
xPos.z=xPos.z+5

xWingBlocks=[
minecraftstuff.ShapeBlock(0,0,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(-1,0,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(-2,0,0,block.WOOL.id,14),
minecraftstuff.ShapeBlock(-3,0,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(1,0,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(0,1,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(1,1,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(2,0,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(2,1,0,block.WOOL.id,0),
minecraftstuff.ShapeBlock(1,2,-1,block.WOOL.id,14),
minecraftstuff.ShapeBlock(1,2,1,block.WOOL.id,14),
minecraftstuff.ShapeBlock(1,-1,-1,block.WOOL.id,14),
minecraftstuff.ShapeBlock(1,-1,1,block.WOOL.id,14),
minecraftstuff.ShapeBlock(1,3,-2,block.WOOL.id,0),
minecraftstuff.ShapeBlock(1,3,2,block.WOOL.id,0),
minecraftstuff.ShapeBlock(1,-2,-2,block.WOOL.id,0),
minecraftstuff.ShapeBlock(1,-2,2,block.WOOL.id,0)]

xWingShape=minecraftstuff.MinecraftShape(mc,xPos,xWingBlocks)
for count in range(1,20):
    time.sleep(0.1)
    xWingShape.moveBy(0,1,0)
for count in range(1,40):
    time.sleep(0.1)
    xWingShape.moveBy(-1,0,0)

xWingShape.clear()
    


The very shaky video below shows the X-Wing moving across the sky.




3. Where next
Two areas:






 All views are those of the author and should not be seen as the views of any organisation the author is associated with.

Wednesday, 30 December 2015

playing with Sonic Pi

If you haven't played with it SonicPi (http://sonic-pi.net/) is well worth a go, it combines programming, computational thinking and  music (or trying to create music in my case). Raspbian on the Raspberry Pi has it as standard, but  is great that SonicPi is available on the Mac and PC as well. 






Here is an simple (if I am honest, because of a lack of musical ability) piece I have been play with. You can end up playing for hours trying out different sounds, synths, effects etc.

in_thread do
  loop do
    sample  :ambi_choir, rate: 0.1, attack: 4, release: 4
    sleep 10
  end
end
2. times do
  sample :bass_trance_c, amp: 0.75, release: 3
  sleep 2
  sample :bass_trance_c, rate: 0.5, amp: 0.5, release: 5
  sleep 4
  sample :bass_trance_c, rate: 0.3, amp: 0.25, release: 9
  sleep 10
end

Here is the sneaky bit, even in the code above there a mixture of threads, loops, sequences as well designing of the 'shape' of the sounds. 

There is also a community producing material/examples. For example at https://gist.github.com/rbnpi/7713ac59ed553d3f336e you can find code for the Star Wars theme. It is very hard to resist playing with the theme and adding drums, etc.

There are some great tutorials available on The MagPi magazine site https://www.raspberrypi.org/magpi/issues/

All views are those of the author and should not be seen as the views of any organisation the author is associated with.

Friday, 9 January 2015

Raspberry Pi Junkbot - Home

Raspberry Pi Junkbot - Home: A new website providing more information of the Raspberry Pi Junkbot project (combining Raspberry Pi, Scratch programming and junk). This will be added to as the  project develops.





At the moment it includes

- Introduction

-Discussion about the robot controller card

-ScratchGPIO

- Example of the drawing bot.



All views are those of the author and should not be seen as the views of any organisation the author is associated with.

Answers not on the Screen

  2.  The answers not on the screen Hill, G. ,  Turner, S. J.  and  Childs, K.  (2017)  Abstract:  Reflection from two areas on the issues...