Skip to content
Sign upLog in
← Back to Community

How to Make 2D Minecraft in Python - Part One: Moving the Character Around

Profile icon
gatorade322

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

Voters
Profile icon
gatorade322
Profile icon
RexGupta
Profile icon
Bigshayu
Profile icon
YahyaYasir
Profile icon
divyansh042009
Profile icon
zaeemfirdaus
Profile icon
EmranZIRAI
Profile icon
Edwin8557
Profile icon
IsaacSands
Profile icon
DESIMONELOCUS
Comments
hotnewtop
Profile icon
Sholanki

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

Traceback (most recent call last):
File "main.py", line 3, in
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

Profile icon
gatorade322

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

Profile icon
YTAyushTGD

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

Profile icon
gatorade322

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.

Profile icon
KirroYT

uh can't move

Profile icon
JeremyMH5909

@KirroYT
did u fix

Profile icon
UnityBird20

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

Profile icon
JeremyMH5909

did u fix

@UnityBird20

Profile icon
wetcoco

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

Profile icon
xolyon

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

Profile icon
gatorade322

@xolyon
what is your tutorial

Profile icon
xolyon

@gatorade322
Console thingy i am making a 4d version

Profile icon
gatorade322

I would love to see that! I love 4D

Profile icon
xolyon

@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)

Profile icon
gatorade322

oh.

Profile icon
uluhtC

I keep getting this error after I run the code. How can I fix this?

Profile icon
uluhtC

@uluhtC
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()

Profile icon
uluhtC

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()

Profile icon
JATKitty

woooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooow

Profile icon
SnowChristal

nice

Profile icon
multicoder2021

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 .

Profile icon
gatorade322

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

Profile icon
JeremyMH5909

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

Profile icon
JeremyMH5909

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()
Profile icon
RowanFromBJC

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

Profile icon
Doubli

when is part 3 coming out?

Profile icon
Ironwolfyyy

when is part 2 coming out?

Profile icon
CodingSquasher

this worked for me!!!!

Profile icon
Leroy01010

nice

Profile icon
neelninja

i cant move it around

Profile icon
gatorade322

what's your problem?

Profile icon
gatorade322

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

Profile icon
neelninja

@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

Profile icon
neelninja

@gatorade322

also when are you going to make part 2?

Profile icon
gatorade322

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

Profile icon
neelninja

@gatorade322

k i will send you a invite

Profile icon
gatorade322

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

Profile icon
Fredrichcoder

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

Profile icon
gatorade322

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

Profile icon
Fredrichcoder

@gatorade322
i tried on both still cannot though

Profile icon
gatorade322

@Fredrichcoder
did you use python turtle?

Profile icon
Fredrichcoder

@gatorade322
i use pygame

Profile icon
gatorade322

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

Profile icon
gatorade322

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

Profile icon
SaiVib

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

Profile icon
gatorade322

@SaiVib
you need to be on python with turtle

Profile icon
YTAyushTGD

Doo you use Tk, Python or Python 2.7

Profile icon
gatorade322

python with turtle

Profile icon
CodingGoose

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!

Profile icon
gatorade322

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!

Profile icon
gatorade322
Profile icon
CodingGoose

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

Profile icon
Ironwolfyyy

nice

Profile icon
gatorade322

@HEROBRATT
thanks

Profile icon
Codemonkey51

Cool!

Profile icon
gatorade322

thanks

Profile icon
Codemonkey51

Your welcome

@gatorade322

Profile icon
gatorade322

For some reason I say thank you a lot. Is that a good thing?

Profile icon
CodingGoose

@gatorade322
Always :)

Profile icon
gatorade322

OK!