如果我在一个列表中添加了多个相同的东西,并且我想去掉其中的一个,那么我怎么知道它是哪一个呢?

2024-06-18 11:22:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个小游戏。我控制一艘船,我射击其他小型敌舰。他们会把赃物扔给我捡起来。如果我射杀了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()

如果有人知道一个更好的方法做这件事,而不是附加到名单,那么我洗耳恭听。你知道吗


Tags: trueifdisplaychangewidthpygamefaceheight
1条回答
网友
1楼 · 发布于 2024-06-18 11:22:17

这里有无限的可能性,你只需要以某种方式确定你的船与战利品。你知道吗

我希望所有的船都能携带不同的战利品。像大的,小的。你知道吗

所以我们说

>>> 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]

在这里你也可以增加/减少战利品,比如说飞船没有死亡,但是只掉落了一些战利品。你知道吗

但理想的情况是你应该编一本{船号:战利品}这将更容易,有更好的访问时间。你知道吗

相关问题 更多 >