碰撞不是';t向Python Turtles注册
我根据在线教程创建了一个简单的2D射击游戏,而敌人的精灵(除了1个,总共有5个)并没有遵守我的碰撞代码。除了玩家控制的子弹物体直接穿过敌人的精灵外,其他一切都正常工作。我收到的错误消息:碰撞不是';t向Python Turtles注册,python,python-3.6,collision,turtle-graphics,Python,Python 3.6,Collision,Turtle Graphics,我根据在线教程创建了一个简单的2D射击游戏,而敌人的精灵(除了1个,总共有5个)并没有遵守我的碰撞代码。除了玩家控制的子弹物体直接穿过敌人的精灵外,其他一切都正常工作。我收到的错误消息: Traceback (most recent call last): File "C:\Python\Python36\pygame projects\space invaders.py", line 163, in <module> bullet.hideturtle(
Traceback (most recent call last):
File "C:\Python\Python36\pygame projects\space invaders.py", line 163, in <module>
bullet.hideturtle()
File "C:\Python\Python36\lib\turtle.py", line 2322, in hideturtle
self.pen(shown=False)
File "C:\Python\Python36\lib\turtle.py", line 2459, in pen
self._update()
File "C:\Python\Python36\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Python\Python36\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Python\Python36\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
回溯(最近一次呼叫最后一次):
文件“C:\Python\Python36\pygame projects\space invaders.py”,第163行,在
子弹
文件“C:\Python\Python36\lib\turtle.py”,第2322行,在hideturtle中
self.pen(显示=False)
文件“C:\Python\Python36\lib\turtle.py”,第2459行,用钢笔书写
自我更新()
更新中第2660行的文件“C:\Python\Python36\lib\turtle.py”
自我更新数据()
文件“C:\Python\Python36\lib\turtle.py”,第2646行,在更新数据中
self.screen.\u incrementudc()
文件“C:\Python\Python36\lib\turtle.py”,第1292行,在增量UDC中
上升终止符
乌龟,终结者
如果你将代码浏览一半,你会发现我用于碰撞数学的毕达哥拉斯理论,不确定它是否正确:
enemies = []
number_of_enemies = 5
for i in range(number_of_enemies):
enemies.append(turtle.Turtle())
for enemy in enemies:
enemy.color("green")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
enemyspeed = 2
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2)+ math.pow(t1.ycor() - t2.ycor(), 2))
if distance < 15:
return True
else:
return False
while True:
for enemy in enemies:
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
if enemy.xcor() > 280:
for e in enemies:
y = enemy.ycor()
y -= 40
enemy.sety(y)
enemyspeed *= -1
if enemy.xcor() < -280:
for e in enemies:
y = enemy.ycor()
y -= 40
enemy.sety(y)
enemyspeed *= -1
if bulletstate == "fire":
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)
if isCollision(bullet, enemy):
#reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -400)
#reset the enemy
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
if isCollision(player, enemy):
player.hideturtle()
enemy.hideturtle()
print ("Get To The Doctors! You're Riddled!")
break
敌人=[]
敌人的数量=5
对于范围内的i(敌人的数量):
敌人。追加(海龟。海龟()
对于敌人中的敌人:
颜色(“绿色”)
敌人形状(“圆圈”)
敌人
敌人,速度(0)
x=random.randint(-200200)
y=random.randint(100250)
敌方设定位置(x,y)
敌人速度=2
def聚合(t1、t2):
距离=math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
如果距离小于15:
返回真值
其他:
返回错误
尽管如此:
对于敌人中的敌人:
x=敌人。xcor()
x+=敌人速度
敌人,setx(x)
如果敌方.xcor()大于280:
对于敌人中的e:
y=敌人。ycor()
y-=40
敌人,塞蒂(y)
敌人速度*=-1
如果敌方.xcor()<-280:
对于敌人中的e:
y=敌人。ycor()
y-=40
敌人,塞蒂(y)
敌人速度*=-1
如果bulletstate==“火灾”:
y=bullet.ycor()
y+=速度
子弹,塞蒂(y)
如果发生冲突(子弹、敌人):
#重设子弹
子弹
bulletstate=“就绪”
项目符号。设置位置(0,-400)
#重置敌人
x=random.randint(-200200)
y=random.randint(100250)
敌方设定位置(x,y)
如果联合(玩家、敌人):
player.hideturtle()
敌人隐藏处
打印(“去找医生!你被打了个谜!”)
打破
如果答案显而易见,我会道歉。如果有人花时间解释错误,我将不胜感激。其他人也提出了类似的问题,但每一个问题都有适用于他们的例子,我仍然处在一个很难想象其他人的例子的阶段。任何帮助都将不胜感激。我认为您的主要问题是在每次敌人更新之间更新子弹,而不是在所有敌人更新中更新一次。这在视觉和真实发生的事情之间造成了一种偏差。而且,你不需要定义毕达哥拉斯公式,海龟们已经知道了!下面是我为解决上述问题而对您的代码进行的修改,并对编码风格和效率进行了一些修改:
from turtle import Turtle, Screen
from random import randint
NUMBER_OF_ENEMIES = 5
ENEMY_JUMP = 40
BULLET_SPEED = 20
PLAYER_SPEED = 15
SAFETY_DISTANCE = 15
GALLERY_WIDTH, GALLERY_HEIGHT = 560, 550
GALLERY_BORDER = 80
# player movement
def move_left():
x = player.xcor() - PLAYER_SPEED
if x < -GALLERY_WIDTH / 2:
x = -GALLERY_WIDTH / 2
player.setx(x)
def move_right():
x = player.xcor() + PLAYER_SPEED
if x > GALLERY_WIDTH / 2:
x = GALLERY_WIDTH / 2
player.setx(x)
def fire_bullet():
global bulletstate
if bulletstate == 'ready':
bulletstate = 'fire'
bullet.setposition(player.position())
bullet.forward(BULLET_SPEED / 2)
bullet.showturtle()
def isCollision(t1, t2):
return t1.distance(t2) < SAFETY_DISTANCE
def move():
global enemy_speed, bulletstate
screen.tracer(False)
for enemy in enemies:
x = enemy.xcor() + enemy_speed
enemy.setx(x)
if x > GALLERY_WIDTH / 2:
for e in enemies:
y = e.ycor() - ENEMY_JUMP
e.sety(y)
enemy_speed *= -1
elif x < -GALLERY_WIDTH / 2:
for e in enemies:
y = e.ycor() - ENEMY_JUMP
e.sety(y)
enemy_speed *= -1
if isCollision(player, enemy):
player.hideturtle()
enemy.hideturtle()
print("Get To The Doctors! You're Riddled!")
screen.update()
return
if isCollision(bullet, enemy):
# reset the bullet
bullet.hideturtle()
bullet.setposition(0, -GALLERY_HEIGHT)
bulletstate = 'ready'
# reset the enemy
enemy.hideturtle()
x = randint(GALLERY_BORDER - GALLERY_WIDTH / 2, GALLERY_WIDTH / 2 - GALLERY_BORDER)
y = randint(GALLERY_HEIGHT / 2 - 175, GALLERY_HEIGHT / 2 - 25)
enemy.setposition(x, y)
enemy.showturtle()
if bulletstate == 'fire':
bullet.forward(BULLET_SPEED)
# check to see if the bullets has gone to the top
if bullet.ycor() > GALLERY_HEIGHT / 2:
bullet.hideturtle()
bulletstate = 'ready'
screen.tracer(True)
screen.ontimer(move, 25)
screen = Screen()
screen.setup(GALLERY_WIDTH + GALLERY_BORDER, GALLERY_HEIGHT + GALLERY_BORDER)
screen.bgcolor('black')
player = Turtle('turtle', visible=False)
player.speed('fastest')
player.color('red')
player.penup()
player.setheading(90)
player.sety(-250) # need to define this
player.showturtle()
enemies = []
enemy_speed = 1
for _ in range(NUMBER_OF_ENEMIES):
enemy = Turtle('circle', visible=False)
enemy.speed('fastest')
enemy.color('green')
enemy.penup()
x = randint(GALLERY_BORDER - GALLERY_WIDTH / 2, GALLERY_WIDTH / 2 - GALLERY_BORDER)
y = randint(GALLERY_HEIGHT / 2 - 175, GALLERY_HEIGHT / 2 - 25) # define these!
enemy.setposition(x, y)
enemy.showturtle()
enemies.append(enemy)
# create the player's spunk shots
bullet = Turtle('triangle', visible=False)
bullet.speed('fastest')
bullet.shapesize(0.5)
bullet.color('white')
bullet.penup()
bullet.setheading(90)
# define bullet state
# ready - ready to fire
# fire - bullet is firing
bulletstate = 'ready'
screen.onkey(move_left, 'Left')
screen.onkey(move_right, 'Right')
screen.onkey(fire_bullet, 'space')
screen.listen()
move()
screen.mainloop()
从海龟导入海龟,屏幕
从随机导入randint
敌人的数量=5
敌人的跳跃=40
子弹头速度=20
玩家速度=15
安全距离=15
廊道宽度、廊道高度=560550
图库_边框=80
#球员运动
def向左移动()
x=player.xcor()-播放器速度
如果x<-通道宽度/2:
x=-廊道宽度/2
player.setx(x)
def向右移动()
x=播放器.xcor()+播放器速度
如果x>GALLERY_宽度/2:
x=通道宽度/2
player.setx(x)
def fire_bullet():
全球通讯状态
如果bulletstate==“就绪”:
bulletstate=‘火灾’
bullet.setposition(player.position())
子弹前进(子弹速度/2)
bullet.showturtle()
def聚合(t1、t2):
返回t1.距离(t2)<安全距离
def move():
全球敌方速度,子弹状态
屏幕跟踪(假)
对于敌人中的敌人:
x=敌方.xcor()+敌方速度
敌人,setx(x)
如果x>GALLERY_宽度/2:
对于敌人中的e:
y=e.ycor()-敌人跳
e、 赛蒂(y)
敌方速度*=-1
elif x<-廊道宽度/2:
对于敌人中的e:
y=e.ycor()-敌人跳
e、 赛蒂(y)
敌方速度*=-1
如果联合(玩家、敌人):
player.hideturtle()
敌人隐藏处
打印(“去找医生!你被打了个谜!”)
screen.update()
回来
如果发生冲突(子弹、敌人):
#重设子弹
子弹
项目符号。设置位置(0,-通道高度)
bulletstate='ready'
#重置敌人
敌人隐藏处
x=randint(图库边框-图库宽度/2,图库宽度/2-图库边框)
y=randint(廊道高度/2-175,廊道高度/2-25)
敌方设定位置(x,y)
敌人
如果bulletstate=='fire':
子弹前进(子弹速度)
#检查子弹是否已经飞到了顶端
如果bullet.ycor()>通道高度/2:
子弹
bulletstate='ready'
屏幕跟踪(真)
屏幕时间(移动,25)
screen=screen()
屏幕设置(图库宽度+图库边框、图库高度+图库边框)
screen.bgcolor('黑色')
玩家=乌龟(“乌龟”,可见=假)
玩家速度(“最快”)
player.color('red'))
player.penup()
玩家设置标题(90)
player.sety(-250)#需要定义这个
player.showturtle()
敌人=[]
敌方速度=1
适用于范围内的(NU