Python Pygame:为太空入侵者游戏发射多发子弹



我正在尝试使用Pygame制作一款太空入侵者游戏。然而,我很难弄清楚如何让宇宙飞船连续发射多发子弹,并让子弹随着它移动。我真正让程序发射多发弹头的唯一方法是通过for loop,尽管一旦for loop击中它的末端,子弹就会停止发射。我应该创建一个列表来存储所有项目符号吗?感谢任何帮助:D。

以下是我到目前为止的Python代码(它只有发射一颗子弹的宇宙飞船)。

from __future__ import print_function
import pygame
import os, sys
from pygame.locals import *
x_location = 357
y_location = 520
bullet_location_x = x_location + 35
bullet_location_y = y_location - 5
def load_image(path, colorkey): # loads an image given the file path
    try:
        image = pygame.image.load(path)
    except pygame.error, message:
        print("Cannot load image: {0}".format(path)) # print out error message
    image = image.convert() # convert image so that it can be displayed properly
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0, 0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect() # Return the image and the image's rectangular area
def main():
    global x_location, y_location, bullet_location_x, bullet_location_y
    pygame.init()
    background_color = (0, 0, 0) # green background
    width, height = (800, 600) # width and height of screen
    screen = pygame.display.set_mode((width, height)) # set width and height
    pygame.display.set_caption('space invaders') # set title of game
    clock = pygame.time.Clock() # create the clock
    spaceship_img, spaceship_rect = load_image("spaceship.png", (0, 0, 0))
    bullet_img, bullet_rect = load_image("bullet.png", (0, 0, 0))
    while True:
        screen.fill(background_color) # make the background green
        ##############################################################
        # DISPLAY BULLET                                             #
        ##############################################################
        screen.blit(bullet_img, (bullet_location_x, bullet_location_y))
        # Render the images
        screen.blit(spaceship_img, (x_location, y_location))
        keys = pygame.key.get_pressed() # get the keysnpressed
        for event in pygame.event.get(): # check the events
            if event.type == pygame.QUIT: # if the user presses quit
                pygame.quit() # quit pygame
                sys.exit() # terminate the process
            if event.type == KEYDOWN:
                if event.key == K_LEFT:
                    screen.fill(background_color)
                    x_location -= 5
                    screen.blit(spaceship_img, (x_location, y_location))
                if event.key == K_RIGHT:
                    screen.fill(background_color)
                    x_location += 5
                    screen.blit(spaceship_img, (x_location, y_location))
                if event.key == K_UP:
                    screen.blit(bullet_img, (bullet_location_x, bullet_location_y))
                display_bullets = True
        pygame.display.flip() # refresh the pygame window
        clock.tick(60) # Makes the game run at around 60 FPS
        bullet_location_y -= 5
        pass
if __name__ == '__main__':
    main()

您应该将游戏分成一个包含3个不同功能的循环:

LOOP
    UPDATE_LOGIC
    INPUT_HANDLING
    DRAW_ELEMENTS
END-LOOP

input_handling中,如果玩家未处于冷却状态,则将项目符号放入项目符号列表中。

update_logic中,迭代项目符号列表,计算玩家的位置和冷却时间。

draw_elements中,只需绘制场景中的每一个东西。(迭代项目符号列表)

并且可以使用不同的计时器进行绘制和更新逻辑。这样可以帮助您轻松地在游戏中添加新元素。

最新更新