[英]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.