问题描述
我一直在尝试在 pygame 中制作 Space Invaders 克隆版。我决定这样写,这样从玩家船上射出的子弹只有在离开屏幕时才能再次发射,但我没有能力这样做。我该怎么做?
import pygame
pygame.init()
screen_size = (500,500)
ship_size = (50,50)
x,y = 250,450
x_rect,y_rect = x + 15,y - 20
height_rect,width_rect = 20,20
vel = 50
shoot = False
"""Loads screen and set gives it a title."""
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Space Invaders")
"""Initializes images for the game and resizes them."""
space_ship = pygame.image.load("Space Invaders Ship.jpg")
space_ship = pygame.transform.scale(space_ship,ship_size)
space = pygame.image.load("Space.jpg")
space = pygame.transform.scale(space,screen_size)
clock = pygame.time.Clock()
run = True
while run:
"""Controls the fps."""
clock.tick(60)
"""Registers keyboard's input."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and x > 0:
x -= vel
elif event.key == pygame.K_RIGHT and x + 50 < 500:
x += vel
elif event.key == pygame.K_SPACE:
x_rect,y - 20
shoot = True
"""Constantly draws on the screen."""
screen.fill((0,0))
screen.blit(space,(0,0))
screen.blit(space_ship,[x,y])
"""Shoots a bullet from where the ship currently is."""
if shoot:
pygame.draw.rect(screen,(250,250,0),[x_rect,y_rect,height_rect,width_rect])
y_rect -= 5
elif y_rect + width_rect > 0:
shoot = False
y_rect = y - 20
pygame.display.flip()
解决方法
您必须创建一个项目符号列表:
bullet_list = []
在按下 SPACE 时向列表添加新的项目符号位置:
elif event.key == pygame.K_SPACE:
bullet_list.append([x + 15,y - 20])
循环移动并绘制列表中的项目符号:
for bullet in bullet_list:
pygame.draw.rect(screen,(250,250,0),[*bullet,height_rect,width_rect])
bullet[1] -= 5
如果 y 坐标小于 0,则从列表中删除项目符号:
for bullet in bullet_list[:]:
if bullet[1] < 0:
bullet_list.remove(bullet)
完整示例:
import pygame
pygame.init()
screen_size = (500,500)
ship_size = (50,50)
x,y = 250,450
x_rect,y_rect = x + 15,y - 20
height_rect,width_rect = 20,20
vel = 50
bullet_list = []
"""Loads screen and set gives it a title."""
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Space Invaders")
"""Initializes images for the game and resizes them."""
space_ship = pygame.image.load("Space Invaders Ship.jpg")
space_ship = pygame.transform.scale(space_ship,ship_size)
space = pygame.image.load("Space.jpg")
space = pygame.transform.scale(space,screen_size)
clock = pygame.time.Clock()
run = True
while run:
"""Controls the fps."""
clock.tick(60)
"""Registers keyboard's input."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and x > 0:
x -= vel
elif event.key == pygame.K_RIGHT and x + 50 < 500:
x += vel
elif event.key == pygame.K_SPACE:
bullet_list.append([x + 15,y - 20])
"""Constantly draws on the screen."""
screen.fill((0,0))
screen.blit(space,(0,0))
screen.blit(space_ship,[x,y])
"""Shoots a bullet from where the ship currently is."""
for bullet in bullet_list:
pygame.draw.rect(screen,width_rect])
bullet[1] -= 5
for bullet in bullet_list[:]:
if bullet[1] < 0:
bullet_list.remove(bullet)
pygame.display.flip()