So I am making a game with python turtle where the player moves the turtle left and right by pressing the corresponding arrow keys. The turtle cannot move up or down in any way. Only left and right. But when my turtle reaches a certain xcor value I want the turtle to stop moving even if I am still pressing that arrow key. But still be able to move the opposite direction with the other arrow key.
def playerRight():
player.goto(player.xcor() + 8,player.ycor())
if player.xcor() >= 200:
def playerLeft():
player.goto(player.xcor() - 8,player.ycor())
if player.xcor() <= -200:
screen.onkey(playerRight,'Right')
screen.onkey(playerLeft,'Left')
screen.listen()
But I have no clue what to put in my conditionals. A reply is greatly appreciated! Thanks!
Perhaps try changing your code to only move the turtle if it won't go too far, like so.
def playerRight():
if player.xcor() <= 192:
player.goto(player.xcor() + 8,player.ycor())
Now it only moves to the right if it doing so won't make it go to far. You then do the same thing for the playerLeft() function
You also have your inequalities the wrong way around (< where you need >)
Related
for count in range(15):
turtle.forward(random.randint(0, 100))
turtle.right(random.randint(0, 90))
turtle.forward(random.randint(0, 50))
turtle.circle(random.randint(0, 50))
if turtle.pos() == turtle.pos():
turtle.color("red")
turtle.speed(0)
#turtle.penup()
#turtle.stamp()
#turtle.hideturtle()
*this code doesnt look right but I am a beginner myself my friend insists it makes the turtle turn red on collision. But how
does
> the if statement differentiate between 1 turtle and another? they ARE
> all red at the end. How is that happening?
not sure how to use Pycharm to track which turtle is being asked about in the if statement
Thanks in Advance I am learning fast.
Do you have a second turtle? if your other turtle is called Turtle2, then it should look like this
if turtle.pos() == turtle2.pos():
turtle.color("red")
turtle.speed(0)
#turtle.penup()
#turtle.stamp()
#turtle.hideturtle()
right now you are looking to see if a turtle's position is the same as a turtle's position (the same values). It will always be true.
I am using pygame 1.9.6 and python 3.7.7. I am looking for a way to have something drawn by pygame(or maybe even an image) that stays inside the window border(and stops when I release the key. I started with the “if keys[]:” method:
if keys[pygame.K_LEFT] and x>0:
x-=speed
that made it stay in bounds, but it only moved a few pixels and then did not repeat.
Next I tried the “event.key” method:
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
xchange=-speed
ychange=-0
but that just makes the object move forever. I tried putting “and x>0” on the same line right after the direction, an “if x>0:” before, an “if x<0:” after, but the event never updates to see that the coordinates are past the edge, even with update commands. It just keeps going and going. I also don’t know how to make the object stop moving when I release the key, since event.key’s have the event always on.
Thank you for all the help you can offer.
In order for something to stay within a frame, you must check if it's coordinates are not past the border before moving it.
keys = pygame.key.get_pressed()
if(keys[K_LEFT]):
if(square.left > 0):
square.left -= 10
if(keys[K_RIGHT]):
if(square.right < 600):
square.left += 10
if(keys[K_UP]):
if(square.top > 0):
square.top -= 10
if(keys[K_DOWN]):
if(square.bottom < 600):
square.bottom += 10
This is an example of how you would do this with a Rect object, it checks for border and then moves the object if its within those border and the appropriate key is being pressed. I can't apply this to your code as I cannot see enough of it but you just have to replace the coordinates. For example, you might have thing.x instead of square.left
I'm not sure how to explain, but I'm trying to make game where player can walk on window and meet enemies etc.
I have code with one window. Like this:
window = pygame.display.set_mode((500, 480))
And my character can move left, right and jump on this window, but when he gets to the corner he stops. So my question is how to make him move further and, for example, change background, meet another enemies etc. when he walks?
Do I have to make a class and call her when character touch corner maybe?
I hope you understand.
Thanks.
You need to understand when your character touch / is close enough to the border of the screen and then take appropriate action.
In a game I'm writing for fun, the class representing the main character has a method to check when the character is inside a surface sface. I copy-paste its skeleton here to show an example:
def insidesurf(self, sface):
if sface.get_rect().contains(self.rect):
return None
else:
if self.rect.top < sface.get_rect().top:
#the character is going out from the screen from the top-side
elif self.rect.left < sface.get_rect().left:
#the character is going out from the screen from the left-side
elif self.rect.bottom > sface.get_rect().bottom:
#the character is going out from the screen from the bottom-side
elif self.rect.right > sface.get_rect().right:
#the character is going out from the screen from the right-side
This method is called at each iteration of the main loop after the character is moved (after a key press). The argument sface is your window.
What to do exactly in the various if statements is up to you. In my case, it redraws the screen replacing the character at the opposite side of the screen. If the character is exiting from left side, it's redrawn at right side, to give the impression that the camera is showing the next room and the character is at the same position.
You may also wish to not pass your window but a smaller surface in order to scroll the scenario instead of redrawing it all.
I've just started working on a version of Snake using Turtle, and I've encountered an issue. I want the snake to move indefinitely, but also to allow the user to move the snake with the keyboard. I got the snake to move from user input, but I can't figure out how to get the snake to keep moving in the same direction while there is no input, whilst preventing it from ignoring user input:
while True:
win.onkey(up,"Up")
win.onkey(right,"Right")
win.onkey(down,"Down")
win.onkey(left,"Left")
win.listen()
#moves the snake one unit in the same direction it is currently facing
movesnake()
I'm new to Turtle, and this is my guess at how to solve this issue - which obviously doesn't work. Any help would be appreciated. I'm conscious Pygame might make this easier but since I've already started with Turtle, I would prefer to get a Turtle solution, if possible.
An event-driven environment like turtle should never have while True: as it potentially blocks out events (e.g. keyboard). Use an ontimer() event instead.
Generally, onkey() and listen() don't belong in a loop -- for most programs they only need to be called once.
Here's a skeletal example of an autonomous turtle being redirected by user input:
from turtle import Screen, Turtle
def right():
snake.setheading(0)
def up():
snake.setheading(90)
def left():
snake.setheading(180)
def down():
snake.setheading(270)
def movesnake():
snake.forward(1)
screen.ontimer(movesnake, 100)
snake = Turtle("turtle")
screen = Screen()
screen.onkey(right, "Right")
screen.onkey(up, "Up")
screen.onkey(left, "Left")
screen.onkey(down, "Down")
screen.listen()
movesnake()
screen.mainloop()
I'm working on Python game using turtles.
I have a player object that moves up and down (jump) on key press. I'm trying to add a moving platform that the player has to jump on.
I tried putting the moving platform in a while loop. The problem is since the while loop is running to keep the platform moving, the program does not detect key press.
I tried moving turtle.listen() inside the main while loop but that didn't work.
How do I keep the platform moving, in a while True loop, and have the listener active?
# moving platform
while True:
s13.backward(3)
if s13.xcor() > 250:
s13.setheading(0)
if s13.xcor() < -200:
s13.setheading(180)
...
turtle.listen()
turtle.onkey(jump, "Up")
Any advice is appreciated...
A while True: statement has no place in an event driven environment like turtle. There are at least a couple of solutions available to you. The most straight forward is to use turtle's built-in ontimer() events to have a function independently run at a fixed (or variable) interval.
Another option is to introduce threading to the program. However, since turtle is tkinter-based, you have to channel all the graphics operations through the main thread, which complicates things.
Try searching StackOverflow for:
python turtle ontimer
python turtle threading
A crude example:
from turtle import Turtle, Screen
screen = Screen()
s13 = Turtle('square')
s13.color('red', 'blue')
s13.shapesize(1, 3, 2)
s13.penup()
def jump():
s13.color(*reversed(s13.color()))
# moving platform
def move_platform():
s13.backward(3)
if s13.xcor() > 250:
s13.setheading(0)
if s13.xcor() < -200:
s13.setheading(180)
screen.ontimer(move_platform, 100)
# ...
screen.onkey(jump, "Up")
screen.listen()
move_platform()
screen.mainloop()
The platform floats back and forth. If you hit the up arrow (after clicking on the window to make it active) you'll see the platform swap its fill and outline colors as it floats.
You need to move:
turtle.listen()
turtle.onkey(jump, "Up")
to the top. Like this:
turtle.listen()
turtle.onkey(jump, "Up")
while True:
s13.backward(3)
if s13.xcor() > 250:
s13.setheading(0)
if s13.xcor() < -200:
s13.setheading(180)
...