Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dave Griffiths
creative-kids-coding-cornwall
Commits
a0f13435
Commit
a0f13435
authored
Apr 10, 2014
by
glenpike
Browse files
Complete draft of spider doc and add code for this.
parent
f1af0c6b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
373 additions
and
0 deletions
+373
-0
doc/projects/04-spider.md
doc/projects/04-spider.md
+343
-0
experiments/spider-doc.py
experiments/spider-doc.py
+30
-0
No files found.
doc/projects/0
3-house
.md
→
doc/projects/0
4-spider
.md
View file @
a0f13435
...
...
@@ -56,7 +56,6 @@ at point(15, 9, 0) and draw 7 blocks down using point(1, -7, 1)
box(OBSIDIAN, point(15, 9, 0), point(1, -7, 1))
//Testme
## Step 3 Make a function for drawing legs
*
But you need to draw 8 legs, 4 on each side. This is where we can use something
...
...
@@ -79,13 +78,13 @@ or just press the "tab" key once:
Now we can "call" this function by simply adding this to the bottom of your
program. Your code should now look something like this:
sphere(DIAMOND_BLOCK, point(0, 10, 0), 8)
sphere(DIAMOND_BLOCK, point(0, 10, 7 + 6), 6)
def leg():
box(OBSIDIAN, point(8, 10, 0), point(8, 1, 1))
box(OBSIDIAN, point(15, 9, 0), point(1, -7, 1))
sphere(DIAMOND_BLOCK, point(0, 10, 0), 8)
sphere(DIAMOND_BLOCK, point(0, 10, 7 + 6), 6)
leg()
*
Press F5 - your spider should appear with one leg, now we need to make
...
...
@@ -101,7 +100,8 @@ add "pos" to all of the positions of the shapes:
box(OBSIDIAN, pos+point(7, -1, 0), point(1, -7, 1))
*
We can now pass in the position when we call the function to draw a leg
in any position. This will draw 4 legs on one side of the body, spaced apart by a block.
in any position. This will draw 4 legs on one side of the body, spaced apart
by a block. Change the "leg()" line to the following:
leg(point(8, 10, 1))
leg(point(8, 10, 3))
...
...
@@ -141,5 +141,203 @@ for loop to do this:
*
Press F5 to test again - your legs should now be positioned either side
of the vertical centre line of the sphere.
## Step 5 Repeat after me.
So now we need to draw some legs the other side of the spider, we need to
do two things:
*
Position the legs on the other side of the body - this means changing one
of the parameters for our point to a negative value. We can be lazy and use
the same loop again, bonus! Copy the code which calls the leg function and
repeat it underneath the line you copied:
for i in range(-2, 2):
leg(point(8, 10, i * 2 + 1))
leg(point(8, 10, i * 2 + 1))
*
Right, now change one of the three parameters to a negative value. See if
you can work out the correct one to do before you press F5.
*
This code will only move the legs to start at the other side of the spider,
they will disappear inside it if you are correct. If you get it
wrong, try again. The correct code will be shown later.
## Step 6 Flip it.
*
The second thing we need to do is turn the legs around so they stick out
from the spider. We can use our function to help us again and pass in a
Boolean (True or False) parameter which says whether to flip them around
the other way.
*
Because of the way the sphere is drawn, we need to jiggle our maths a little
to make the legs draw correctly. The code below shows the function and the
for loop which calls it - replace your leg drawing code with this:
def leg(pos, flip):
if(flip):
box(GOLD_BLOCK, pos+point(-7, 0, 0), point(8, 1, 1))
box(OBSIDIAN, pos+point(-7, -1, 0), point(1, -7, 1))
else:
box(GOLD_BLOCK, pos, point(8, 1, 1))
box(OBSIDIAN, pos+point(7, -1, 0), point(1, -7, 1))
for i in range(-2, 2):
leg(point(8, 10, i * 2 + 1), False)
leg(point(-8, 10, i * 2 + 1), True)
## Step 7 Eyes.
*
Spiders have 8 eyes, so we will try and give our creature some. We will
do this programmatically, but remember how the sphere had an odd diameter?
This means we'll have to be arrange our eyes so they don't look off centre.
Computers are good at loops so we will use this to make a check pattern.
*
We also want the eyes to be at the front of the head, so we need to move
them from the centre of the spider forward - how far do you think this is?
*
The distance is the radius of the body + the diameter of the head.
*
First we will write some code to draw 8 blocks in a 3 x 2 check pattern.
We will make 2 loops to do 3 rows of 5 columns and draw a block every other loop.
We use a Boolean "flag" like the flip for the legs to decide whether to
draw a block. Each time we go through our loop we "toggle" the flag, to be
the opposite of what it was last time. This means we can do a check pattern
quite simply.
*
Add the following code to the bottom of your file:
draw = True
for x in range(-2, 3):
for y in range(-1, 2):
if True == draw:
box(GOLD_BLOCK, point(x, y + 10, 20), point(1, 1, 1))
draw = ~draw
*
Press F5 to test and you should see the eyes appear in a check pattern
in front of the spiders head. We have a gap between the head and eyes because
we chose "20" for the z- position just to get it working.
*
Right, lets put this in a function so we can tell it where to put the
eyes easily. Remove the code you just wrote and replace it with the following:
def eyes(pos):
draw = True
for x in range(-2, 3):
for y in range(-1, 2):
if True == draw:
box(GOLD_BLOCK, point(pos.x + x, pos.y + y, pos.z), point(1, 1, 1))
draw = ~draw
eyes(point(0, 10, 19))
## Step 8 Tidy up.
*
Right we've got a spider like thing on the screen, but what about those colours?
*
Lets make a function to draw the spider with a colour for the body and head
and another colour for the legs and eyes.
*
Whilst we are here, let's make it so you can draw the spider in any position on
the map.
*
We need to move our functions round a little, so some copy and paste might be useful.
Try to move all your "def" functions above your function calls so your code looks
like this:
from dbscode_minecraft import *
bulldoze()
def leg(pos, flip):
if(flip):
box(GOLD_BLOCK, pos+point(-7, 0, 0), point(8, 1, 1))
box(OBSIDIAN, pos+point(-7, -1, 0), point(1, -7, 1))
else:
box(GOLD_BLOCK, pos, point(8, 1, 1))
box(OBSIDIAN, pos+point(7, -1, 0), point(1, -7, 1))
def eyes(pos):
draw = True
for x in range(-2, 3):
for y in range(-1, 2):
if True == draw:
box(GOLD_BLOCK, point(pos.x + x, pos.y + y, pos.z), point(1, 1, 1))
draw = ~draw
sphere(DIAMOND_BLOCK, point(0, 10, 0), 8)
sphere(DIAMOND_BLOCK, point(0, 10, 7 + 6), 6)
for i in range(-2, 2):
leg(point(8, 10, i * 2 + 1), False)
leg(point(-8, 10, i * 2 + 1), True)
eyes(point(0, 10, 19))
*
Now create a spider function which draws the 2 spheres, the legs and eyes.
This needs to take 3 parameters, the colours and the position.
def spider(body_colour, leg_colour, pos):
*
Which code goes inside the function?
*
How would you call the function?
*
Can you change the sphere functions to use the body colour?
*
Can you change the sphere functions to draw the body and head in the position
you chose?
*
Can you add another parameter to the leg and eye functions so you can
tell it the colour you want?
*
Can you reposition the legs with the position of the spider?
*
Here is the whole code that should do all this:
from dbscode_minecraft import *
bulldoze()
def leg(pos, flip, colour):
if(flip):
box(colour, pos+point(-7, 0, 0), point(8, 1, 1))
box(colour, pos+point(-7, -1, 0), point(1, -7, 1))
else:
box(colour, pos, point(8, 1, 1))
box(colour, pos+point(7, -1, 0), point(1, -7, 1))
def eyes(pos, colour):
draw = True
for x in range(-2, 3):
for y in range(-1, 2):
if True == draw:
box(colour, point(pos.x + x, pos.y + y, pos.z), point(1, 1, 1))
draw = ~draw
def spider(body_colour, leg_colour, pos):
sphere(body_colour, pos, 8)
sphere(body_colour, pos+point(0, 0, 13), 6)
for i in range(-2, 2):
leg(pos+point(8, 0, i * 2 + 1), False, leg_colour)
leg(pos+point(-8, 0, i * 2 + 1), True, leg_colour)
eyes(pos+point(0, 0, 19), leg_colour)
spider(OBSIDIAN, GOLD_BLOCK, point(0, 10, 0))
## Step 9 Spidercraft
*
So you can make a spider, but can you make more of them? How might you do
that?
*
Could you make 3 spiders in a line with different colours?
*
How might you make things a bit more random, could you make each spider
have a random size? If you do this, how do you make the head and legs stay in
proportion to the body?
*
What happens if you try to draw 10 spiders?
*
Could you invent some new creatures?
*
Could you re-use some of the code to draw a snowman?
experiments/spider-doc.py
0 → 100644
View file @
a0f13435
from
dbscode_minecraft
import
*
bulldoze
()
def
leg
(
pos
,
flip
,
colour
):
if
(
flip
):
box
(
colour
,
pos
+
point
(
-
7
,
0
,
0
),
point
(
8
,
1
,
1
))
box
(
colour
,
pos
+
point
(
-
7
,
-
1
,
0
),
point
(
1
,
-
7
,
1
))
else
:
box
(
colour
,
pos
,
point
(
8
,
1
,
1
))
box
(
colour
,
pos
+
point
(
7
,
-
1
,
0
),
point
(
1
,
-
7
,
1
))
def
eyes
(
pos
,
colour
):
draw
=
True
for
x
in
range
(
-
2
,
3
):
for
y
in
range
(
-
1
,
2
):
if
True
==
draw
:
box
(
colour
,
point
(
pos
.
x
+
x
,
pos
.
y
+
y
,
pos
.
z
),
point
(
1
,
1
,
1
))
draw
=
~
draw
def
spider
(
body_colour
,
leg_colour
,
pos
):
sphere
(
body_colour
,
pos
,
8
)
sphere
(
body_colour
,
pos
+
point
(
0
,
0
,
13
),
6
)
for
i
in
range
(
-
2
,
2
):
leg
(
pos
+
point
(
8
,
0
,
i
*
2
+
1
),
False
,
leg_colour
)
leg
(
pos
+
point
(
-
8
,
0
,
i
*
2
+
1
),
True
,
leg_colour
)
eyes
(
pos
+
point
(
0
,
0
,
19
),
leg_colour
)
spider
(
OBSIDIAN
,
GOLD_BLOCK
,
point
(
0
,
10
,
0
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment