0

I'm trying to blit an image to the screen when the event K_SPACE is triggered. However, when I press space nothing happens. I've tried moving where the screen is updated or flipped, but i've hit a brick wall.

Here is my code:

def Play():

    background = pygame.image.load(bifPlay).convert() 
    char = pygame.image.load(mif).convert_alpha()
    bullet = pygame.image.load(bulletLoad).convert()
    zombie = pygame.image.load(zombieLoad).convert_alpha()
    #converting images needed so pygame can use them
    x,y = 115,350
    movex, movey = 0,0
    zombieX = 1100
    zombieY = random.randint(0,675)
    bulletX = x+90
    bulletY = y+37.5
    clock = pygame.time.Clock()
    zombieSpeed = 100
    bulletSpeed = 200
    while True:

        milli = clock.tick()
        seconds = milli/1000.
        zomMovement = seconds*zombieSpeed
        bulletMovement = seconds*bulletSpeed
        zombieX-=zomMovement
        bulletX+=bulletMovement
        x += movex
        y += movey
        screen.blit(background,(0,0))
        screen.blit(char,(x,y))
        screen.blit(zombie,(zombieX,zombieY))
        spacePressed = pygame.key.get_pressed()

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
    #code above allows the user to exit - while program is running if event is exit
    #allows user to exit program.
            if event.type == KEYDOWN:
                if event.key == K_UP:
                    movey =-1
                elif event.key == K_DOWN:
                    movey =+1
                elif event.key == K_SPACE:
                    screen.blit(bullet,(bulletX,bulletY))
            if event.type == KEYUP:
                if event.key == K_UP:
                    movey =0
                elif event.key == K_DOWN:
                    movey =0

        if y == 675: 
            y -= 1
        if y == 0: 
            y +=1
        pygame.display.update()
4

2 回答 2

1

您的图像正在显示,但仅显示 1 次,然后被背景冲走。使用 clock.tick() 减慢你的循环,看看它是否正在显示。

于 2013-11-01T16:58:38.837 回答
1

既然你有 spacePressed,用它来画这样的子弹:

if spacePressed[K_SPACE]:
    screen.blit(bullet, (bulletx, bullety))

您也可以将 spacePressed 用于任何键。

于 2014-01-02T22:18:06.750 回答