猿问

碰撞发生时如何显示爆炸图像?

不知道为什么没有爆炸效果。

其他图都画得很好,唯独爆炸图不行


explosion = pygame.image.load(os.path.join(image_path, "explosion.png"))

explosion_size = explosion.get_rect().size

explosion_width = explosion_size[0]

for missile_idx, missile_val in enumerate(missiles):

    missile_pos_x = missile_val[0]

    missile_pos_y = missile_val[1]


    #weapon information upgrade

    missile_rect = missile.get_rect()

    missile_rect.left = missile_pos_x

    missile_rect.top = missile_pos_y


    if missile_rect.colliderect(rock_rect):

        explosion_sound.play()

        **explosion_pos_x = missile_pos_x

        explosion_pos_y = missile_pos_y

        screen.blit(explosion,(explosion_pos_x,explosion_pos_y))**

        del(rock)

        del(missiles)

        missiles = []


        # missile position coordination

        missiles = [[m[0], m[1] - missile_speed] for m in missiles]


        # top missile elimination

        missiles = [[m[0], m[1]] for m in missiles if m[1]>0]


守候你守候我
浏览 109回答 2
2回答

森林海

爆炸只显示了一小会儿。用于pygame.time.get_ticks()返回自调用以来的毫秒数pygame.init()。计算必须删除爆炸图像之后的时间点。将爆炸的坐标和结束时间点添加到列表的头部 ( explosionList )。在主应用程序循环中绘制爆炸。从列表尾部删除过期的爆炸:explosionList = []while run:    current_time = pygame.time.get_ticks()    # [...]    for missile_idx, missile_val in enumerate(missiles)        # [...]        if missile_rect.colliderect(rock_rect):            explosion_sound.play()            explosion_pos_x = missile_pos_x            explosion_pos_y = missile_pos_y            end_time = current_time + 2000 # 2000 milliseconds = 2 seconds            explosionList.insert(0, (end_time, explosion_pos_x, explosion_pos_y))            # [...]    for i in range(len(explosionList)):        if current_time < explosionList[i][0]:            screen.blit(explosion, (explosionList[i][1], explosionList[i][2]))        else:            explosionList = explosionList[:i]            break        # [...]使用该算法可以管理多次爆炸。import pygamepygame.init()window = pygame.display.set_mode((210, 210))def create_rectangles():    global rectangles    w, h = window.get_size()    rectangles = []    for x in range(0, w - 60, 60):        for y in range(0, h - 60, 60):            rectangles.append(pygame.Rect(x + 30, y + 30, 30, 30))create_rectangles()hit_list = []fade_out_time = 3000run = Truewhile run:    current_time = pygame.time.get_ticks()        for event in pygame.event.get():        if event.type == pygame.QUIT:            run = False    point = pygame.mouse.get_pos()    collideindex = pygame.Rect(point, (1, 1)).collidelist(rectangles)    if collideindex >= 0:        end_time = current_time + fade_out_time        hit_list.insert(0, (end_time, rectangles[collideindex].center))        del rectangles[collideindex]    if not hit_list and not rectangles:        create_rectangles()    window.fill(0)    for r in rectangles:        pygame.draw.rect(window, (255, 0, 0), r)    for i in range(len(hit_list)):        delta_time = hit_list[i][0] - current_time        if delta_time > 0:            radius = round(30 * delta_time / fade_out_time)            pygame.draw.circle(window, (255, 255, 0), hit_list[i][1], radius)        else:            hit_list = hit_list[:i]            break    pygame.display.flip()

智慧大石

如果您想显示爆炸图像一段时间,此方法可能很有用。explosion_list = [] # list for explosion effectrunning = Truewhile running:&nbsp; &nbsp; &nbsp;start_time = pygame.time.get_ticks() # add time for explosion effect&nbsp; &nbsp; &nbsp;for missile_idx, missile_val in enumerate(missiles):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missile_pos_x = missile_val[0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missile_pos_y = missile_val[1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#weapon information upgrade&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missile_rect = missile.get_rect()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missile_rect.left = missile_pos_x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missile_rect.top = missile_pos_y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if missile_rect.colliderect(rock_rect):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;explosion_sound.play()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;explosion_pos_x = missile_pos_x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;explosion_pos_y = missile_pos_y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;explosion_time = start_time + 2000 # 1000 milliseconds = 2 seconds&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**explosion_list.insert(0, (explosion_time, explosion_pos_x, explosion_pos_y))**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;del(rock)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;del(missiles)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missiles = []&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# missile position coordination&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missiles = [[m[0], m[1] - missile_speed] for m in missiles]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# top missile elimination&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missiles = [[m[0], m[1]] for m in missiles if m[1]>0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**for i in range(len(explosion_list)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if start_time < explosion_time:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; screen.blit(explosion, (explosion_pos_x - 30, explosion_pos_y - 30))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pygame.time.delay(10)**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;explosion_list = explosion_list[:i]
随时随地看视频慕课网APP

相关分类

Python
我要回答