[英]If I append more than one of the same thing to a list and i want to get rid of one of them how do i know which one it is?
I am making a little game. 我正在做一个小游戏。 I control a ship and I shoot other little enemy ships.
我控制一艘船,并向其他敌方小舰射击。 They will drop loot for me to pick up.
他们会为我捡赃物。 If I shoot 10 ships and they all drop loot, that's 10 loot to append to a list and if I pick up 1 loot how can I know which loot to remove from the list?
如果我射击了10艘战舰并且它们全部掉落了战利品,那么这10个战利品可以追加到列表中,如果我捡起1个战利品,我怎么知道要从列表中删除哪个战利品?
import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Destroyer')
clock = pygame.time.Clock()
FPS = 60
font = pygame.font.SysFont(None, 25)
#~~~~~~~~~~~~~~~~~~~Other Functions~~~~~~~~~~~~~~~~~#
def devMode(x, y, shipW, shipH, xcross, ycross, face):
devText = font.render("X: "+str(x) + " Y: "+str(y) + " shipW: "+str(shipW) + " shipH: "+str(shipH) + " xcross: "+str(xcross) + " ycross: "+str(ycross) + " Face: "+str(face), True, white)
gameDisplay.blit(devText,(0,display_height-17))
def money(count):
text = font.render("Asteroids: "+str(count),True ,white)
gameDisplay.blit(text,(0,0))
def rot_center(image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image,rot_rect
#~~~~~~~~~~~~~~~~~~~~~~The Game~~~~~~~~~~~~~~~~~~~~~#
def game_loop():
face = 1
asteroids = 0
fthrust = False
xcross = False
ycross = False
loot_spawnx = random.randrange(0,display_width)
loot_spawny = random.randrange(0,display_height)
loot_width = 20
loot_height = 20
x = (display_width * 0.45)
y = (display_height * 0.45)
x_change = 0
y_change = 0
shipspeed = 5
bulSpeed = 10
cooltime = 10000
duration = 5000
newGame = True
lastTime = 0
boost = True
cooldown = False
power = True
#Start of bullets
#1
lGun1 = -999
rGun1 = -999
yBul1 = 0
xBul1 = 0
fire1 = 0
b1 = False
ybul1_change = 0
xbul1_change = 0
#End of bullets
shipW = 69
shipH = 88
shipImg = pygame.image.load('ship1.png')
forwardthrust = pygame.image.load('ship1_forwardthrust.png')
reversethrust = pygame.image.load('ship1_reversethrust.png')
strafe = pygame.image.load('ship1_strafe.png')
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
#shipImg, newRect = rot_center(shipImg,oldRect,90)
#gameDisplay.blit(shipImg, newRect)
shipImg = pygame.transform.rotate(shipImg,90)
face -= 1
face = (face - 1) % 4
face += 1
if face == 2 or face == 4:
shipW = 88
shipH = 69
else:
shipW = 69
shipH = 88
elif event.key == pygame.K_RIGHT:
#shipImg, newRect = rot_center(shipImg,oldRect,-90)
#blit(shipImg, newRect)
shipImg = pygame.transform.rotate(shipImg,-90)
face -= 1
face = (face + 1) % 4
face += 1
if face == 2 or face == 4:
shipW = 88
shipH = 69
else:
shipW = 69
shipH = 88
elif event.key == pygame.K_UP:
if face == 1:
y_change = -shipspeed
x_change = 0
elif face == 2:
x_change = shipspeed
y_change = 0
elif face == 3:
y_change = shipspeed
x_change = 0
elif face == 4:
x_change = -shipspeed
y_change = 0
elif event.key == pygame.K_DOWN:
if face == 1:
y_change = shipspeed
x_change = 0
elif face == 2:
x_change = -shipspeed
y_change = 0
elif face == 3:
y_change = -shipspeed
x_change = 0
elif face == 4:
x_change = shipspeed
y_change = 0
elif event.key == pygame.K_s:
y_change = 0
x_change = 0
elif event.key == pygame.K_PAGEUP:
if face == 1:
x_change = -shipspeed
y_change = 0
elif face == 2:
y_change = -shipspeed
x_change = 0
elif face == 3:
x_change = shipspeed
y_change = 0
elif face == 4:
y_change = shipspeed
x_change = 0
elif event.key == pygame.K_PAGEDOWN:
if face == 1:
x_change = shipspeed
y_change = 0
elif face == 2:
y_change = shipspeed
x_change = 0
elif face == 3:
x_change = -shipspeed
y_change = 0
elif face == 4:
y_change = -shipspeed
x_change = 0
elif event.key == pygame.K_d:
if power == True:
power = False
if boost == True:
shipspeed = 10
start = pygame.time.get_ticks()
if now - start >= duration:
boost = False
shipspeed = 5
cooldown = True
lastTime = pygame.time.get_ticks()
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 = False
if y < loot_spawny+loot_height and y > loot_spawny or y + shipH < loot_spawny+loot_height and y + shipH > loot_spawny:
ycross = True
if x < loot_spawnx +loot_width and x > loot_spawnx or x + shipW > loot_spawnx and x + shipW < loot_spawnx + loot_width:
loot_spawnx = random.randrange(0,display_width)
loot_spawny = random.randrange(0,display_height)
asteroids += 10
xcross = True
elif x + shipW > loot_spawnx + loot_width and x < loot_spawnx:
loot_spawnx = random.randrange(0,display_width)
loot_spawny = random.randrange(0,display_height)
asteroids += 10
xcross = True
else:
xcross = False
else:
ycross = False
if x > loot_spawnx and x < loot_spawnx + loot_width or x + shipW > loot_spawnx and x + shipW < loot_spawnx + loot_width:
xcross = True
if y < loot_spawny+loot_height and y > loot_spawny or y + shipH < loot_spawny+loot_height and y + shipH > loot_spawny:
loot_spawnx = random.randrange(0,display_width)
loot_spawny = random.randrange(0,display_height)
asteroids += 10
ycross = True
elif y + shipH > loot_spawny + loot_height and y < loot_spawny:
loot_spawnx = random.randrange(0,display_width)
loot_spawny = random.randrange(0,display_height)
asteroids += 10
ycross = True
else:
ycross = False
else:
xcross = False
if y + shipH > loot_spawny + loot_height and y < loot_spawny:
ycross = True
if x + shipW > loot_spawnx + loot_width and x < loot_spawnx:
xcross = True
loot_spawnx = random.randrange(0,display_width)
loot_spawny = random.randrange(0,display_height)
asteroids += 10
else:
xcross = False
else:
ycorss = False
now = pygame.time.get_ticks()
if cooldown == True:
nowTime = pygame.time.get_ticks()
if nowTime - lastTime >= cooltime:
lastTime = nowTime
cooldown = False
boost = True
power = True
pygame.display.update()
gameDisplay.fill(green)
y += y_change
x += x_change
if x <= -5:
x += 5
elif x + shipW >= display_width:
x -= 5
elif y <= -5:
y += 5
elif y + shipH >= display_height:
y -= 5
#oldRect = shipImg.get_rect(center=(x + (shipW/ 2), y + (shipH / 2)))
#gameDisplay.blit(shipImg, oldRect)
gameDisplay.blit(shipImg, (x, y))
## if event.type == pygame.KEYDOWN:
## if event.key == pygame.K_UP:
## gameDisplay.blit(forwardthrust, (x + 19, y + shipH))
## gameDisplay.blit(forwardthrust, (x + 42, y + shipH))
#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])
#1
yBul1 += ybul1_change
xBul1 += xbul1_change
pygame.draw.rect(gameDisplay, blue, [loot_spawnx, loot_spawny, loot_width, loot_height])
money(asteroids)
devMode(x, y, shipW, shipH, xcross, ycross, face)
pygame.display.update()
clock.tick(FPS)
game_loop()
pygame.quit()
quit()
If anyone knows of a better way of doing this other than appending to lists then i am all ears. 如果有人知道除了添加列表之外还有更好的方法,那么我无所不能。
There are endless possibilities here, You just need to somehow identify your ship with the loot. 这里有无穷的可能性,您只需要以某种方式识别战利品即可。
I am hoping all ships are different carrying different loots. 我希望所有运载不同劫掠物的船只都是不同的。 Like big, small.
像大,小。
So let's say 所以说
>>> ships = [5, 67, 3, 10]
>>> loot = [100, 30, 300, 50]
so say my Ship No. 67 dies. 所以说我的67号船死了 You can even use names here 'a', 'b'.
您甚至可以在这里使用名称“ a”,“ b”。 We will remove the ship and it's loot.
我们将拆除这艘船,并对其进行抢劫。
First let's store it's index before removing it. 首先,在删除索引之前先存储它的索引。
>>> index = ships.index(67)
Now let's remove it. 现在,将其删除。
>>> ships.remove(67)
Finally remove the loot. 最后删除战利品。 Previously using remove method we remove the exact element.
以前使用remove方法,我们删除了确切的元素。 Here using del we give the index which needs to be removed.
在这里,使用del给出需要删除的索引。
>>> del loot[index]
>>> ships
[5, 3, 10]
>>> loot
[100, 300, 50]
Here you can also increase/decrease loot, say ship doesn't die but has only dropped some loot. 在这里,您还可以增加/减少战利品,比如说船只不会死,只是掉落了一些战利品。
But ideally you should make a dictionary of {shipNo:loot} which would be much easier, with better access times. 但是理想情况下,您应该制作一个{shipNo:loot}的字典,这样会更容易,并且访问时间更短。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.