Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Make 2D Minecraft in Python - Part One: Moving the Character Around
gatorade322 (123)

Making 2D Minecraft With Python: Part One

In this we will be doing the basic movements of the turtle. So first we will do all the imports and starting up stuff like Screen, Turtle, shape, and other stuff. So start by putting this into your code:

import turtle
 
mover = turtle.Turtle()
mover.up()
mover.shape("square")
listenner = turtle.Screen()
mover.speed(0)
mover.goto(-250, 0)

Now we’re going to check if the Up button is pushed (using the onkey function so we don’t have to push enter!). So type this in to your code:

while True:
 def forward():
   mover.forward(10)
 
 listenner.onkey(forward, "Up")

Now we can do backwards left and right!

 def backward():
   mover.backward(10)
 
 listenner.onkey(backward, "Down")
 
 def right():
   mover.right(10)
 
 listenner.onkey(right, "Right")
 
 def left():
   mover.left(10)
 
 listenner.onkey(left, "Left")
 listenner.listen()

At the end of all the code it should look like a square on a white screen that you can move around with the arrow keys

Comments
hotnewtop
JATKitty (0)

woooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooow

multicoder2021 (4)

This code works for me

import turtle
wn = turtle.Screen() 
t = turtle.Turtle() 
t.speed(0)
t.pensize(5)
def h1():
 t.fd(10)
 
def h2():
 t.left(45)
 
def h3():
   t.right(45)
 
def h4():
   wn.bye() 
wn.onkey(h1, "Up")
wn.onkey(h2, "Left")
wn.onkey(h3, "Right")
wn.onkey(h4, "esc")
wn.listen()
wn.mainloop()

Source: https://keepthinkup.wordpress.com/programming/python/python-turtle-handling-with-keypressarrow-key/
Edit: I ran this on Python Turtle, not Python 3.8 or 2.7 .

gatorade322 (123)

Edit: This doesn't work anymore on Python turtle graphics. Don't ask me why

JeremyMH5909 (0)

I don't get an error but in turtle, it just makes my mouse a loading icon and says "Not Responding"

JeremyMH5909 (0)

Here is my code:

import turtle

mover = turtle.Turtle()
mover.up()
mover.shape("square")
listenner = turtle.Screen()
mover.speed(0)
mover.goto(-250, 0)

while True:
    def forward():
        mover.forward(10)

listenner.onkey(forward, "Up")


def backward():
    mover.backward(10)


listenner.onkey(backward, "Right")


def right():
    mover.right(10)


listenner.onkey(right, "Right")


def left():
    mover.left(10)


listenner.onkey(left, "Left")
listenner.listen()
UnityBird20 (6)

Hello, Im getting this error ParseError: bad input on line 14

RowanFromBJC (51)

I had to fix a few indentation errors, but in the end this works.

omarElgohary (0)

when is part 3 coming out?

Ironwolfyyy (1)

when is part 2 coming out?

CodingSquasher (0)

this worked for me!!!!

wetcoco (1)

Ehhh, i can't move it! ;-;

neelninja (1)

i cant move it around

gatorade322 (123)

what's your problem?

gatorade322 (123)

like not in real life, but what's the problem with the coding
=>

neelninja (1)

@gatorade322
well,for startes i type in all of the code and once all of thats done i press run it doesnt give any errors i just cant move around

neelninja (1)

@gatorade322
also when are you going to make part 2?

gatorade322 (123)

I did make part two, and also, maybe you could show me the code that you typed

neelninja (1)

@gatorade322
k i will send you a invite

gatorade322 (123)

@neelninja I fixed it up. It looked like there were just some indenting problems . . . (:

Fredrichcoder (0)

This is an error on pygame! This code is wrong since it is so obvious that the code is so short

gatorade322 (123)

I HAVE TO TELL SO MANY PEOPLE! THIS IS PYTHON TURTLE. NOT PYGAME. NOT TKINTER. NOT ANYTHING BUT TURTLE

Fredrichcoder (0)

@gatorade322 i tried on both still cannot though

gatorade322 (123)

it won't work if you use pygame
pygame doesn't have a turtle, or movable object.

gatorade322 (123)

people have had mistakes with this, so I'm anouncing that you use python with turtle

SaiVib (0)

I did in repl.it,and I got this error. I cannot copy and paste the error. I will put the link insted. https://repl.it/@SaiVib/Minecraft

gatorade322 (123)

@SaiVib you need to be on python with turtle

YTAyushTGD (0)

Doo you use Tk, Python or Python 2.7

gatorade322 (123)

python with turtle

CodingGoose (697)

Thanks! It's a really cool program. I learned a lot of Python Turtle, which I find interesting.

Also, when will the second part come out? I'm really hyped now!

gatorade322 (123)

the second part, I kept running in to some hard parts, but I'm almost finished (sneak peak that it's the background), and it should be done by tomorrow!

CodingGoose (697)

@gatorade322 Ok.Thanks! I can't wait! :)

Sholanki (3)

Hi, I am getting this error after writing down code:

Traceback (most recent call last):
File "main.py", line 3, in <module>
mover = turtle.Turtle()
File "/usr/local/lib/python3.8/turtle.py", line 3812, in init
Turtle._screen = Screen()
File "/usr/local/lib/python3.8/turtle.py", line 3662, in Screen
Turtle._screen = _Screen()
File "/usr/local/lib/python3.8/turtle.py", line 3678, in init
_Screen._root = self._root = _Root()
File "/usr/local/lib/python3.8/turtle.py", line 434, in init
TK.Tk.init(self)
File "/usr/local/lib/python3.8/tkinter/init.py", line 2261, in init
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

KeyboardInterrupt

Can anyone please help me with this? Sorry, I am a newbie

gatorade322 (123)

I don't think you're using python turtle right now. You are using tkinter.

YTAyushTGD (0)

@gatorade322 it doesn't work with python 2.7 or normal python for some reason

gatorade322 (123)

turtle is a package, and on repl.it, you need to get the proper repl for the package. If you were running on Linux or something, once you have installed python (sudo apt install python3 I think), you would just import turtle, and it would open a new window.

xolyon (347)

I was scared that I posted my tutorial without knowledge of this, Phew turns out they are different, nice tutorial anyway!

xolyon (347)

@gatorade322 Console thingy i am making a 4d version

gatorade322 (123)

I would love to see that! I love 4D

xolyon (347)

@gatorade322 I made it in scratch pen, but I am having an issue with mapping in python so yeah, (That's what original tutorial came out by accident)