如何处理pygame中不同组件的时间

我正在制作一个 pygame 游戏,一个人可以从商店购买炸弹。玩家也可以投下他购买的炸弹。我需要一种方法让每个炸弹在投下 3 秒后消失。在下面的代码中,我只能投下炸弹,但是我尝试了各种方法但都失败了。


import pygame

import random


pygame.font.init()


width = 900

height = 600


screen = pygame.display.set_mode([width, height])


walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),

             pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'),

             pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]


walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),

            pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'),

            pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]


char = pygame.image.load('standing.png')

bomb_pic = pygame.transform.scale(pygame.image.load('bomb.png'), (20,20))

bomb_explosion = pygame.transform.scale(pygame.image.load('explosion1.png'), (40,40))

# char_rect = char.get_rect()



enemy_Left = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'),

            pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'),

            pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png')] 




x = 50

y = 50

width = 40

height = 60

vel = 5

isJump = False

jumpCount = 10

left = False

right = False

down = False

up = False

walkCount = 0


enemy_vel = 2

enemy_list = []


shop = pygame.transform.scale(pygame.image.load("shop.png"), (60, 60))


clock = pygame.time.Clock()

FPS = 60


font = pygame.font.Font('freesansbold.ttf', 32)

items_font = pygame.font.Font('freesansbold.ttf', 16)


bombs =[]

bag = {'bomb': 0}

print(bag["bomb"])


森林海
浏览 140回答 1
1回答

慕容森

用于pygame.time.get_ticks()获取当前时间(以毫秒为单位)。计算炸弹必须消失的时间。将时间存储到列表中bombs。该列表bombs必须包含位置和时间的元组。如果时间已到,则从列表中删除炸弹:def redrawGameWindow():    current_time = pygame.time.get_ticks()    # [...]    for i in reversed(range(len(bombs))):        pos, end_time = bombs[i]        if current_time > end_time            bombs.pop(i)        else:            screen.blit(bomb_pic, pos)def main():    # [...]    while run:        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 event.key == pygame.K_SPACE and bag["bomb"] >= 1:                    pos = x + char.get_width()/2, y + char.get_height() - 20                    end_time = current_time + 3000 # 3000 milliseconds = 3 seconds                    bombs.append((pos, end_time))                    bag["bomb"] -= 1        redrawGameWindow()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python