盒子克隆不会像原始克隆一样向下移动

时间:2019-09-21 21:08:18

标签: python turtle-graphics

我编写了一个代码,该代码生成了几个随机的红色框,它们应该向下移动,然后蓝色框(播放器)需要使用箭头键躲避它们。我制作了这些克隆,但它们不会像原始的红色框那样掉下来,只有一个掉下来的盒子可能是原始盒子。

这是代码:

from turtle import Screen, Turtle
import turtle
import random
import time
import sys
width = 800
height = 600
p_x = 0 #player x
p_y = 0 #player y

e_x = 0 #enemy x
e_y = 0 #enemy y
e_s = 2 #enemy speed

wn = turtle.Screen()
wn.bgcolor('Black')
wn.setup(width, height)

#player
p = Turtle()
p.penup()
p.shape('square')
p.color('blue')
p.penup()

#enemy
e = Turtle()
e.penup()
e.goto (0, 400)
e.pendown()
e.shape('square')
e.color('red')
e.penup()

#pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)

alive = True

#main loop

while alive == True:
    #player movment
    def p_up():
        check_collision()
        p_y = p.ycor()
        p_y += 20
        p.sety(p_y)

    def p_down():
       check_collision()
       p_y = p.ycor()
       p_y -= 20
       p.sety(p_y)

    def p_right():
        check_collision()
        p_x = p.xcor()
        p_x += 20
        p.setx(p_x)

    def p_left():
        check_collision()
        p_x = p.xcor()
        p_x -= 20
        p.setx(p_x)

    #keyboard bindind
    p.penup()
    wn.listen()
    wn.onkeypress(p_up, 'Up')
    wn.onkeypress(p_down, 'Down')
    wn.onkeypress(p_right, 'Right')
    wn.onkeypress(p_left, 'Left')

    #end game
    def end():
            wn.bgcolor('white')
            alive = False
            pen.write("you touched a box", align="center", font=("Courier", 24, "normal"))
            alive = False

    #collision
    def check_collision():
        if p.distance(e) < 20:
            end()

    #bringing enemy to the top, and then to a random spot on the y axis 
    for i in range (1, 5):
        e.clone()
        e.hideturtle()
        e_x = random.randint(-300, 300)
        e_y = 300
        print(random)
        e.goto(e_x, e_y)
        e.showturtle()

    #ememy moving down
    for i in range (1, 300):
        check_collision()
        e_y = e.ycor()
        e_y -= e_s
        e.sety(e_y)

    e.hideturtle()

while True:
    wn.update()

1 个答案:

答案 0 :(得分:0)

您的代码一团糟,因此您的问题没有简单的解决方法。具体来说:

您尝试创建多个敌人,但是您的check_collision()函数和运动代码仅设计用于处理单个敌人。

您执行了e.clone(),但是无法保存结果,因此只会使屏幕乱七八糟。乌龟实际上是不会被垃圾收集的全局实体,因此与其克隆新的,不如重用旧的。

while循环看起来很奇怪:

while alive == True:
    # ...

while True:
    wn.update()

特别是,除非限制update(),否则tracer()并没有多大意义。因此,第二个循环实际上什么也没做。

在像乌龟这样的事件驱动世界中,无限循环或游戏时长没有意义。使用screen.ontimer()更有意义。

这是我对您的代码的重写,我相信它可以满足您的要求:

from turtle import Screen, Turtle
from random import randint

WIDTH, HEIGHT = 800, 600
FONT = ("Courier", 24, "normal")

# end game
def end():
    global alive

    screen.bgcolor('white')
    pen.write("You touched a box!", align="center", font=FONT)
    alive = False

    screen.update()

# collision
def check_collision():
    screen.update()

    for enemy in enemies:
        if player.distance(enemy) < 20:
            end()
            break

# player movement
def p_up():
    player.sety(player.ycor() + 20)
    check_collision()

def p_down():
    player.sety(player.ycor() - 20)
    check_collision()

def p_right():
    player.setx(player.xcor() + 20)
    check_collision()

def p_left():
    player.setx(player.xcor() - 20)
    check_collision()

# main loop
def move():

    for enemy in enemies:
        if enemy.ycor() < -300:
            enemy.hideturtle()
            enemy.goto(randint(-400, 400), 300)
            enemy.velocity = randint(4, 8)
            enemy.showturtle()
        else:
            enemy.sety(enemy.ycor() - enemy.velocity)

        check_collision()

        if not alive:
            break

    if alive:
        screen.ontimer(move, 50)

alive = True

screen = Screen()
screen.bgcolor('Black')
screen.setup(WIDTH, HEIGHT)
screen.tracer(False)

# player
player = Turtle()
player.shape('square')
player.color('blue')
player.penup()

# enemy
enemy_prototype = Turtle()
enemy_prototype.hideturtle()
enemy_prototype.shape('square')
enemy_prototype.color('red')
enemy_prototype.penup()

enemies = []

for _ in range(3):
    enemy = enemy_prototype.clone()
    enemy.goto(randint(-400, 400), 300)
    enemy.velocity = randint(4, 8)
    enemy.showturtle()
    enemies.append(enemy)

# pen
pen = Turtle()
pen.hideturtle()
pen.penup()
pen.sety(260)

# keyboard bindind
screen.onkeypress(p_up, 'Up')
screen.onkeypress(p_down, 'Down')
screen.onkeypress(p_right, 'Right')
screen.onkeypress(p_left, 'Left')
screen.listen()

move()

screen.mainloop()