简体   繁体   English

将鼠标光标位置设置为对象标题

[英]set mouse cursor position as objects heading

I want to set object u1 's heading by the mouse cursors position inside the window.我想通过窗口内的鼠标光标位置设置对象u1的标题。

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:让我们重新开始,抛开pygame时间,只在乌龟框架内工作:

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.一开始似乎需要pygame.init()

u1.setheading has a number inside, not tuple. u1.setheading里面有一个数字,而不是元组。 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. turtle.heading()需要一个数字参数,以度为单位指定航向。

I don't think you can use the pygame and turtle modules at the same time.我认为您不能同时使用pygameturtle模块。 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.无论您使用的是哪一种,您都需要根据鼠标的 x、y 位置计算航向的角度。

Here's how to measure the angle to the origin of an x, y position (assuming its x value isn't zero):以下是如何测量到 x, y 位置原点的角度(假设其 x 值不为零):

import math

x, y = ... # Mouse position.
print(math.degrees(math.atan(y/x)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM