如果我的船发射10发子弹,我需要在屏幕上画10颗子弹。 我可以在屏幕上绘制10个同样的东西,而不需要为每个代码添加代码,并且只有一个代码块吗?
例如,这里有10个子弹的所有:
#1
lGun1 = -999
rGun1 = -999
yBul1 = 0
xBul1 = 0
fire1 = 0
b1 = False
ybul1_change = 0
xbul1_change = 0
#2
lGun2 = -999
rGun2 = -999
yBul2 = 0
xBul2 = 0
fire2 = 0
b2 = False
ybul2_change = 0
xbul2_change = 0
#3
lGun3 = -999
rGun3 = -999
yBul3 = 0
xBul3 = 0
fire3 = 0
b3 = False
ybul3_change = 0
xbul3_change = 0
#4
lGun4 = -999
rGun4 = -999
yBul4 = 0
xBul4 = 0
fire4 = 0
b4 = False
ybul4_change = 0
xbul4_change = 0
#5
lGun5 = -999
rGun5 = -999
yBul5 = 0
xBul5 = 0
fire5 = 0
b5 = False
ybul5_change = 0
xbul5_change = 0
#6
lGun6 = -999
rGun6 = -999
yBul6 = 0
xBul6 = 0
fire6 = 0
b6 = False
ybul6_change = 0
xbul6_change = 0
#7
lGun7 = -999
rGun7 = -999
yBul7 = 0
xBul7 = 0
fire7 = 0
b7 = False
ybul7_change = 0
xbul7_change = 0
#8
lGun8 = -999
rGun8 = -999
yBul8 = 0
xBul8 = 0
fire8 = 0
b8 = False
ybul8_change = 0
xbul8_change = 0
#9
lGun9 = -999
rGun9 = -999
yBul9 = 0
xBul9 = 0
fire9 = 0
b9 = False
ybul9_change = 0
xbul9_change = 0
#10
lGun10 = -999
rGun10 = -999
yBul10 = 0
xBul10 = 0
fire10 = 0
b10 = False
ybul10_change = 0
xbul10_change = 0
#End of bullets
gameExit = False
#Start of Game Loop
while not gameExit:
for event in pygame.event.get():
elif event.key == pygame.K_f:
#1
if b1 == False:
fire1 = face
if fire1 == 1 or fire1 == 3:
xbul1_change = 0
xBul1 = 0
lGun1 = x + 9
rGun1 = x + 59
if fire1 == 1:
ybul1_change = -bulSpeed
yBul1 = y - 10
else:
ybul1_change = bulSpeed
yBul1 = y + 88
else:
ybul1_change = 0
yBul1 = 0
lGun1 = y + 9
rGun1 = y + 59
if fire1 == 2:
xbul1_change = bulSpeed
xBul1 = x + 88
else:
xbul1_change = -bulSpeed
xBul1 = x - 10
b1 = True
#2
elif b2 == False:
fire2 = face
if fire2 == 1 or fire2 == 3:
xbul2_change = 0
xBul2 = 0
lGun2 = x + 9
rGun2 = x + 59
if fire2 == 1:
ybul2_change = -bulSpeed
yBul2 = y - 10
else:
ybul2_change = bulSpeed
yBul2 = y + 88
else:
ybul2_change = 0
yBul2 = 0
lGun2 = y + 9
rGun2 = y + 59
if fire2 == 2:
xbul2_change = bulSpeed
xBul2 = x + 88
else:
xbul2_change = -bulSpeed
xBul2 = x - 10
b2 = True
#3
elif b3 == False:
fire3 = face
if fire3 == 1 or fire3 == 3:
xbul3_change = 0
xBul3 = 0
lGun3 = x + 9
rGun3 = x + 59
if fire3 == 1:
ybul3_change = -bulSpeed
yBul3 = y - 10
else:
ybul3_change = bulSpeed
yBul3 = y + 88
else:
ybul3_change = 0
yBul3 = 0
lGun3 = y + 9
rGun3 = y + 59
if fire3 == 2:
xbul3_change = bulSpeed
xBul3 = x + 88
else:
xbul3_change = -bulSpeed
xBul3 = x - 10
b3 = True
#4
elif b4 == False:
fire4 = face
if fire4 == 1 or fire4 == 3:
xbul4_change = 0
xBul4 = 0
lGun4 = x + 9
rGun4 = x + 59
if fire4 == 1:
ybul4_change = -bulSpeed
yBul4 = y - 10
else:
ybul4_change = bulSpeed
yBul4 = y + 88
else:
ybul4_change = 0
yBul4 = 0
lGun4 = y + 9
rGun4 = y + 59
if fire4 == 2:
xbul4_change = bulSpeed
xBul4 = x + 88
else:
xbul4_change = -bulSpeed
xBul4 = x - 10
b4 = True
#5
elif b5 == False:
fire5 = face
if fire5 == 1 or fire5 == 3:
xbul5_change = 0
xBul5 = 0
lGun5 = x + 9
rGun5 = x + 59
if fire5 == 1:
ybul5_change = -bulSpeed
yBul5 = y - 10
else:
ybul5_change = bulSpeed
yBul5 = y + 88
else:
ybul5_change = 0
yBul5 = 0
lGun5 = y + 9
rGun5 = y + 59
if fire5 == 2:
xbul5_change = bulSpeed
xBul5 = x + 88
else:
xbul5_change = -bulSpeed
xBul5 = x - 10
b5 = True
#6
elif b6 == False:
fire6 = face
if fire6 == 1 or fire6 == 3:
xbul6_change = 0
xBul6 = 0
lGun6 = x + 9
rGun6 = x + 59
if fire6 == 1:
ybul6_change = -bulSpeed
yBul6 = y - 10
else:
ybul6_change = bulSpeed
yBul6 = y + 88
else:
ybul6_change = 0
yBul6 = 0
lGun6 = y + 9
rGun6 = y + 59
if fire6 == 2:
xbul6_change = bulSpeed
xBul6 = x + 88
else:
xbul6_change = -bulSpeed
xBul6 = x - 10
b6 = True
#7
elif b7 == False:
fire7 = face
if fire7 == 1 or fire7 == 3:
xbul7_change = 0
xBul7 = 0
lGun7 = x + 9
rGun7 = x + 59
if fire7 == 1:
ybul7_change = -bulSpeed
yBul7 = y - 10
else:
ybul7_change = bulSpeed
yBul7 = y + 88
else:
ybul7_change = 0
yBul7 = 0
lGun7 = y + 9
rGun7 = y + 59
if fire7 == 2:
xbul7_change = bulSpeed
xBul7 = x + 88
else:
xbul7_change = -bulSpeed
xBul7 = x - 10
b7 = True
#8
elif b8 == False:
fire8 = face
if fire8 == 1 or fire8 == 3:
xbul8_change = 0
xBul8 = 0
lGun8 = x + 9
rGun8 = x + 59
if fire8 == 1:
ybul8_change = -bulSpeed
yBul8 = y - 10
else:
ybul8_change = bulSpeed
yBul8 = y + 88
else:
ybul8_change = 0
yBul8 = 0
lGun8 = y + 9
rGun8 = y + 59
if fire8 == 2:
xbul8_change = bulSpeed
xBul8 = x + 88
else:
xbul8_change = -bulSpeed
xBul8 = x - 10
b8 = True
#9
elif b9 == False:
fire9 = face
if fire9 == 1 or fire9 == 3:
xbul9_change = 0
xBul9 = 0
lGun9 = x + 9
rGun9 = x + 59
if fire9 == 1:
ybul9_change = -bulSpeed
yBul9 = y - 10
else:
ybul9_change = bulSpeed
yBul9 = y + 88
else:
ybul9_change = 0
yBul9 = 0
lGun9 = y + 9
rGun9 = y + 59
if fire9 == 2:
xbul9_change = bulSpeed
xBul9 = x + 88
else:
xbul9_change = -bulSpeed
xBul9 = x - 10
b9 = True
#10
elif b10 == False:
fire10 = face
if fire10 == 1 or fire10 == 3:
xbul10_change = 0
xBul10 = 0
lGun10 = x + 9
rGun10 = x + 59
if fire10 == 1:
ybul10_change = -bulSpeed
yBul10 = y - 10
else:
ybul10_change = bulSpeed
yBul10 = y + 88
else:
ybul10_change = 0
yBul10 = 0
lGun10 = y + 9
rGun10 = y + 59
if fire10 == 2:
xbul10_change = bulSpeed
xBul10 = x + 88
else:
xbul10_change = -bulSpeed
xBul10 = x - 10
b1 = False
b2 = False
b3 = False
b4 = False
b5 = False
b6 = False
b7 = False
b8 = False
b9 = False
b10 = False
#1
if fire1 == 1 or fire1 == 3:
pygame.draw.rect(gameDisplay, red, [lGun1, yBul1, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun1, yBul1, 2, 10])
elif fire1 == 2 or fire1 == 4:
pygame.draw.rect(gameDisplay, red, [xBul1, lGun1, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul1, rGun1, 10, 2])
#2
if fire2 == 1 or fire2 == 3:
pygame.draw.rect(gameDisplay, red, [lGun2, yBul2, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun2, yBul2, 2, 10])
elif fire2 == 2 or fire2 == 4:
pygame.draw.rect(gameDisplay, red, [xBul2, lGun2, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul2, rGun2, 10, 2])
#3
if fire3 == 1 or fire3 == 3:
pygame.draw.rect(gameDisplay, red, [lGun3, yBul3, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun3, yBul3, 2, 10])
elif fire3 == 2 or fire3 == 4:
pygame.draw.rect(gameDisplay, red, [xBul3, lGun3, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul3, rGun3, 10, 2])
#4
if fire4 == 1 or fire4 == 3:
pygame.draw.rect(gameDisplay, red, [lGun4, yBul4, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun4, yBul4, 2, 10])
elif fire4 == 2 or fire4 == 4:
pygame.draw.rect(gameDisplay, red, [xBul4, lGun4, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul4, rGun4, 10, 2])
#5
if fire5 == 1 or fire5 == 3:
pygame.draw.rect(gameDisplay, red, [lGun5, yBul5, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun5, yBul5, 2, 10])
elif fire5 == 2 or fire5 == 4:
pygame.draw.rect(gameDisplay, red, [xBul5, lGun5, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul5, rGun5, 10, 2])
#6
if fire6 == 1 or fire6 == 3:
pygame.draw.rect(gameDisplay, red, [lGun6, yBul6, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun6, yBul6, 2, 10])
elif fire6 == 2 or fire6 == 4:
pygame.draw.rect(gameDisplay, red, [xBul6, lGun6, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul6, rGun6, 10, 2])
#7
if fire7 == 1 or fire7 == 3:
pygame.draw.rect(gameDisplay, red, [lGun7, yBul7, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun7, yBul7, 2, 10])
elif fire7 == 2 or fire7 == 4:
pygame.draw.rect(gameDisplay, red, [xBul7, lGun7, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul7, rGun7, 10, 2])
#8
if fire8 == 1 or fire8 == 3:
pygame.draw.rect(gameDisplay, red, [lGun8, yBul8, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun8, yBul8, 2, 10])
elif fire8 == 2 or fire8 == 4:
pygame.draw.rect(gameDisplay, red, [xBul8, lGun8, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul8, rGun8, 10, 2])
#9
if fire9 == 1 or fire9 == 3:
pygame.draw.rect(gameDisplay, red, [lGun9, yBul9, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun9, yBul9, 2, 10])
elif fire9 == 2 or fire9 == 4:
pygame.draw.rect(gameDisplay, red, [xBul9, lGun9, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul9, rGun9, 10, 2])
#10
if fire10 == 1 or fire10 == 3:
pygame.draw.rect(gameDisplay, red, [lGun10, yBul10, 2, 10])
pygame.draw.rect(gameDisplay, red, [rGun10, yBul10, 2, 10])
elif fire10 == 2 or fire10 == 4:
pygame.draw.rect(gameDisplay, red, [xBul10, lGun10, 10, 2])
pygame.draw.rect(gameDisplay, red, [xBul10, rGun10, 10, 2])
#1
yBul1 += ybul1_change
xBul1 += xbul1_change
#2
yBul2 += ybul2_change
xBul2 += xbul2_change
#3
yBul3 += ybul3_change
xBul3 += xbul3_change
#4
yBul4 += ybul4_change
xBul4 += xbul4_change
#5
yBul5 += ybul5_change
xBul5 += xbul5_change
#6
yBul6 += ybul6_change
xBul6 += xbul6_change
#7
yBul7 += ybul7_change
xBul7 += xbul7_change
#8
yBul8 += ybul8_change
xBul8 += xbul8_change
#9
yBul9 += ybul9_change
xBul9 += xbul9_change
#10
yBul10 += ybul10_change
xBul10 += xbul10_change
我可以使用一大块代码和一个列表吗? 如果有人有想法,我会全力以赴。
答案 0 :(得分:1)
我以前也遇到过这个问题。你需要的是一个清单。
这是一个例子:
Bullets = [[BulletX,BulletY,BulletDirection],[BulletX,BulletY,BulletDirection]]
要向项目列表中添加项目符号,您可以执行以下操作:
Bullets.append([BulletX,BulletY,BulletDirection])
要显示它们,请使用:
for Bullet in Bullets:
BulletX = Bullet[0]
BulletY = Bullet[1]
BulletDIrection = Bullet[2]
(PutYourSurfaceHere).blit(BulletImage,(BulletX,BulletY)
如果您不知道,for
是一个循环,基本上意味着:对于子弹中的每个子弹:执行以下操作。此外,BulletX = Bullet[0]
将变量BulletX
设置为列表Bullet
的第一部分。列表项目符号内有列表。子弹内的每个列表都是" bullet"。通过循环,您只需访问Bullet的数据。此外,Bullet[1]
正在访问该特定项目符号列表的第二部分。 (因为它从0开始计算)然后你只是在它的位置上点击子弹的图像。要移动它,您可以Bullet[0] += 5
。 (这会将它向右移动5个像素。)
要移除子弹,只需放入循环Bullets.remove(Bullet)
即可。这将从目前正在处理的列表中删除子弹。
如果你无法从中解决这个问题,只需google一个列表并循环学习python。 (谷歌他们分开。)
答案 1 :(得分:1)
这里有两个很好的教程可以帮助您学习如何使用list和list.pop()。
列表 - http://www.tutorialspoint.com/python/python_lists.htm
List.pop() - http://www.tutorialspoint.com/python/list_pop.htm
这是我写的一些示例代码的链接,可以帮助您学习:
https://drive.google.com/folderview?id=0B5qxaP8LSuzcQWlucUM4NDUwelk&usp=sharing