栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

How do I stop more than 1 bullet firing at once?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

How do I stop more than 1 bullet firing at once?

The states which are returned by

pygame.key.get_pressed()

are, set, as long a key is hold down. That is useful for the movement of a
player. The player keeps moving as long a key is hold down.
But it contradicts your intention, when you want to fire a bullet. If you want
to fire a bullet when a key is pressed, then can use the
KEYDOWN
event. The event
occurs only once when a key is pressed:

while run == True:    clock.tick(27)    for event in pygame.event.get():        if event.type == pygame.QUIT: run = False        if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE:      if player1.left == True:   ## handles the direction of the bullet         facing = -1     else:         facing = 1       if len(bullets) < 5:    ## max amounts of bullets on screen         bx, by = player1.x + player1.width //2 ,player1.y + player1.height//2         bullets.append(projectile(bx, by, 6, black, facing))    # [...]

If you want to implement some kind of rapid fire, then the things get more
tricky. If you would use the state of

pygame.key.get_pressed()
then you
would spawn one bullet in every frame. That is far too fast. You have to
implement some timeout.
When a bullet is fired, the get the current time by
pygame.time.get_ticks()
. Define
a number of milliseconds for the delay between to bullets. Add the dela to the
time and state the time in a variable (
next_bullet_threshold
). Skip bullets,
as long the time is not exceeded:

next_bullet_threshold = 0run = Truewhile run == True:    # [...]    current_time = pygame.time.get_ticks()    if keys[pygame.K_SPACE] and current_time > next_bullet_threshold:        bullet_delay = 500 # 500 milliseconds (0.5 seconds)        next_bullet_threshold = current_time + bullet_delay        if player1.left == True:   ## handles the direction of the bullet facing = -1        else: facing = 1          if len(bullets) < 5: bx, by = player1.x + player1.width //2 ,player1.y + player1.height//2 bullets.append(projectile(bx, by, 6, black, facing))

Minimal example:

[![](https://i.stack.imgur.com/5jD0C.png)repl.it/@Rabbid76/PyGame-ShootBullet](https://repl.it/@Rabbid76/PyGame-ShootBullet#main.py)

import pygamepygame.init()window = pygame.display.set_mode((500, 200))clock = pygame.time.Clock()tank_surf = pygame.Surface((60, 40), pygame.SRCALPHA)pygame.draw.rect(tank_surf, (0, 96, 0), (0, 00, 50, 40))pygame.draw.rect(tank_surf, (0, 128, 0), (10, 10, 30, 20))pygame.draw.rect(tank_surf, (32, 32, 96), (20, 16, 40, 8))tank_rect = tank_surf.get_rect(midleft = (20, window.get_height() // 2))bullet_surf = pygame.Surface((10, 10), pygame.SRCALPHA)pygame.draw.circle(bullet_surf, (64, 64, 62), bullet_surf.get_rect().center, bullet_surf.get_width() // 2)bullet_list = []max_bullets = 4next_bullet_time = 0bullet_delta_time = 200 # millisecondsrun = Truewhile run:    clock.tick(60)    current_time = pygame.time.get_ticks()    for event in pygame.event.get():        if event.type == pygame.QUIT: run = False        if event.type == pygame.KEYDOWN: if len(bullet_list) < max_bullets and current_time >= next_bullet_time:     next_bullet_time = current_time + bullet_delta_time     bullet_list.insert(0, tank_rect.midright)    for i, bullet_pos in enumerate(bullet_list):        bullet_list[i] = bullet_pos[0] + 5, bullet_pos[1]        if bullet_surf.get_rect(center = bullet_pos).left > window.get_width(): del bullet_list[i:] break    window.fill((224, 192, 160))    window.blit(tank_surf, tank_rect)    for bullet_pos in bullet_list:        window.blit(bullet_surf, bullet_surf.get_rect(center = bullet_pos))    pygame.display.flip()pygame.quit()exit()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号