Созданы 2 спрайта (самолет и астероид, которые движутся навстречу). При столкновении исчезает самолетик (так и задумано) и окно Pyglet сворачивается.
Как сделать так, чтобы самолет исчезал при столкновении, а программа продолжала работать?
Python 3.8.5
from pyglet.window import key
from pyglet import image
import random
import pyglet
listplayer = []
window = pyglet.window.Window(800,450)
#asteroid = pyglet.image.load("a30003.png")
ast1 =pyglet.sprite.Sprite(img =pyglet.image.load("a30003.png") ,x = 400, y = 350)
listast = [ast1]
airdevice = pyglet.image.load("самолет2.png")
sm1 = pyglet.sprite.Sprite(img = pyglet.image.load("самолет2.png"))
class Player(pyglet.sprite.Sprite):
def __init__(self,*args,**kwargs):
super().__init__(img = airdevice , *args, **kwargs)
self.x = 400
self.y = 100
self.speed = 20
def check(self):
if self.delete():
self.speed = 0
def update(self):
self.check()
self.y += self.speed
sm1 = Player()
listplayer.append(sm1)
@window.event
def on_draw():
window.clear()
for i in listplayer:
i.draw()
ast1.draw()
def update(self):
ast1.y -= 10
sm1.update()
if sm1.y >= ast1.y:
sm1.delete()
listplayer.remove(sm1)
pyglet.clock.schedule_interval(update, 20 / 60.)
pyglet.app.run()
'''
ошибки:
C:\Users\user\PycharmProjects\number1\venv\Scripts\python.exe C:/Users/user/PycharmProjects/number1/collisions.py
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/number1/collisions.py", line 56, in <module>
pyglet.app.run()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\app\__init__.py", line 107, in run
event_loop.run()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\app\base.py", line 167, in run
timeout = self.idle()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\app\base.py", line 237, in idle
redraw_all = self.clock.call_scheduled_functions(dt)
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\clock.py", line 292, in call_scheduled_functions
item.func(now - item.last_ts, *item.args, **item.kwargs)
File "C:/Users/user/PycharmProjects/number1/collisions.py", line 51, in update
sm1.update()
File "C:/Users/user/PycharmProjects/number1/collisions.py", line 32, in update
self.y += self.speed
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\sprite.py", line 485, in y
self._update_position()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\sprite.py", line 428, in _update_position
x1 = self._x - img.anchor_x
AttributeError: 'NoneType' object has no attribute 'anchor_x'
'''[/CODE]
Как сделать так, чтобы самолет исчезал при столкновении, а программа продолжала работать?
Python 3.8.5
from pyglet.window import key
from pyglet import image
import random
import pyglet
listplayer = []
window = pyglet.window.Window(800,450)
#asteroid = pyglet.image.load("a30003.png")
ast1 =pyglet.sprite.Sprite(img =pyglet.image.load("a30003.png") ,x = 400, y = 350)
listast = [ast1]
airdevice = pyglet.image.load("самолет2.png")
sm1 = pyglet.sprite.Sprite(img = pyglet.image.load("самолет2.png"))
class Player(pyglet.sprite.Sprite):
def __init__(self,*args,**kwargs):
super().__init__(img = airdevice , *args, **kwargs)
self.x = 400
self.y = 100
self.speed = 20
def check(self):
if self.delete():
self.speed = 0
def update(self):
self.check()
self.y += self.speed
sm1 = Player()
listplayer.append(sm1)
@window.event
def on_draw():
window.clear()
for i in listplayer:
i.draw()
ast1.draw()
def update(self):
ast1.y -= 10
sm1.update()
if sm1.y >= ast1.y:
sm1.delete()
listplayer.remove(sm1)
pyglet.clock.schedule_interval(update, 20 / 60.)
pyglet.app.run()
'''
ошибки:
C:\Users\user\PycharmProjects\number1\venv\Scripts\python.exe C:/Users/user/PycharmProjects/number1/collisions.py
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/number1/collisions.py", line 56, in <module>
pyglet.app.run()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\app\__init__.py", line 107, in run
event_loop.run()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\app\base.py", line 167, in run
timeout = self.idle()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\app\base.py", line 237, in idle
redraw_all = self.clock.call_scheduled_functions(dt)
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\clock.py", line 292, in call_scheduled_functions
item.func(now - item.last_ts, *item.args, **item.kwargs)
File "C:/Users/user/PycharmProjects/number1/collisions.py", line 51, in update
sm1.update()
File "C:/Users/user/PycharmProjects/number1/collisions.py", line 32, in update
self.y += self.speed
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\sprite.py", line 485, in y
self._update_position()
File "C:\Users\user\PycharmProjects\number1\venv\lib\site-packages\pyglet\sprite.py", line 428, in _update_position
x1 = self._x - img.anchor_x
AttributeError: 'NoneType' object has no attribute 'anchor_x'
'''[/CODE]