簡體   English   中英

當我的烏龜撞到精靈時,如何讓我的游戲重置?

[英]How do I make my game reset when my turtle hits an sprite?

標題解釋了我想用這個程序做什么,當我的烏龜撞到一個圓圈時,游戲重置/結束

這是宇宙飛船(你的角色)的代碼:

move = turtle.Turtle()

showturtle()
turtle.hideturtle()
move.setposition(-500,0)
move.pencolor('cyan')
move.fillcolor("blue")
move.penup()
move.speed()
move.shapesize(3,3,3)



turtle.fillcolor("blue")
turtle.shapesize(3,3,3)
outline = ['white', 'green', 'red', 'blue', 'purple', 'yellow', 'orange']
colors = ['red', 'blue', 'green', 'purple', 'yellow', 'orange', 'black']


def up():
   move.forward(25)

def down():
   move.backward(15)

def left():
    move.left(30)


def right():
    move.right(30)
 


def clickleft(x,y):
    move.fillcolor(random.choice(colors))

def clickright(x,y):
    move.pencolor(random.choice(outline))


    
turtle.listen()

turtle.onscreenclick(clickleft, 1)
turtle.onscreenclick(clickright, 3)

turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')

這是小行星(當移動命中時,游戲重置的精靈):

asteroid_prototype = Turtle()
asteroid_prototype.hideturtle()
asteroid_prototype.color('grey')
asteroid_prototype.shape('circle')
asteroid_prototype.shapesize(ASTEROID_RADIUS / CURSOR_SIZE)
asteroid_prototype.speed('fastest')  # because 15 isn't a valid argument
asteroid_prototype.penup()

asteroids = []

for _ in range(NUMBER_ASTEROIDS):
    asteroid = asteroid_prototype.clone()
    asteroid.setposition( \
        randint(ASTEROID_RADIUS - WIDTH, WIDTH - ASTEROID_RADIUS), \
        randint(ASTEROID_RADIUS - HEIGHT, HEIGHT - ASTEROID_RADIUS) \
    )

    while any(map((lambda a: lambda b: a.distance(b) < ASTEROID_RADIUS)(asteroid), asteroids)):
        asteroid.setposition( \
            randint(ASTEROID_RADIUS - WIDTH, WIDTH - ASTEROID_RADIUS), \
            randint(ASTEROID_RADIUS - HEIGHT, HEIGHT - ASTEROID_RADIUS) \
        )

    asteroid.showturtle()
    asteroids.append(asteroid)

我需要它,所以如果移動命中 asteroid_prototype,游戲會重置或至少結束。 先感謝您

您可以嘗試制作 function 將所有內容移動到其起始位置(我將其命名為 startPos)。 我會把海龜的所有位置,將所有分數重置為 0,並清空這個 function 中的所有敵人列表等。 然后,當 asteroid_prototype 和你的海龜在同一個 position 時調用它。 例如,在你的主循環中

for i in range(asteroids):
    if i.xcor == turtle.xcor and i.ycor == turtle.ycor:
        startPos()

if i.xcor > turtle.xcor - 5 and i.xcor < turtle.xcor + 5 and i.ycor > turtle.ycor - 5 and i.ycor < turtle.ycor + 5您總是可以稍微更改參數,這樣它們就不必完全相同 position if i.xcor > turtle.xcor - 5 and i.xcor < turtle.xcor + 5 and i.ycor > turtle.ycor - 5 and i.ycor < turtle.ycor + 5或類似的東西。 您可能希望將參數設置為海龜/小行星半徑的大小(我選擇了 5 作為任意數字)。

我希望這會有所幫助,並且寫得足夠清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM