有谁知道我怎样才能做到,所以我的代码只能在空格键按下时才发射子弹呢?现在,任何键都会使我的角色弹出子弹,但如果可能的话,我想要更改它,下面是我的代码:
import pygame, sys, random
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode([screen_width, screen_height])
all_sprites_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
background = pygame.image.load('space.jpg')
pygame.display.set_caption("Alien Invasion!")
explosion = pygame.mixer.Sound('explosion.wav')
score = 0
class Block(pygame.sprite.Sprite):
def __init__(self, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("spaceship.png")
self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.x=0
self.y=0
self.image = pygame.image.load("alien.png")
self.rect = self.image.get_rect()
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos[0]
def render(self):
if (self.currentImage==0):
screen.blit(self.image, (self.x, self.y))
class Bullet(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("bullet.png")
self.rect = self.image.get_rect()
def update(self):
self.rect.y -= 3
for i in range(30):
block = Block(BLACK)
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(330)
block_list.add(block)
all_sprites_list.add(block)
player = Player()
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()
player.rect.y = 480
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
all_sprites_list.update()
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
explosion.play()
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
score += 10
if bullet.rect.y < -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), True, WHITE)
textpos = text.get_rect(centerx=screen.get_width()/12)
screen.blit(background, (0,0))
screen.blit(text, textpos)
all_sprites_list.draw(screen)
pygame.display.update()
clock.tick(80)
pygame.quit()此外,如果可能的话,我想让它,一旦退出键被按下,游戏退出。为此,我有以下代码:
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):然而,由于某些原因,它似乎无法正常工作:(我对python很陌生,所以任何帮助都会很感激!)
发布于 2014-03-25 18:12:03
正确的!所以首先要做的是!
要通过按空格键发射子弹,您需要添加以下内容:
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet) 以上解决方案将通过按空格键生成子弹。
如果您想通过按空格键来激发,那么您需要创建一个k_space变量,该变量将在上述事件中设置为True,并在循环结束时设置为False。
因此,要完成上述操作,您必须更改一些内容:)
k_space = False #---- Initialize the boolean for the space key here
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
k_space = True #----- Set it to true here
if k_space: #---- create a bullet when the k_space variable is set to True
bullet = Bullet()
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
all_sprites_list.add(bullet)
bullet_list.add(bullet)
k_space = False #------ Reset it here
all_sprites_list.update()
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
explosion.play()
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
score += 10
if bullet.rect.y < -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
font = pygame.font.Font(None, 36)
text = font.render("Score: " + str(score), True, WHITE)
textpos = text.get_rect(centerx=screen.get_width()/12)
screen.blit(background, (0,0))
screen.blit(text, textpos)
all_sprites_list.draw(screen)
pygame.display.update()
clock.tick(80)更改上述代码只会使每个空格键按下one子弹子程序!如果您希望通过按空格键生成多颗子弹,只需坚持我建议的第一个更改:)
最后,如果您希望通过按转义键关闭程序,那么只需更改循环中的第一个事件:
取而代之的是:
if event.type == pygame.QUIT:
done = True你可以把它改为:
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True应该安全地退出您的程序:)
希望能帮上忙!
干杯,
亚历克斯
(编辑)
好了!只需将两个退出事件分开,它就会顺利地工作:)
#code above
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
#code below最后:)如果您希望子弹完全从飞船中间产生,只需在k_space检查中添加以下代码:
if k_space:
bullet = Bullet()
bullet.rect.x = player.rect.center[0] - bullet.rect.width/2
bullet.rect.y = player.rect.center[1] - bullet.rect.height/2
all_sprites_list.add(bullet)
bullet_list.add(bullet)呼!希望能帮助伴侣:)
干杯!
https://stackoverflow.com/questions/22641328
复制相似问题