задержать событие в python / pygame - PullRequest
0 голосов
/ 08 мая 2018

Я новичок в Python и Pygame, и я создаю игру, в которой задействован танк, который может стрелять, я заставил его работать, однако пуля настолько быстрая, что больше похожа на лазер, и я просто хочу отпустить медленно по экрану

import pygame

pygame.init()

# window size
windowWidth = 850
windowHeight = 600

# displaying the main window
gameDisplay = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption("TANKS")
clock = pygame.time.Clock()

# all colors i could use (currently don't use them all)
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
darkRed = (150, 0, 0)
darkGreen = (0, 150, 0)
darkBlue = (0, 0, 150)

# player's body variables that don't change (color and size)
playerColor = darkGreen
playerWidth = 25
playerHeight = 25

# barrel variables that don't change (Horizontal and Vertical sizes of the barrel [H/V])
barrelWidthV = 5
barrelHeightV = playerHeight + barrelWidthV

barrelHeightH = 5
barrelWidthH = playerWidth + barrelHeightH

# bullet variables that don't change (size)
bulW = 5
bulH = 5


# draw a body and barrel for the player
def player(playerX, playerY, barrelX, barrelY, W, H):
    pygame.draw.rect(gameDisplay, playerColor, (playerX, playerY, playerWidth, playerHeight))
    pygame.draw.rect(gameDisplay, darkRed, (barrelX, barrelY, W, H))


# draw a bullet that can be shot by the player
def bullet(shotX, shotY):
    pygame.draw.rect(gameDisplay, black, (shotX, shotY, bulW, bulH))


# the game loop function with variables and the actual loop
def gameLoop():

    #  startingplayer variables that can change
    playerX = int(windowWidth * 0.1)
    playerY = int(windowHeight * 0.5)
    pSpeedX = 15
    pSpeedY = 15

    # starting barrel variables that can change
    barrelX = playerX + playerWidth / 2 - barrelWidthV / 2
    barrelY = playerY - barrelWidthV
    W = barrelWidthV
    H = barrelHeightV

    # bullet variables that can change
    shotX = barrelX
    shotY = barrelY
    bulletSpeed = 1

    # the actual game loop
    playing = True
    while playing:
        gameDisplay.fill(white)

        # if the game is exited actually exit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                playing = False

        # test if a key is pressed
        if event.type == pygame.KEYDOWN:
            # test if the key that is pressed is the spacebar
            if event.key == pygame.K_SPACE:
                # test if the bullet is still in the screen
                if 0 < shotY < windowHeight and 0 < shotX < windowWidth:
                    # test if the player is looking down
                    if barrelX == playerX + playerWidth/2 - barrelWidthV/2 and barrelY == playerY:
                        # shoot the bullet down until it flies of the screen
                        while 0 < shotY < windowHeight:
                            shotY + bulletSpeed
                            shotY += bulletSpeed
                            pygame.display.update(bullet(shotX, shotY))
                    # test if the player is looking up
                    if barrelX == playerX + playerWidth/2 - barrelWidthV/2 and barrelY == playerY - barrelWidthV:
                        # shoot the bullet up until it flies of the screen
                        while 0 < shotY < windowHeight:
                            shotY -= bulletSpeed
                            pygame.display.update(bullet(shotX, shotY))
                    # test if the player is looking right
                    if barrelX == playerX and barrelY == playerY + playerHeight/2 - barrelHeightH/2:
                        # shoot the bullet right until it flies of the screen
                        while 0 < shotX < windowWidth:
                            shotX += bulletSpeed
                            pygame.display.update(bullet(shotX, shotY))
                    # test if the player is looking left
                    if barrelX == playerX - barrelHeightH and barrelY == playerY + playerHeight / 2 - barrelHeightH / 2:
                        # shoot the bullet left until it flies of the screen
                        while 0 < shotX < windowWidth:
                            shotX -= bulletSpeed
                            pygame.display.update(bullet(shotX, shotY))
                # when the bullet flew off the screen return to the player
                else:
                    shotX = barrelX
                    shotY = barrelY

            # test if the player is at the bottom edge and if so can't move left anymore thus creating a border
            if playerY + barrelHeightV < windowHeight:
                if event.key == pygame.K_DOWN:
                    # make sure the bullet is with the player
                    shotX = barrelX
                    shotY = barrelY
                    # move the body, barrel and bullet simultaniously
                    playerY += pSpeedY
                    barrelY += pSpeedY
                    shotY += pSpeedY

                    # use the vertical barrel
                    W = barrelWidthV
                    H = barrelHeightV

                    # place the barrel on the body (sticking out at the bottom)
                    barrelX = playerX + playerWidth/2 - barrelWidthV/2
                    barrelY = playerY

            # test if the player is at the top edge and if so can't move left anymore thus creating a border
            if playerY - barrelWidthV > 0:
                if event.key == pygame.K_UP:
                    # make sure the bullet is with the player
                    shotX = barrelX
                    shotY = barrelY
                    # move the body, barrel and bullet simultaniously
                    playerY -= pSpeedY
                    barrelY -= pSpeedY
                    shotY -= pSpeedY

                    # use the vertical barrel
                    W = barrelWidthV
                    H = barrelHeightV

                    # place the barrel on the body (sticking out at the top)
                    barrelX = playerX + playerWidth/2 - barrelWidthV/2
                    barrelY = playerY - barrelWidthV

            # test if the player is at the right edge and if so can't move left anymore thus creating a border
            if playerX + barrelWidthH < windowWidth:
                if event.key == pygame.K_RIGHT:
                    # make sure the bullet is with the player
                    shotX = barrelX
                    shotY = barrelY
                    # move the body, barrel and bullet simultaniously
                    playerX += pSpeedX
                    barrelX += pSpeedX
                    shotX += pSpeedX

                    # use the horizontal barrel
                    W = barrelWidthH
                    H = barrelHeightH

                    # place the barrel on the body (sticking out at the right)
                    barrelX = playerX
                    barrelY = playerY + playerHeight/2 - barrelHeightH/2

            # test if the player is at the left edge and if so can't move left anymore thus creating a border
            if playerX - barrelWidthV > 0:
                if event.key == pygame.K_LEFT:
                    shotX = barrelX
                    shotY = barrelY
                    # move the body, barrel and bullet simultaniously
                    playerX -= pSpeedX
                    barrelX -= pSpeedX
                    shotX -= pSpeedX

                    # use the horizontal barrel
                    W = barrelWidthH
                    H = barrelHeightH

                    # place the barrel on the body (sticking out at the left)
                    barrelX = playerX - barrelHeightH
                    barrelY = playerY + playerHeight/2 - barrelHeightH/2

        # draw everything on the screen in the right order and go through the loop in 60 fps
        player(playerX, playerY, barrelX, barrelY, W, H)
        bullet(shotX, shotY)
        pygame.display.update()
        clock.tick(60)


# call the gameloop and when that ends close the game
gameLoop()
pygame.quit()

так есть ли способ замедлить пулю, чтобы у вас была устойчиво движущаяся пуля (не лазер)

Я создал как можно больше комментариев, сохраняя при этом удобочитаемость, поэтому за большим количеством кода легко следить.

p.s. Я хотел бы знать, как я могу сделать пулю одним кликом, пока игрок еще может двигаться, но это не моя главная проблема

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...