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,并且会出现更多故障。如果您需要我尝试更好地解释它,我很乐意。只是想学习。

完整代码在这里: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)

每当您发现自己在其中创建带有数字的变量时,这都是代码异味,这意味着您可能应该使用不同的数据类型。

正如已经建议的那样,您可能想要查看 sprite 和 group 模块,但这至少可以完成您目前尝试做的事情。

于 2012-05-19T05:01:11.360 回答