I'm new to python and decided to practice by building a game similar to snake with the turtle library. I was able to initiate the turtle to continually move forward with a while True loop, but now I'm having trouble with getting the turtle to break this loop to make turns.
I have tried various different ways of writing my conditionals but I can't seem to figure out where the issue is. Thanks in advance!
import turtle
window = turtle.Screen()
snake = turtle.Turtle()
snake.speed(1)
snake.penup()
#Functions that move the snake:
def forward():
while True:
snake.forward(.7)
def left():
snake.left(90)
def right():
snake.right(90)
#Movement functions all put together:
def movesnake():
while True:
entry = input()
if entry == 'w':
forward()
if entry == 'a':
left()
if entry == 'd':
right()
movesnake()
Try this approach to move:
def forward():
turtle.forward(how-many-pixels-forward)
turtle.onkey(forward,'what-key-you-want-to-listen-to') #when key pressed call forward function
https://www.geeksforgeeks.org/turtle-onkey-function-in-python/
The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support.
turtle.onkey()
This function is used to bind fun to the key-release event of the key. In order to be able to register key-events, TurtleScreen must have focus.
You can try increasing the value of the forward function:
def forward():
while True:
snake.forward(5)
Take a look at this book
https://argentinaenpython.com/quiero-aprender-python/doma-de-serpientes-para-ninos_swfk-es-linux-0.0.4.pdf
Related
I am very, and by that I mean very new to Python (i know literally nothing). I'm attemtping to create a little game using the turtle module, and following a tutorial I don't see the listen() function working
here's my code
I'm trying to create a controllable character
from turtle import *
#background
Screen().bgcolor("orange")
#player
pl = Turtle()
pl.color('dodgerblue')
pl.shape('turtle')
pl.penup()
def turnleft():
player.left(30)
turtle.listen()
onkeypress(turnleft, "Left")
speed = 1
while True:
pl.forward(speed)
When you do from turtle import * it imports everything into the built-in namespace, i.e., you can then just do:
listen()
rather than
turtle.listen()
If you had just done
import turtle
then everything in the turtle package would then be accessed through the turtle namespace, i.e.,
turtle.listen()
You have plenty of mistakes here, totally normal because you are so new, that's the way to get better.
I will ""arrange"" a bit your code.
import turtle
#background
turtle.Screen().bgcolor("orange")
#player
pl = turtle.Turtle()
pl.color('dodgerblue')
pl.shape('turtle')
pl.penup()
def turnleft():
pl.left(30)
turtle.listen()
turtle.onkeypress(turnleft, "Left")
speed = 1
while True:
pl.forward(speed)
First of all i recommend you to check in google what is "OOP" and check how it works on Python.
When you use "functions" from a module (in this case turtle) , you need to call first the module and after the function for example:
turtle.onkeypress(turnleft, "Left")
# Instead
onkeypress(turnleft,"Left")
Another thing it's in your function "turnleft" you call the variable "player", but "player" doesnt exist, you want to call "pl".
Good luck with your small new projects, keep trying.
I'm trying to build a game similar to snake with the turtle library. I was able to make the turtle continually move forward with a while True loop, and also make turns without breaking the while loop.
Now I'm trying to figure out a way to exit the while loop that makes the turtle go forward in order to end the game. My aim is to allow the player to exit the loop by entering 'e' on their keyboard.
This code currently results in: AttributeError: 'Turtle' object has no attribute 'done'
def forward():
while True:
snake.forward(0.8)
if window.onkey(exit,"e"):
exit()
def left():
snake.left(90)
def right():
snake.right(90)
def back():
snake.back(0.8)
def exit():
snake.done()
#the function that actually moves the snake
def movesnake():
while True:
window.listen()
window.onkey(forward, "w")
window.onkey(left, "a")
window.onkey(right, "d")
window.onkey(back, "s")
window.mainloop()
movesnake()
If you just want the snake to stop moving, snake.done() should be turtle.done(). done is a turtle module function, not a turtle.Turtle method, so you can call it as a function, but not on the Turtle object.
I have used the onkey function for the right and left:
sc.onkey(user_left,"left")
sc.onkey(user_right,"right")
I have also set the screen after setting the turtle and importing turtle:
sc=Screen()
But when I use the same format for up and down:
sc.onkey(user_up,"up")
sc.onkey(user_down,"down")
It does nothing. I also have my functions:
def user_right:
t3.forward(5)
def user_left:
t3.backward(5)
t3 is my user turtle, and it is sideways, the shape is turtle, and its head is facing right. t3 is automatically set to make its head facing the right side when the code runs. By the way, I import from turtle import*
I see several issues here. First, this can't work as you claim:
def user_right:
t3.forward(5)
def user_left:
t3.backward(5)
It would need to be:
def user_right():
t3.forward(5)
def user_left():
t3.backward(5)
Next, this won't work in turtle in standard Python:
sc.onkey(user_left,"left")
sc.onkey(user_right,"right")
These keys would have to be "Left" and "Right". Are you using a non-standard turtle implementation? (E.g. the one on repl.it) You show your two event handlers that work, but fail to show the two that don't which would be of more interest when trying to debug your code.
Finally, your missing a call to the screen's listen() method, so your keystrokes are ignored. Here's how I might implement the functionality your code is hinting at:
from turtle import Screen, Turtle
def user_right():
turtle.forward(5)
def user_left():
turtle.backward(5)
def user_up():
turtle.sety(turtle.ycor() + 5)
def user_down():
turtle.sety(turtle.ycor() - 5)
turtle = Turtle()
turtle.shape('turtle')
screen = Screen()
screen.onkey(user_left, 'Left')
screen.onkey(user_right, 'Right')
screen.onkey(user_up, 'Up')
screen.onkey(user_down, 'Down')
screen.listen()
screen.mainloop()
I'm making a game where you control a space ship, and currently, its movement is very hard to look at, because it's quite janky. Is there a command to make the movement smoother and more natural? I'm using
python with turtle on repl.it, and onrelease doesn't seem to work. Any answer is appreciated!
import turtle
you = turtle.Turtle()
keycoms = turtle.Screen()
you.penup()
def w():
you.fd(5)
def s():
you.bk(3)
def a():
you.lt(5)
def d():
you.rt(5)
keycoms.onkey(w,"w")
keycoms.onkey(s,"s")
keycoms.onkey(a,"a")
keycoms.onkey(d,"d")
keycoms.listen()
One way this is often solved is to make more, smaller movements, with a slight time delay between each one. This gives the illusion of flowing movement rather than a jump - something like the following (there may be a better way to actually code it, but this gives you an idea):
def w():
for _ in range(5):
you.fd(1)
sleep(0.1)
Simply change your onkey(...) to onkeypress(...):
import turtle
you = turtle.Turtle()
keycoms = turtle.Screen()
you.penup()
def w():
you.fd(5)
def s():
you.bk(3)
def a():
you.lt(5)
def d():
you.rt(5)
keycoms.onkeypress(w,"w")
keycoms.onkeypress(s,"s")
keycoms.onkeypress(a,"a")
keycoms.onkeypress(d,"d")
keycoms.listen()
keycoms.mainloop()
The Scrren.onkey() method will allow the user to press down on a specific key for as long as the user wants, and the function command to the assigned action will only execute once.
Screen.onkeypress(), on the other hand, will execute the command as many times as the user holds down the key.
I am creating a game in python using turtle but I am unable to control the speed of the turtle in the loop as the speed of the turtle is 0. It is supposed to run like flash but it is running at normal speed
import turtle
c=turtle.Screen()
a=turtle.Turtle()
a.speed(0)
b=True
def ch( a , d):
global b
b = False
while b:
a.fd(1)
c.onclick(ch)
c.mainloop()
speed(0) can only speed up the animations a bit.
Try using c.tracer(0, 0)
This fully disables all animations, and should result it a bit more of a speed up. Although, to refresh the screen you'll need to call c.update()
First, your code is structured incorrectly. You don't need to call onclick() in a loop, it simply sets a handler function so only needs to be called once. Also, mainloop() should be running the events, not called after the events are over.
I don't believe you're going get any more speed out of this code unless you increase the forward distance. Simply increading to fd(3) will make a noticeable differece. My rework of your code:
from turtle import Turtle, Screen
def click_handler(x, y):
global flag
flag = False
def turtle_forward():
if flag:
turtle.forward(3)
screen.ontimer(turtle_forward, 0)
flag = True
screen = Screen()
screen.onclick(click_handler)
turtle = Turtle()
turtle.speed('fastest')
turtle_forward()
screen.mainloop()