Мерцающий снаряд - PullRequest
       6

Мерцающий снаряд

0 голосов
/ 28 апреля 2019

Я работал над проектом, в котором есть персонаж из загруженного изображения, который может стрелять снарядами.В этом случае мой персонаж - Танос, и я заставляю его запускать фиолетовый шарик энергии.Я установил его так, что когда я нажимаю «е», шар появляется и должен запуститься.Однако, когда я нажимаю «е», шар появляется и мерцает.Он запускается один раз, затем снова появляется в Таносе, все еще мерцая.Вот мой код: (Игнорируйте камни бесконечности, я еще не начал над ними работать, возможно, я неправильно вставил код, это мой первый пост здесь)

import pygame

pygame.init()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREY = (150, 150, 150)

FRAMERATE = 100

SCREENWIDTH = 800
SCREENHEIGHT = 800

using_power_blast = False

screenSize = (SCREENWIDTH, SCREENHEIGHT)

screen = pygame.display.set_mode(screenSize)

thanosHeight = 130
thanosWidth = 80

thanosImg = pygame.image.load("thanos.png")
thanosLeftImg = pygame.image.load("thanos_left.png")
thanosImg = pygame.transform.scale(thanosImg, (thanosWidth, thanosHeight))
thanosLeftImg = pygame.transform.scale(thanosLeftImg, (thanosWidth, thanosHeight))
thanos_x = 400
thanos_y = 400
thanos_vel = 2

thanos_orientation = "right"

time_stone = pygame.image.load("time_stone.png")
power_stone = pygame.image.load("power_stone.png")
space_stone = pygame.image.load("space_stone.png")
mind_stone = pygame.image.load("mind_stone.png")
soul_stone = pygame.image.load("soul_stone.png")
reality_stone = pygame.image.load("reality_stone.png")

p_width = 100
p_height = 100
p_x = thanos_x
p_y = thanos_y
p_vel = 5

power_blast = pygame.image.load("power_blast_round.png")
power_blast = pygame.transform.scale(power_blast, (p_width, p_height))


def thanos(x, y):
    global thanos_x
    global thanos_y
    x = thanos_x
    y = thanos_y
    if thanos_orientation == "right":
        screen.blit(thanosImg, (x, y))
    if thanos_orientation == "left":
        screen.blit(thanosLeftImg, (x, y))


running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if using_power_blast:
        screen.blit(power_blast, (thanos_x, thanos_y))
        if thanos_orientation == "left":
            p_x -= p_vel
            screen.blit(power_blast, (p_x, p_y))
            pygame.display.update()
        if thanos_orientation == "right":
            p_x += p_vel
            pygame.display.update()

    screen.fill(GREY)
    thanos(thanos_x, thanos_y)
    pygame.display.update()

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a] and thanos_x > thanos_vel:
        thanos_x -= thanos_vel
        thanos_moving_left = True
        thanos_orientation = "left"
    if keys[pygame.K_d] and thanos_x < SCREENWIDTH - thanosWidth - thanos_vel:
        thanos_x += thanos_vel
        thanos_moving_right = True
        thanos_orientation = "right"
    if keys[pygame.K_w] and thanos_y > thanos_vel:
        thanos_y -= thanos_vel
    if keys[pygame.K_s] and thanos_y < SCREENHEIGHT - (thanosWidth + 45) - thanos_vel:
        thanos_y += thanos_vel
    if keys[pygame.K_e]:
        using_power_blast = True

    clock.tick(FRAMERATE)

pygame.quit()

1 Ответ

0 голосов
/ 28 апреля 2019

У вас должен быть только один update(), и вы должны бить все между fill() и updated()

fill() очищает буфер, а update() отправляет буфер на видеокарту и отображает его на экране.

Если вы моргаете до fill(), тогда fill() удалит его из буфера, и вы его не увидите. Но вы не должны использовать update() для решения этой проблемы.

Если вы используете update() перед тем, как скопировать все элементы в буфере, тогда он отобразит неполное изображение, а затем update() отобразит полное изображение, так что вы можете увидеть переворачивающиеся элементы.

while running:

    # --- events (without draws) ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_a] and thanos_x > thanos_vel:
        thanos_x -= thanos_vel
        thanos_moving_left = True
        thanos_orientation = "left"
    if keys[pygame.K_d] and thanos_x < SCREENWIDTH - thanosWidth - thanos_vel:
        thanos_x += thanos_vel
        thanos_moving_right = True
        thanos_orientation = "right"
    if keys[pygame.K_w] and thanos_y > thanos_vel:
        thanos_y -= thanos_vel
    if keys[pygame.K_s] and thanos_y < SCREENHEIGHT - (thanosWidth + 45) - thanos_vel:
        thanos_y += thanos_vel
    if keys[pygame.K_e]:
        # is it new power blast ?
        if using_power_blast is False:
            # new power blast starts in thanos position
            using_power_blast = True 
            p_x = thanos_x
            p_y = thanos_y

    # --- moves/changes (without draws) ---

    if using_power_blast:
        if thanos_orientation == "left":
            p_x -= p_vel
        if thanos_orientation == "right":
            p_x += p_vel

    # --- draws (without moves) ---

    # clear buffer
    screen.fill(GREY)

    # draw/blit all elements 

    if using_power_blast:
        screen.blit(power_blast, (p_x, p_y))

    thanos(thanos_x, thanos_y)

    # send buffer on screen
    pygame.display.update()

    # --- FPS ---

    clock.tick(FRAMERATE)

Я не проверял это, но оно должно работать.

Мне пришлось добавить также код:

    if keys[pygame.K_e]:
        # is it new power blast ?
        if using_power_blast is False:
            # new power blast starts in thanos position
            using_power_blast = True 
            p_x = thanos_x
            p_y = thanos_y
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...