Learn to Code via Tutorials on Repl.it!

← Back to all posts
##### 2D Minecraft (Part Two: The background)

Edit: Does not work in Python Turtle anymore sorry

Hey guys, sorry about this tutorial being so late. I’m just putting it up right now. So this part is going to be the random background. So we need to `import random`! And by the way this is all going to be the first code, so don’t type it after the code we typed last time, tipe it before.

``````import random

#we’re going to need a lot of  turtles for this. It’s going to be 8*8 pixels, which will all be 100 pixels wide. ``````

The first one will be made with a turtle, and it will be a random color. The turtle will have to move back 400 pixels (since it’s in the middle), and up 400 pixels. For this we should use the `goto` function/argument:

``````p1 = turtle.Turtle() #for clarifying that this is a turtle
p1.speed(0) #So it goes super fast
p1.up() #so it doesn't draw a line
p1.hideturtle() #so you can’t see the turtle

p1.goto(-400, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p1color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p1.color(p1color)

p1.begin_fill()
for i in range(4):
p1.forward(100)
p1.right(90)
p1.end_fill()``````

That should draw the first square, a different color almost every time. The rest is the exact thing, but with different turtle names, different variables, and different coordinates. So I’m going to go ahead and show you the rest of the code:

``````p2 = turtle.Turtle() #for clarifying that this is a turtle
p2.speed(0) #So it goes super fast
p2.up() #so it doesn't draw a line
p2.hideturtle() #so you can’t see the turtle

p2.goto(-300, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p2color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p2.color(p2color)

p2.begin_fill()
for i in range(4):
p2.forward(100)
p2.right(90)
p2.end_fill()

p3 = turtle.Turtle() #for clarifying that this is a turtle
p3.speed(0) #So it goes super fast
p3.up() #so it doesn't draw a line
p3.hideturtle() #so you can’t see the turtle

p3.goto(-200, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p3color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p3.color(p3color)

p3.begin_fill()
for i in range(4):
p3.forward(100)
p3.right(90)
p3.end_fill()

p4 = turtle.Turtle() #for clarifying that this is a turtle
p4.speed(0) #So it goes super fast
p4.up() #so it doesn't draw a line
p4.hideturtle() #so you can’t see the turtle

p4.goto(-0, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p4color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p4.color(p4color)

p1.begin_fill()
for i in range(4):
p1.forward(100)
p1.right(90)
p1.end_fill()

p5 = turtle.Turtle() #for clarifying that this is a turtle
p5.speed(0) #So it goes super fast
p5.up() #so it doesn't draw a line
p5.hideturtle() #so you can’t see the turtle

p5.goto(100, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p5color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p5.color(p5color)

p5.begin_fill()
for i in range(4):
p5.forward(100)
p5.right(90)
p5.end_fill()

p6 = turtle.Turtle() #for clarifying that this is a turtle
p6.speed(0) #So it goes super fast
p6.up() #so it doesn't draw a line
p6.hideturtle() #so you can’t see the turtle

p6.goto(200, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p6color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p6.color(p6color)

p5.begin_fill()
for i in range(4):
p5.forward(100)
p5.right(90)
p5.end_fill()

p7 = turtle.Turtle() #for clarifying that this is a turtle
p7.speed(0) #So it goes super fast
p7.up() #so it doesn't draw a line
p7.hideturtle() #so you can’t see the turtle

p7.goto(300, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p7color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p7.color(p7color)

p7.begin_fill()
for i in range(4):
p7.forward(100)
p7.right(90)
p7.end_fill()

p8 = turtle.Turtle() #for clarifying that this is a turtle
p8.speed(0) #So it goes super fast
p8.up() #so it doesn't draw a line
p8.hideturtle() #so you can’t see the turtle

p8.goto(100, 400) #so it goes to the position it’s supposed to go to (x is first, y is second)
p8color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p8.color(p8color)

p8.begin_fill()
for i in range(4):
p8.forward(100)
p8.right(90)
p8.end_fill()

p9 = turtle.Turtle() #for clarifying that this is a turtle
p9.speed(0) #So it goes super fast
p9.up() #so it doesn't draw a line
p9.hideturtle() #so you can’t see the turtle

p9.goto(-400, 300) #so it goes to the position it’s supposed to go to (x is first, y is second)
p9color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p9.color(p9color)

p9.begin_fill()
for i in range(4):
p9.forward(100)
p9.right(90)
p9.end_fill()

p10 = turtle.Turtle() #for clarifying that this is a turtle
p10.speed(0) #So it goes super fast
p10.up() #so it doesn't draw a line
p10.hideturtle() #so you can’t see the turtle

p10.goto(-400, 300) #so it goes to the position it’s supposed to go to (x is first, y is second)
p10color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p10.color(p10color)

p10.begin_fill()
for i in range(4):
p10.forward(100)
p10.right(90)
p10.end_fill()

p11 = turtle.Turtle() #for clarifying that this is a turtle
p11.speed(0) #So it goes super fast
p11.up() #so it doesn't draw a line
p11.hideturtle() #so you can’t see the turtle

p11.goto(-400, 300) #so it goes to the position it’s supposed to go to (x is first, y is second)
p11color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p11.color(p11color)

p11.begin_fill()
for i in range(4):
p11.forward(100)
p11.right(90)
p11.end_fill()

p12 = turtle.Turtle() #for clarifying that this is a turtle
p12.speed(0) #So it goes super fast
p12.up() #so it doesn't draw a line
p12.hideturtle() #so you can’t see the turtle

p12.goto(-400, 300) #so it goes to the position it’s supposed to go to (x is first, y is second)
p12color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p12.color(p12color)

p12.begin_fill()
for i in range(4):
p12.forward(100)
p12.right(90)
p12.end_fill()

p13 = turtle.Turtle() #for clarifying that this is a turtle
p13.speed(0) #So it goes super fast
p13.up() #so it doesn't draw a line
p13.hideturtle() #so you can’t see the turtle

p13.goto(-400, 300) #so it goes to the position it’s supposed to go to (x is first, y is second)
p13color = random.randint(1, 4) #yellow, green, blue, and brown are the colors I chose.
p13.color(p13color)

p13.begin_fill()
for i in range(4):
p13.forward(100)
p13.right(90)
p13.end_fill()``````

I’m pretty sure you get the pattern. You can finish it from there if you keep going. Anyways, thanks for reading! Here is the link to the original document. Just tap the link, request access, and I will give you access as soon as possible. Anyways, thanks for reading and goodbye.

hotnewtop
Phantin0micalSR (0)

It's saying "NameError: name 'turtle' is not defined on line 4"

any idea how to fix

omarElgohary (0)

all it does is add an arrow to the middle of the screen am i missing something or is this it?
also when is part 3?

FidgetDaHacker2 (5)

idk how, but you could just use like an array to make turtles maybe?

FidgetDaHacker2 (5)
``````#maybe:
import turtle
from random import randint
turts = []
colors = [(255, 255, 0),(0,255,0),(0,0,255),(50, 20, 20)]
wn = turtle.Screen()
wn.tracer(0,0)
for i in range(64):
turts.append(turtle.Turtle())
turts[i].speed(0)

for a in turts:
a.fillcolor(colors[randint(0,3)])
a.fill(True)
for b in range(4):
a.fd(100)
a.rt(90)
a.fill(False)
a.update()
#and now make some pattern for where the turtles go``````

There are propably quicker ways to do this, but I just used the simpler way. Probably a for loop that just changes the names of the turtles, like with a function probably. idk either

Add one more thing to the patern. After the random statement type this:

``````if p13color == 1:
p13color = yellow
elif p13color == 2:
p13color = green
elif p13color == 3:
p13color = blue
elif p13color == 4:
p13color = brown``````

I just used p13 for example, but you have to do it on all of them

CodingGoose (698)

Hi! I have a little problem. When the squares are drawn they aren't colored, they are just black. I wrote the code correctly