0

私は初心者プログラマーで、母国語はPython 2.7です。スペースインベーダータイプのゲームを作ろうとしていますが、一度に複数の弾丸を持ちたいのですが、その方法がわかりません。だから私はここで私自身の方法を作りましたあなたがそれを見た後に私のコードはもっと説明しません

        if event.type ==pygame.KEYDOWN and event.key==K_SPACE:
            if m < 5:
                m+=1

            if m==1:
                m1a=1
                m1x=ls

            if m==2:
                m2a=1
                m2x=ls

            if m==3:
                m3a=1
                m3x=ls

            if m==4:
                m4a=1
                m4x=ls

            if m==5:
                m5a=1
                m5x=ls

            print m
#missle 1
    if m1a==1:
        screen.blit(rship,(m1x,m1))
        if m1>=0:
            m1-=1
        else:
            m-=1
            m1a=0
            m1=460
#missle 2
    if m2a==1:
        screen.blit(rship,(m2x,m2))
        if m2>=0:
            m2-=1
        else:
            m-=1
            m2a=0
            m2=460
#missle 3
    if m3a==1:
        screen.blit(rship,(m3x,m3))
        if m3>=0:
            m3-=1
        else:
            m-=1
            m3a=0
            m3=460
#missle 4
    if m4a==1:
        screen.blit(rship,(m4x,m4))
        if m4>=0:
            m4-=1
        else:
            m-=1
            m4a=0
            m4=460
#missle 5
    if m5a==1:
        screen.blit(rship,(m5x,m5))
        if m5>=0:
            m5-=1
        else:
            m-=1
            m5a=0
            m5=460

笑えるほどおとなしいとは思いますが、私はただ学んでいますが、問題は、1発目と2発目のミサイルが3発目以降は問題ないということです。3番目を発射すると、2番目が射撃元の場所に移動します。もう一度ファーを押すと、コードは1に戻りませんが、2のままになり、さらに多くのグリッチが発生します。あなたが私にそれをもっとよく説明しようとする必要があるなら、私は喜んでそうします。ただ学ぼうとしています。

ここに完全なコード:pastebin.com/FnPaME6N

4

2 に答える 2

1

「弾丸」スプライトを作成してから、または何かと呼ばれるグループに追加する必要がありbulletsます。グループでメソッドを呼び出すと、updateすべての箇条書きが一度に更新されます。

于 2012-05-19T05:57:30.240 に答える
0

ここにいくつかの実用的なコードがありますが、素晴らしいとは言えません。

import pygame
import math
from pygame.locals import *
background_colour = (122, 100, 155)
(width, height) = (500, 500)

pygame.init()

#ship = pygame.image.load('lolol.jpeg')\
rship = pygame.image.load('js.jpg')
mis = pygame.image.load('lot.jpg')
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('A.N.T.O.N.I.A.')
screen.fill(background_colour)
pygame.display.flip()
running = True

MAX_MISSILES = 5

ls = 250  # Horizontal ship location

LEFT = False
RIGHT = False

def move(ls, RIGHT, LEFT):
    '''Moves the ship 3 pixels to the left or right.

    Only moves if just one direction key is down, and not both.
    Also, will not move if the ship is at either horizontal screen edge.

    ls is the ship location.

    '''
    if LEFT and not RIGHT:
        if ls >= 10:
            ls -= 3
    elif RIGHT and not LEFT:
        if ls <= 440:
            ls += 3
    return ls

def fire_missile(ls, missiles):
    '''Fire a missile, as long as we are not at the maximum number.

    We use a list to hold our missile locations.

    '''
    if len(missiles) >= MAX_MISSILES:
        return
    else:
        missiles.append((ls, 460))

def draw_missiles(missiles):
    '''Draw all the missiles.'''
    if missiles:
        for missile in missiles:
            screen.blit(mis, (missile))

def move_missiles(missiles):
    '''If there are any missiles, move them up 1 pixel, and append
    them to the newlist.  The new list will replace the old list.

    '''
    if missiles:
        newmissiles = []
        for missile in missiles:
            # Do not append the missile to the new list if it is going
            # off the screen
            if missile[1] > 0:
                newmissiles.append((missile[0], missile[1] - 1))
        return newmissiles
    else:
        return missiles

missiles = []

while running:
    screen.blit(rship, (ls, 450))
    pygame.display.flip()
    screen.fill(background_colour)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False
        if event.type == pygame.KEYDOWN and event.key == K_ESCAPE:
            # You can now quit with the escape key.
            pygame.quit()
            running = False
        if event.type == pygame.KEYDOWN and event.key == K_LEFT:
            LEFT = True
        # LEFT is True untli you let up in the LEFT key
        if event.type == pygame.KEYUP and event.key == K_LEFT:
            LEFT = False
        if event.type == pygame.KEYDOWN and event.key == K_RIGHT:
            RIGHT = True
        if event.type == pygame.KEYUP and event.key == K_RIGHT:
            RIGHT = False
        if event.type == pygame.KEYDOWN and event.key == K_SPACE:
            fire_missile(ls, missiles)
    ls = move(ls, RIGHT, LEFT)
    draw_missiles(missiles)
    missiles = move_missiles(missiles)

数値を含む変数を作成していることに気付くときはいつでも、それはコードの匂いであり、おそらく別のデータ型を使用する必要があることを意味します.

示唆されているように、おそらくスプライトとグループのモジュールを調べたいと思うでしょうが、これは少なくともこれまでにやろうとしていたことを行います.

于 2012-05-19T05:01:11.360 に答える