How to Make 2D Minecraft in Python - Part One: Moving the Character Around
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
Hello, Im getting this error ParseError: bad input on line 14
did u fix @UnityBird20
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
I don't think you're using python turtle right now. You are using tkinter.
@gatorade322 it doesn't work with python 2.7 or normal python for some reason
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.
Ehhh, i can't move it! ;-;
I was scared that I posted my tutorial without knowledge of this, Phew turns out they are different, nice tutorial anyway!
@xolyon what is your tutorial
@gatorade322 Console thingy i am making a 4d version
I would love to see that! I love 4D
@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)
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 .
Edit: This doesn't work anymore on Python turtle graphics. Don't ask me why
I don't get an error but in turtle, it just makes my mouse a loading icon and says "Not Responding"
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()
I had to fix a few indentation errors, but in the end this works.
when is part 3 coming out?
when is part 2 coming out?
this worked for me!!!!
nice
i cant move it around
what's your problem?
like not in real life, but what's the problem with the coding
=>
@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
@gatorade322
also when are you going to make part 2?
I did make part two, and also, maybe you could show me the code that you typed
@gatorade322
k i will send you a invite
@neelninja I fixed it up. It looked like there were just some indenting problems . . . (:
This is an error on pygame! This code is wrong since it is so obvious that the code is so short
I HAVE TO TELL SO MANY PEOPLE! THIS IS PYTHON TURTLE. NOT PYGAME. NOT TKINTER. NOT ANYTHING BUT TURTLE
@gatorade322 i tried on both still cannot though
@Fredrichcoder did you use python turtle?
@gatorade322 i use pygame
it won't work if you use pygame
pygame doesn't have a turtle, or movable object.
people have had mistakes with this, so I'm anouncing that you use python with turtle
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
@SaiVib you need to be on python with turtle
Doo you use Tk, Python or Python 2.7
python with turtle
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!
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!
I just posted it. Here is the link:
https://repl.it/talk/learn/2D-Minecraft-Part-Two-The-background/33934
@gatorade322 Ok.Thanks! I can't wait! :)
nice
@HEROBRATT thanks
Cool!
thanks
Your welcome @gatorade322
For some reason I say thank you a lot. Is that a good thing?
@gatorade322 Always :)
uh can't move
@KirroYT did u fix