我正在用Python 2.7制作自己的太空射击/侵略者游戏,我想使用空格键使其发射子弹。谁能帮我写出射击子弹的代码? (下面的代码只是移动了船,而没有让它离开屏幕)
代码:
# Starting point
x, y = 250, 650
MOVE_RIGHT = 1
MOVE_LEFT = 2
direction = 0
circle_radius = 7
circle_pos = (int(x), int(y))
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# movement
if event.type == KEYDOWN:
if event.key == K_LEFT:
direction = MOVE_LEFT
elif event.key == K_RIGHT:
direction = MOVE_RIGHT
elif event.type == KEYUP:
if event.key == K_LEFT:
direction = 0
elif event.key == K_RIGHT:
direction = 0
if(direction == MOVE_LEFT):
x-=0.3
elif(direction == MOVE_RIGHT):
x+=0.3
# out of boundaries
if x > 517 - (circle_radius * 2):
x = -x
x = 517 - (circle_radius * 2)
elif x < 0:
x = -x
x = 0
screen.blit(background, (0, 0))
screen.blit(player, (x, y))
pygame.display.update()