简体   繁体   English

嗨,我是编码新手,我正在学习 python 乌龟,我正在制作这款钢琴拼图游戏,但我无法真正让鼠标点击工作

[英]Hi am new at coding and I am learning python turtle , and I am making this piano tiles game like but I cant really get the mouse click to work

I just want the tiles to move when they are being clicked, so when are coming down you should be able to click them and they will just move back up so they should just change position when they are clicked that's all.我只是希望瓷砖在被点击时移动,所以当它们下降时你应该能够点击它们并且它们只会向上移动所以它们应该在被点击时改变 position 就是这样。 Here's my code, its probably really messed up but I tried my best and honestly right now I am just trying to fill the question because "Your question couldn't be submitted" this is my first time using stackoverflow honeslty.这是我的代码,它可能真的搞砸了,但我尽了最大努力,老实说现在我只是想填写这个问题,因为“你的问题无法提交”这是我第一次使用 stackoverflow honeslty。

import turtle
import random


list1 = [900, 1000, 1100, 1300, 1400,1500,1600,1700,1800]
spawn1 = (random.choice(list1))
spawn2 = (random.choice(list1))
spawn3 = (random.choice(list1))
spawn4 = (random.choice(list1))

wn = turtle.Screen()
wn.setup(500,800)
wn.title("Magic Tiles")
wn.bgcolor("black")
wn.tracer(0)


tile1 = turtle.Turtle()
tile1.speed(2)
tile1.shape("square")
tile1.shapesize(10,5)
tile1.color("white")
tile1.penup()
tile1.goto(-185,spawn1)
tile1.dy = -0.5

tile2 = turtle.Turtle()
tile2.speed(2)
tile2.shape("square")
tile2.shapesize(10,5)
tile2.color("white")
tile2.penup()
tile2.goto(-65,spawn2)
tile2.dy = -0.5

tile3 = turtle.Turtle()
tile3.speed(2)
tile3.shape("square")
tile3.shapesize(10,5)
tile3.color("white")
tile3.penup()
tile3.goto(56,spawn3)
tile3.dy = -0.5

tile4 = turtle.Turtle()
tile4.speed(2)
tile4.shape("square")
tile4.shapesize(10,5)
tile4.color("white")
tile4.penup()
tile4.goto(175,spawn4)
tile4.dy = -0.5

while True:
    wn.update()

    tile1.sety(tile1.ycor() + tile1.dy)
    tile2.sety(tile2.ycor() + tile2.dy)
    tile3.sety(tile3.ycor() + tile3.dy)
    tile4.sety(tile4.ycor() + tile4.dy)


    if tile1.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0
    if tile2.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0
    if tile3.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0
    if tile4.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0

You have to use turtle.onclick(function) to execute function when you click turtle点击turtle时必须使用turtle.onclick(function)执行function

def move_back_1(x, y):  # `on_click` will run it with two values
    tile1.sety(random.choice(list1))

tile1 = turtle.Turtle()
# ... code ...
tile1.onclick(move_back_1)  # function's name without `()`

Full working code完整的工作代码

import turtle
import random


list1 = [900, 1000, 1100, 1300, 1400,1500,1600,1700,1800]
spawn1 = (random.choice(list1))
spawn2 = (random.choice(list1))
spawn3 = (random.choice(list1))
spawn4 = (random.choice(list1))

wn = turtle.Screen()
wn.setup(500,800)
wn.title("Magic Tiles")
wn.bgcolor("black")
wn.tracer(0)

def move_back_1(x, y):
    tile1.sety(random.choice(list1))

tile1 = turtle.Turtle()
tile1.speed(2)
tile1.shape("square")
tile1.shapesize(10,5)
tile1.color("white")
tile1.penup()
tile1.goto(-185,spawn1)
tile1.dy = -0.5
tile1.onclick(move_back_1)

def move_back_2(x, y):
    tile2.sety(random.choice(list1))

tile2 = turtle.Turtle()
tile2.speed(2)
tile2.shape("square")
tile2.shapesize(10,5)
tile2.color("white")
tile2.penup()
tile2.goto(-65,spawn2)
tile2.dy = -0.5
tile2.onclick(move_back_2)

def move_back_3(x, y):
    tile3.sety(random.choice(list1))

tile3 = turtle.Turtle()
tile3.speed(2)
tile3.shape("square")
tile3.shapesize(10,5)
tile3.color("white")
tile3.penup()
tile3.goto(56,spawn3)
tile3.dy = -0.5
tile3.onclick(move_back_3)

def move_back_4(x, y):
    tile4.sety(random.choice(list1))

tile4 = turtle.Turtle()
tile4.speed(2)
tile4.shape("square")
tile4.shapesize(10,5)
tile4.color("white")
tile4.penup()
tile4.goto(175,spawn4)
tile4.dy = -0.5
tile4.onclick(move_back_4)

    
while True:
    wn.update()

    tile1.sety(tile1.ycor() + tile1.dy)
    tile2.sety(tile2.ycor() + tile2.dy)
    tile3.sety(tile3.ycor() + tile3.dy)
    tile4.sety(tile4.ycor() + tile4.dy)


    if tile1.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0
    if tile2.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0
    if tile3.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0
    if tile4.ycor() < -250:
        tile1.dy = 0
        tile2.dy = 0
        tile3.dy = 0
        tile4.dy = 0

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

相关问题 嗨,我是 Python 的新手,我正在编写这个有问题的程序 - Hi I am new to Python and I was coding this program that I have a problem with 我在Mac Mojave上,正尝试将python乌龟游戏转换为exe应用,但遇到意外错误 - I am on Mac Mojave and am trying to convert a python turtle game into an exe app, but am getting an unexpected error 嗨,我正在 python 中试验 selenium - Hi I am experimenting with selenium in python 大家好,我是机器学习的新手,正在尝试制作我的第一个神经网络 - hi, everyone I am new to machine learning and was trying to make my first neural network 我正在使用 Python 制作井字游戏 - I am making a Tic-Tac-Toe game using Python 我是编码新手,无法将数据导入 python - I'm new to coding and am having trouble with importing data into python 我正在制作一个合上盒子的游戏 - I am making a shut the box game 我正在做乒乓球比赛,但球出现故障 - I am making a pong game but the ball is malfunctioning 我正在为我的 python 类制作一个游戏,但我无法让它运行 - I am making a game for my python class and I cannot get it to run 嗨,我是 python 的新手,并试图将列表转换为字典,以便我可以获得按摩的价值并在某处使用它 - Hi, I am new to python and trying to convert a list into a Dictionary so I can get the value of the massage and use it somewhere
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM