1

わかりましたので、私は学校のためにこのプロジェクトに取り組んでいます。スペースインベーダー系のゲームを作ろうと思っています。船を動かして発砲するように仕上げました。ここに問題があります。マルチファイアしようとすると、以前に発射された弾丸が消去され、まったく良いサイトではない新しい弾丸が発射されます。実際に複数のショットを発射するにはどうすればよいですか?

while (running == 1):
    screen.fill(white)
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            running = 0
        elif (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_d):
                dir = "R"
                move = True
            elif (event.key == pygame.K_a):
                dir = "L"
                move = True
            elif (event.key == pygame.K_s):
                dir = "D"
                move = True
            elif (event.key == pygame.K_w):
                dir = "U"
                move = True
            elif (event.key == pygame.K_ESCAPE): 
                sys.exit(0)
            elif (event.key == pygame.K_SPACE):
                shot=True
                xbul=xgun + 18
                ybul=ygun
            #if key[K_SPACE]:
                #shot = True
        if (event.type == pygame.KEYUP):
            move = False

    #OBJECT'S MOVEMENTS
    if ((dir == "R" and xgun<460) and (move == True)):
        xgun = xgun + 5
        pygame.event.wait
    elif ((dir == "L" and xgun>0) and (move == True)):
        xgun = xgun - 5
        pygame.event.wait
    elif ((dir == "D" and ygun<660) and (move == True)):
        ygun = ygun + 5
        pygame.event.wait
    elif ((dir == "U" and ygun>0) and (move == True)):
        ygun = ygun - 5

    screen.blit(gun, (xgun,ygun))

    #PROJECTILE MOTION
    #key = pygame.key.get_pressed()

    if shot == True:
        ybul = ybul - 10
        screen.blit(bullet, (xbul, ybul))

    if xbul>640:
        shot=False

    pygame.display.flip()
    time.sleep(0.012)
4

2 に答える 2

7

xbul と ybul という 1 つの箇条書きの変数しかありません。複数の箇条書きが必要な場合は、それぞれをリストにする必要があります。各リストに追加して新しい箇条書きを追加したり、ポップして古い箇条書きを削除したり、描画時にリストを反復処理したりできます。

于 2013-01-11T18:51:34.200 に答える
1

x 座標と y 座標、および弾丸に関連するその他のものを含む弾丸のクラスを作成できます。次に、発射ボタンを押すたびに、新しいインスタンスを作成してリストに追加します。このようにして、必要な数の弾丸を持つことができます。(新しい関数
のコードを変更) move

class Bullet:
    def __init__(self,x,y,vx,vy):# you can add other arguments like colour,radius
        self.x = x
        self.y = y
        self.vx = vx # velocity on x axis
        self.vy = vy # velocity on y axis
    def move(self):
        self.x += self.vx
        self.y += self.vy

list箇条書きの位置を使用および更新するために追加するコードの例(move()上にあります):

if shot == True: # if there are bullets on screen (True if new bullet is fired).
    if new_bullet== True: # if a new bullet is fired
        bullet_list.append(Bullet(n_x,n_y,0,10)) # n_x and n_y are the co-ords
                                                 # for the new bullet.

    for bullet in bullet_list:
        bullet.move()
于 2013-01-12T07:03:14.197 に答える