I want to set object u1
's heading by the mouse cursors position inside the window.
import turtle
import pygame
import time
win = turtle.Screen()#window
win.title("eagle.py")
win.setup(1920,1080)
win.bgcolor("black")
win.bgpic("triangle")
c1 = turtle.Turtle()#cloud1
c1.speed(0)
c1.penup()
c1.setposition(-1075, 256)
c1.color("white")
c1.shape("triangle")
c_speed = 1 #cloudspeed
u1 = turtle.Turtle()#user1
mouse_pos = pygame.mouse.get_pos()
u1.shape("triangle")
u1.color("red")
u1.speed(0)
u1.setposition(0,0)
u1.setheading(mouse_pos)
u1.penup()
u_speed = 10 #playerspeed
def u1_r():
x = u1.xcor()
x += u_speed
u1.setx(x)
def u1_l():
x = u1.xcor()
x -= u_speed
u1.setx(x)
def u1_up():
y = u1.ycor()
y += u_speed
u1.sety(y)
def u1_down():
y = u1.ycor()
y -= u_speed
u1.sety(y)
while True:
win.update()
time.sleep(1/160)
c1.setx(c1.xcor() + c_speed)
if c1.xcor() > 1075:
c1.goto(-1075, 256)
win.listen()
win.onkeypress(u1_r, "d")
win.onkeypress(u1_l, "a")
win.onkeypress(u1_up, "w")
win.onkeypress(u1_down, "s")
The program keeps shutting down immediately after running. What have I done wrong?
The fact that you have the following:
win.listen()
win.onkeypress(u1_r, "d")
win.onkeypress(u1_l, "a")
win.onkeypress(u1_up, "w")
win.onkeypress(u1_down, "s")
inside a loop indicates that you don't have a basic understanding of the environment in which you're working. Let's start over, tossing pygame and time and just work within the turtle framework:
from turtle import Screen, Turtle
def player_r():
player.setheading(0)
player.setx(player.xcor() + player_speed)
def player_l():
player.setheading(180)
player.setx(player.xcor() - player_speed)
def player_up():
player.setheading(90)
player.sety(player.ycor() + player_speed)
def player_down():
player.setheading(270)
player.sety(player.ycor() - player_speed)
def move():
cloud.setx(cloud.xcor() + cloud_speed)
if cloud.xcor() > 940:
cloud.goto(-940, 256)
screen.update()
screen.ontimer(move)
screen = Screen()
screen.setup(1920, 1080)
screen.bgcolor('black')
screen.tracer(False)
cloud = Turtle()
cloud.shape('circle')
cloud.shapesize(1, 3)
cloud.color('white')
cloud.penup()
cloud.setposition(-940, 256)
cloud_speed = 1
player = Turtle()
player.shape('turtle')
player.color('red')
player.penup()
player_speed = 10
screen.onkeypress(player_r, 'd')
screen.onkeypress(player_l, 'a')
screen.onkeypress(player_up, 'w')
screen.onkeypress(player_down, 's')
screen.listen()
move()
screen.mainloop()
In your code, string is in heading. Instead, put variable.
u1 = turtle.Turtle()
mouse_pos = pygame.mouse.get_pos()
u1.heading(mouse_pos) # variable
Edit: This worked for me, triangle moving..
It seems pygame.init()
is need at first.
u1.setheading
has a number inside, not tuple. This is for angle. (man turtle)
import turtle
import pygame
import time
pygame.init()
win = turtle.Screen()#window
win.title("eagle.py")
win.setup(1920,1080)
win.bgcolor("black")
win.bgpic() # changed
c1 = turtle.Turtle()#cloud1
c1.speed(0)
c1.penup()
c1.setposition(-1075, 256)
c1.color("white")
c1.shape("triangle")
c_speed = 1 #cloudspeed
u1 = turtle.Turtle()#user1
mouse_pos = pygame.mouse.get_pos()
u1.shape("triangle")
u1.color("red")
u1.speed(0)
u1.setposition(0,0)
u1.setheading(0) # to be changed, setheading(int)
u1.penup()
u_speed = 10 #playerspeed
def u1_r():
x = u1.xcor()
x += u_speed
u1.setx(x)
def u1_l():
x = u1.xcor()
x -= u_speed
u1.setx(x)
def u1_up():
y = u1.ycor()
y += u_speed
u1.sety(y)
def u1_down():
y = u1.ycor()
y -= u_speed
u1.sety(y)
while True:
win.update()
time.sleep(1/160)
c1.setx(c1.xcor() + c_speed)
if c1.xcor() > 1075:
c1.goto(-1075, 256)
win.listen()
win.onkeypress(u1_r, "d")
win.onkeypress(u1_l, "a")
win.onkeypress(u1_up, "w")
win.onkeypress(u1_down, "s")
turtle.heading()
expects a numeric argument specifying the heading in degrees.
I don't think you can use the pygame
and turtle
modules at the same time. Both have their own way of drawing graphics and getting the mouse's current position, so theoretically you can use either.
Regardless, of which one you're using, you'll need to compute the heading's angle from the mouse's x, y position.
Here's how to measure the angle to the origin of an x, y position (assuming its x value isn't zero):
import math
x, y = ... # Mouse position.
print(math.degrees(math.atan(y/x)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.