Python Pygame:为Space Invader游戏发射多个子弹

时间:2016-07-18 20:12:52

标签: 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()

1 个答案:

答案 0 :(得分:0)

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

LOOP
    UPDATE_LOGIC
    INPUT_HANDLING
    DRAW_ELEMENTS
END-LOOP

input_handling 中,如果播放器未处于冷却状态,请将子弹放入子弹列表中。

update_logic 中,迭代子弹列表,计算玩家的位置和玩家的冷静时间。

draw_elements 中,只需绘制场景中的所有内容即可。 (遍历子弹列表)

您可以使用不同的计时器进行绘制和update_logic。这样可以帮助您轻松地为游戏添加新元素