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

时间:2019-12-24 10:50:08

标签: python python-3.x pygame mouse turtle-graphics

我想通过鼠标光标在窗口内的位置来设置对象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")

程序运行后立即保持关闭状态。我做错了什么?

3 个答案:

答案 0 :(得分:2)

您具有以下事实:

import os
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from multiprocessing import Process


def startTensorboard(logdir):
    # Start tensorboard with system call
    os.system("tensorboard --logdir {}".format(logdir))


def fitModel():
    # Create your model
    model = Sequential()
    model.add(Dense(32, activation='relu', input_dim=100))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    # Some mock training data
    data = np.random.random((1000, 100))
    labels = np.random.randint(2, size=(1000, 1))

    # Run the fit function
    model.fit(data, labels, epochs=100, batch_size=32)


if __name__ == '__main__':
    # Run both processes simultaneously
    Process(target=startTensorboard, args=("logs",)).start()
    Process(target=fitModel).start()
循环内的

表示您对工作环境没有基本的了解。让我们重新开始,扔 pygame 时间,然后在 turtle 框架内工作:

win.listen()
win.onkeypress(u1_r, "d")
win.onkeypress(u1_l, "a")
win.onkeypress(u1_up, "w")
win.onkeypress(u1_down, "s")

答案 1 :(得分:1)

在您的代码中,字符串位于标题中。而是放置变量。

u1 = turtle.Turtle()
mouse_pos = pygame.mouse.get_pos()
u1.heading(mouse_pos)  # variable

编辑:这对我有用,三角形移动。

似乎一开始需要pygame.init()

u1.setheading里面有一个数字,而不是元组。这是角度。 (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")

答案 2 :(得分:1)

turtle.heading()需要一个以度为单位的数字参数。

我认为您不能同时使用pygameturtle模块。两者都有自己的图形绘制方式和获取鼠标的当前位置,因此从理论上讲,您都可以使用。

无论使用哪种,都需要从鼠标的x,y位置计算出航向角。

以下是测量与x,y位置的原点的角度的方法(假设其x值不为零):

import math

x, y = ... # Mouse position.
print(math.degrees(math.atan(y/x)))
相关问题