自己理解尝试写了己方飞机的代码,发现子弹原地不动,帮忙求解!拜托
本帖最后由 幸运值MAX 于 2020-10-3 11:59 编辑import pygame
#己方飞机类
class Myplane:
def __init__(self, screen):
self.x = 180
self.y = 550
self.screen = screen
self.image = p.image.load('./sucai/images/plane.png')
self.moving_left = False
self.moving_right = False
self.moving_up = False
self.moving_down = False
self.keeping_fire = False
self.bulletlist = []
def display(self):
self.screen.blit(self.image, (self.x, self.y))
delbulletlist = []
for bullet in self.bulletlist:
bullet.display()
bullet.move()
for firebullet in self.bulletlist:
if firebullet.judge:
delbulletlist.append(firebullet)
for item in delbulletlist:
self.bulletlist.remove(item)
def control(self):
for event in p.event.get():
if event.type == p.QUIT:
exit()
#实现按下按钮时激活
elif event.type == p.KEYDOWN:
if event.key == p.K_d:
self.moving_right = True
elif event.key == p.K_a:
self.moving_left = True
elif event.key == p.K_w:
self.moving_up = True
elif event.key == p.K_s:
self.moving_down = True
elif event.key == p.K_SPACE:
self.keeping_fire = True
#实现松开按钮时关闭
elif event.type == p.KEYUP:
if event.key == p.K_d:
self.moving_right = False
elif event.key == p.K_a:
self.moving_left = False
elif event.key == p.K_w:
self.moving_up = False
elif event.key == p.K_s:
self.moving_down = False
elif event.key == p.K_SPACE:
self.keeping_fire = False
if self.moving_left:
if self.x > 0:
self.x -= 0.4
elif self.moving_right:
if self.x < 355:
self.x += 0.4
elif self.moving_down:
if self.y < 550:
self.y += 0.4
elif self.moving_up:
if self.y > 0:
self.y -= 0.4
if self.keeping_fire:
#创建子弹实例对象,并将产生的子弹添加到子弹列表中
newbullet = MyBullet(self.x, self.y, self.screen)
self.bulletlist.append(newbullet)
#己方子弹类
class MyBullet:
def __init__(self, x, y, screen):
self.x = x + 21
self.y = y - 6
self.screen = screen
self.image = p.image.load('./sucai/images/bullet.png')
def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self):
self.y -= 1
#判断是否子弹越界
def judge(self):
if self.y < 0:
return True
else:
return False
def main():
p.init()
screen = p.display.set_mode((400, 600))
background = p.image.load('./sucai/images/background.png')
p.display.set_caption('大战外星飞船')
myplane = Myplane(screen)
while True:
screen.blit(background, (0, 0))
myplane.display()
myplane.control()
p.display.update()
if __name__ == '__main__':
main() 小甲鱼的源代码:
嘉岳呀 发表于 2020-10-3 18:45
小甲鱼的源代码:
很感谢能回答问题,我已经买了小甲鱼的课件。但我这个什么也不参考自己凭理解写的代码找不到哪里有问题,然而子弹仅仅只在飞机头部出现。我尝试打印子弹列表,发现子弹列表中始终只有一个元素,而且没有移动就删除了。但我有限定条件:子弹出界以后才会被删除。 我之前也写过,也是有问题找了半天。
再看看子弹这一块吧
页:
[1]