If my ship fires 10 bullets, I need to draw 10 bullets to the screen. Can I draw 10 of the same thing to the screen without having a chuck of code for each of them and only have one chunk of code?
For example, here is everything for the 10 bullets:
#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
Can I use one chunk of code and a list or something? If anyone has an Idea I am all ears.
I used to have this problem too. What you need is a list. Here would be an example: Bullets = [[BulletX,BulletY,BulletDirection],[BulletX,BulletY,BulletDirection]]
To add a bullet to the list, you could do:
Bullets.append([BulletX,BulletY,BulletDirection])
To display them you use:
for Bullet in Bullets: BulletX = Bullet[0] BulletY = Bullet[1] BulletDIrection = Bullet[2] (PutYourSurfaceHere).blit(BulletImage,(BulletX,BulletY)
If you didn't know, for
is a loop which basically means: for every Bullet in Bullets: do the following. Also, the BulletX = Bullet[0]
is setting the variable BulletX
to the first part of the list Bullet
. The list bullet has lists inside of it. Every list inside Bullets is a "bullet". With the loop, you are just accessing the Bullet's data. Also, Bullet[1]
is accessing the second part of the list of that specific bullet. (because it counts from 0) Then you are just blitting the image of a bullet at its location. To move it, you could do Bullet[0] += 5
. (That would move it over to the right by 5 pixels.)
To remove a bullet, just put in the loop Bullets.remove(Bullet)
. That will remove the bullet from the list that it is currently handling.
If you can't figure it out from this, just google a list and loop tutorial for python. (Google them separately.)
Here are two good tutorials to help you learn how to use list and list.pop().
List - http://www.tutorialspoint.com/python/python_lists.htm
List.pop() - http://www.tutorialspoint.com/python/list_pop.htm
Here is a link to some example code i wrote that may help you to learn as well:
https://drive.google.com/folderview?id=0B5qxaP8LSuzcQWlucUM4NDUwelk&usp=sharing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.