Python 3.x 为什么我的乌龟子弹在移动时会留下两条线?
在释放w键之前,程序运行正常。w键对应的函数是shoot_key(),它将子弹海龟的位置设置为用户海龟(player1)的位置,还设置笔并为子弹运行showturl()函数,使其可见。然后,它使用on_定时器每隔50毫秒运行嵌套在shoot_key()中的shot函数,从而将bullet向前设置为5像素,同时将checkbullet()函数作为3个函数分别运行,每个函数将bullet作为一个参数,另一个海龟作为另一个参数。当checkbullet函数运行时,如果任何海龟离子弹足够近,足以与子弹相撞,它们就会消失。当子弹和另一只乌龟与墙壁相撞时,bullet_end()函数启动,当运行时隐藏子弹并向用户提供ON键输入,以便用户可以继续在屏幕上移动播放器,甚至按下释放w键,从而重复该过程并发射子弹乌龟。然而,当我运行射击功能时,一条线跟随子弹龟,尽管我把笔放在上面,一条线也是随机线,来自玩家龟,也是随机方向和随机长度,我似乎不知道为什么Python 3.x 为什么我的乌龟子弹在移动时会留下两条线?,python-3.x,turtle-graphics,Python 3.x,Turtle Graphics,在释放w键之前,程序运行正常。w键对应的函数是shoot_key(),它将子弹海龟的位置设置为用户海龟(player1)的位置,还设置笔并为子弹运行showturl()函数,使其可见。然后,它使用on_定时器每隔50毫秒运行嵌套在shoot_key()中的shot函数,从而将bullet向前设置为5像素,同时将checkbullet()函数作为3个函数分别运行,每个函数将bullet作为一个参数,另一个海龟作为另一个参数。当checkbullet函数运行时,如果任何海龟离子弹足够近,足以与子弹相
from turtle import Screen, Turtle
import random
from random import randint
#screen setup
screen = Screen()
screen.setup(width=450, height=450)
screen.bgcolor('green')
screen.tracer(3)
#user
player = Turtle()
player.shape('square')
player.penup()
#First enemy
player2 = Turtle()
player2.shape('turtle')
player2.penup()
player2.setpos(random.randint(-200,200), random.randint(-200,200))
player2.setheading(random.randint(1,360))
#Second enemy
player3 = Turtle()
player3.shape('square')
player3.penup()
player3.setpos(random.randint(-200,200), random.randint(-200,200))
player3.setheading(random.randint(1,360))
#third enemy
player4 = Turtle()
player4.shape('triangle')
player4.penup()
player4.setpos(random.randint(-200,200), random.randint(-200,200))
player4.setheading(random.randint(1,360))
###4th enemy
##player5 = Turtle()
##player5.shape('turtle')
##player5.penup()
##player5.setpos(random.randint(-200,200), random.randint(-200,200))
#bullet
bullet = Turtle()
bullet.shape("turtle")
bullet.hideturtle()
bullet.penup
bullet.setpos(random.randint(-200,200),random.randint(-200,200))
bullet.hideturtle()
bullet.penup
px = 0
py = 0
def up():
global px
global py
py = player.ycor() + 5
if py >= 200:
py -= 15
player.sety(py)
def down():
global px
global py
py = player.ycor() - 5
if py < -200:
py += 15
player.sety(py)
def left():
global px
global py
px = player.xcor() - 5
if px <= -200:
px += 15
player.setx(px)
def right():
global px
global py
px = player.xcor() + 5
if px >= 200:
px -= 15
player.setx(px)
#distance calculator
def checkcollision(t1, t2):
while t1.distance(t2) < 10:
t2.setpos(randint(-100, 100), randint(-100, 100))
# the x and y distance that the player2 turtle moves
dx = 5
dy = 5
def checkbullet(bullet,turtle):
while bullet.distance(turtle) < 10:
turtle.hideturtle()
#1st enemy(switch heading)
head = 0
def enemy1():
checkcollision(player,player2)
global head
player2.fd(5)
x2, y2 = player2.position()
head = player2.heading()
if y2 <= -200 or y2 >= 200:
player2.fd(0)
player2.backward(7.5)
player2.setheading((head)* -1)
if x2 <= -200 or x2 >= 200:
player2.fd(0)
player2.backward(7.5)
if head < 90:
player2.setheading(0 - ((head) * 2))
if head>90<179:
player2.setheading((head)/2)
if head>179<260:
player2.setheading((head)/3)
if head>260<361:
player2.setheading((head)/2)
screen.ontimer(enemy1,50)
#Second enemy(dx,dy)
def enemy2():
checkcollision(player, player3)
global dx
global dy
x3, y3 = player3.position()
player3.setposition(x3 + dx, y3 + dy)
if y3 <= -200 or y3 >= 200:
dy *= -1
player3.sety(y3 + dy)
if x3 <= -200 or x3 >= 200:
dx *= -1
player3.setx(x3 + dx)
screen.ontimer(enemy2,50)
def enemy3():
checkcollision(player,player4)
player4.fd(5)
x4, y4 = player4.position()
head3 = player4.heading()
if y4 <= -200 or y4 >= 200:
player4.fd(0)
player4.backward(7.5)
player4.fd(0)
player4.setheading((head3)* -1)
if x4 <= -200 or x4 >= 200:
player4.fd(0)
player4.backward(7.5)
player4.fd(0)
if head3 < 90:
player4.setheading(0 - ((head3) * 2))
if head3>90<179:
player4.setheading((head3)/2)
if head3>179<260:
player4.setheading((head3)/3)
if head3>260<361:
player4.setheading((head3)/2)
screen.ontimer(enemy3,50)
#When bullet hits wall
def bullet_end():
screen.listen()
screen.onkeypress(up, 'Up')
screen.onkeypress(left, 'Left')
screen.onkeypress(right, 'Right')
screen.onkeypress(down, 'Down')
screen.onkeyrelease(shoot_key,"w")
def shoot_key():
bullet.setposition(px,py)
bullet.showturtle()
bullet.penup
def shoot():
checkbullet(bullet,player2)
checkbullet(bullet,player3)
checkbullet(bullet,player4)
bx = bullet.xcor()
by = bullet.ycor()
bullet.fd(5)
if bx>=200 or bx<=-200:
bullet.hideturtle()
bullet.backward(7.5)
bullet_end()
if by>=200 or by<=-200:
bullet.hideturtle()
bullet.backward(7.5)
bullet_end()
screen.ontimer(shoot,50)
shoot()
screen.listen()
screen.onkeypress(up, 'Up')
screen.onkeypress(left, 'Left')
screen.onkeypress(right, 'Right')
screen.onkeypress(down, 'Down')
screen.onkeyrelease(shoot_key,"w")
enemy1()
enemy2()
enemy3()
screen.mainloop()
从海龟导入屏幕,海龟
随机输入
从随机导入randint
#屏幕设置
screen=screen()
屏幕设置(宽度=450,高度=450)
screen.bgcolor('green'))
屏幕跟踪(3)
#使用者
玩家=乌龟()
player.shape('square')
player.penup()
#第一敌人
player2=海龟()
player2.形状(“海龟”)
player2.penup()
player2.setpos(random.randint(-200200),random.randint(-200200))
player2.设置标题(random.randint(1360))
#第二敌人
player3=海龟()
播放器3.形状(“方形”)
player3.penup()
player3.setpos(random.randint(-200200),random.randint(-200200))
player3.设置标题(random.randint(1360))
#第三敌人
player4=海龟()
player4.形状(“三角形”)
player4.penup()
player4.setpos(random.randint(-200200),random.randint(-200200))
player4.设置标题(random.randint(1360))
###第四敌人
##player5=海龟()
##player5.形状(“海龟”)
##player5.penup()
##player5.setpos(random.randint(-200200),random.randint(-200200))
#子弹头
子弹=乌龟()
子弹形状(“海龟”)
子弹
子弹头
bullet.setpos(random.randint(-200200),random.randint(-200200))
子弹
子弹头
px=0
py=0
def up():
全球px
全局py
py=player.ycor()+5
如果py>=200:
py-=15
player.sety(py)
def down():
全球px
全局py
py=player.ycor()-5
如果py<-200:
py+=15
player.sety(py)
def left():
全球px
全局py
px=player.xcor()-5
如果px=200:
px-=15
player.setx(px)
#距离计算器
def检查碰撞(t1、t2):
而t1.距离(t2)<10:
t2.setpos(randint(-100100),randint(-100100))
#player2海龟移动的x和y距离
dx=5
dy=5
def检查子弹(子弹,乌龟):
子弹距离(乌龟)<10:
乌龟
#第一敌人(切换航向)
水头=0
def enemy1():
检查碰撞(玩家,玩家2)
全球负责人
player2.fd(5)
x2,y2=播放机2.位置()
head=player2.heading()
如果y2=200:
player2.fd(0)
运动员2.向后(7.5)
播放者2.设置标题((标题)*-1)
如果x2=200:
player2.fd(0)
运动员2.向后(7.5)
如果压头<90:
播放者2.设置标题(0-((标题)*2))
如果head>9017926090179260=200或bx=200或by你就快到了!bullet.penup
有三种用法,应该是bullet.penup()
。只有使用()
才能真正调用函数。您就快到了!bullet.penup
有三种用法,应该是bullet.penup()
。只有使用()
才能实际调用该函数