我对这个我正在制作的游戏有一些疑问......
首先,当我按F
使坦克开火时,weapon_explosion.zip
的图像全部放在一起,造成大量滞后。
第二,在我解决了很多错误之后: 再次射击使子弹再次朝同一方向发展。 子弹加速到极高的速度? (爆炸滞后)
第三:
如何制作.zip
动画一次,我只是在约2.5秒后摧毁它
现在,这是我的代码:
PlayerImage Class
(坦克):
class PlayerImage(Image):
angle = NumericProperty(0)
def __init__(self, **kwargs):
self.tuplePos = None
self.y = 122
self.eventf = None
self.eventr = None
self.source = './rpgArt/tankright.zip'
Clock.schedule_interval(lambda dt: self.gravity(), 1.0 / 60.0)
super(PlayerImage, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self, None)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_key_down)
self.gravVelocity = 1
self.bullet = ""
self.explosion = None
self.iterationBul = 0
self.left = None
self.iterations = 0
self.weapon = "./rpgArt/"
self.redtank = "./rpgArt/tankModels/tank_red/"
self.bluetank = "./rpgArt/tankModels/tank_red/"
self.redtankRight = self.redtank + "normal/tank_red_right.zip"
self.redtankLeft = self.redtank + "normal/tank_red_left.zip"
self.bullet = Image(source=self.weapon + "weapon_bullet.zip")
def on_key_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == "right" or keycode[1] == "d":
self.source = self.redtankRight
self.x += 5
if keycode[1] == "left" or keycode[1] == "a":
self.source = self.redtankLeft
self.x -= 5
if keycode[1] == "up" or keycode[1] == "spacebar" or keycode[1] == "w":
if self.source == self.redtankLeft:
self.source = self.redtankLeft
if self.source == self.redtankRight:
self.source = self.redtankRight
self.y += 50
if keycode[1] == "f":
if self.source == self.redtankLeft:
self.bullet = Image(source=self.weapon + "weapon_bullet.zip")
self.source = self.redtank + "fire/tank_red_left_fire.zip"
self.tuplePos = self.x - 50, self.y - 3
self.bullet.pos = self.tuplePos # TODO POSITION BULLET
self.add_widget(self.bullet)
if self.bullet is not None:
self.eventr = Clock.schedule_interval(lambda dt: self.moveBullet(), 1.0 / 60.0) # TODO MOVE BULLET CLOCK
elif self.source == self.redtankRight:
self.bullet = Image(source=self.weapon + "weapon_bullet.zip")
self.source = self.redtank + "fire/tank_red_right_fire.zip"
self.tuplePos = self.x + 50, self.y - 3
self.bullet.pos = self.tuplePos
self.add_widget(self.bullet)
if self.bullet is not None:
self.eventr = Clock.schedule_interval(lambda dt: self.moveBullet(), 1.0 / 60.0)
def moveBullet(self):
if self.iterationBul == 0:
if self.source == self.redtank + "fire/tank_red_left_fire.zip":
self.left = True
self.iterationBul = 1
elif self.source == self.redtank + "fire/tank_red_right_fire.zip":
self.left = False
self.iterationBul = 1
else:
if self.left is True:
self.eventf = Clock.schedule_interval(lambda dt: self.movebulletleft(), 5.0 / 20.0)
if self.left is False:
self.eventf = Clock.schedule_interval(lambda dt: self.movebulletright(), 5.0 / 20.0)
def movebulletleft(self):
if self.bullet is None:
self.bullet = Image(source=self.weapon + "weapon_bullet.zip")
else:
x, y = self.tuplePos
if x < 0:
self.explosion = Image(source=self.weapon + "weapon_explosion.zip")
self.explosion.pos = x, y
self.add_widget(self.explosion)
self.remove_widget(self.bullet)
Clock.unschedule(self.eventf)
Clock.unschedule(self.eventr)
self.iterationBul = 0
self.left = None
self.bullet = None
Clock.schedule_once(lambda dt: self.REexplosion(), 5)
else:
self.bullet.pos = (x - 3, y)
self.tuplePos = self.bullet.pos
print('left is true')
print(str(self.bullet.pos) + " bullet pos")
def REexplosion(self):
self.remove_widget(self.explosion)
def movebulletright(self):
if self.bullet is None:
self.bullet = Image(source=self.weapon + "weapon_bullet.zip")
else:
self.bullet.pos = (x, y) = self.tuplePos
if x > 680:
self.explosion = Image(source=self.weapon + "weapon_explosion.zip")
self.explosion.pos = x, y
self.add_widget(self.explosion)
self.remove_widget(self.bullet)
Clock.unschedule(self.eventf)
Clock.unschedule(self.eventr)
self.iterationBul = 0
self.left = None
self.bullet = None
Clock.schedule_once(lambda dt: self.REexplosion(), 1.7)
else:
v = 3
self.bullet.pos = (x + v, y)
self.tuplePos = self.bullet.pos
print('right is true')
try:
print(str(self.bullet.pos) + " bullet pos")
except AttributeError:
print("bullet out of range!")
def gravity(self):
if self.y == 123:
pass
else:
self.y -= 2
if self.y <= 122:
self.y += 1
# if self.iterations / 60 != 0:
# result = self.iterations / 60
# self.gravVelocity += result
# print(self.y)
def on_touch_down(self, touch):
# self.currentstate = self.states["person.zip/"]
Animation.cancel_all(self)
angle = degrees(atan2(touch.y - self.center_y,
touch.x - self.center_x))
Animation(center=touch.pos, angle=angle).start(self)
# self.currentstate = self.states["personred/rest.png/"]
print(self.y)
另外,我的Builder.load_string
s:
root = Builder.load_string('''
Widget:
Widget:
canvas:
Color:
rgb: 0,0,1
Rectangle:
size: (900,500)
pos: (0,150)
Widget:
Image:
pos: (0,150)
size: (400,400)
source: './rpgArt/city.zip'
Widget:
canvas:
Color:
rgb: 0,1,0
Rectangle:
size: (900,150)
pos: (0,0)
PlayerImage:
source: './rpgArt/tankModels/tank_red/normal/tank_red_right.zip'
allow_stretch: True
keep_ratio: True
pos:(0,130)
''')
runTouchApp(root)
另一个我不完全理解的......(来自别人。)
Builder.load_string('''
<PlayerImage>:
canvas.before:
PushMatrix
canvas.after:
PopMatrix
''')
答案 0 :(得分:0)
我需要在self.eventr
取消预定movebullet(self):
。
Clock.unschedule(self.eventr)