【问题标题】:Python Pygame: Fire multiple bullets for Space Invader gamePython Pygame:为 Space Invader 游戏发射多个子弹
【发布时间】:2016-07-18 20:12:52
【问题描述】:

我正在尝试使用 Pygame 制作 Space Invaders 游戏。但是,我无法弄清楚如何让宇宙飞船连续发射多发子弹并让子弹随之移动。我实际上使程序发射多发子弹的唯一方法是通过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()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

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

    LOOP
        UPDATE_LOGIC
        INPUT_HANDLING
        DRAW_ELEMENTS
    END-LOOP
    

    input_handling中,如果玩家未处于冷却状态,则将子弹放入子弹列表中。

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

    draw_elements 中,只需绘制场景中的所有内容。 (遍历项目符号列表)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多