我正在制作一个小游戏。我控制一艘船,并射击其他小型敌舰。他们会掉落战利品让我捡起来。如果我射击了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()
如果有人知道一个更好的方式做这件事,而不是追加到列表,那么我洗耳恭听。
这里有无限的可能性,你只需要以某种方式将你的船与战利品联系起来。
我希望所有的船都是不同的携带不同的战利品。比如大的,小的。
我们输入
>>> ships = [5, 67, 3, 10]
>>> loot = [100, 30, 300, 50]
假设我的67号船死了。你甚至可以用名字,a, b。我们会把船移走,把它带走。
首先让我们在删除它之前存储它的索引。
>>> index = ships.index(67)
现在我们把它去掉。
>>> ships.remove(67)
最后移除战利品。以前使用remove方法,我们删除了确切的元素。这里使用del给出需要删除的索引。
>>> del loot[index]
>>> ships
[5, 3, 10]
>>> loot
[100, 300, 50]
这里你也可以增加/减少战利品,说船没有死,但只掉落了一些战利品。
但理想情况下,你应该制作一个{shipNo:loot}的字典,这将更容易,具有更好的访问时间。