首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >闪烁弹丸

闪烁弹丸
EN

Stack Overflow用户
提问于 2019-04-29 00:46:39
回答 1查看 20关注 0票数 0

我一直在做一个项目,这个项目有一个角色,由一个加载的图像组成,他可以发射弹丸。在这种情况下,我的角色是萨诺斯,我让他发射一个紫色能量球的东西。我已经设置好了,当我按下"e“键时,球就会出现,并且应该会飞走。然而,当我按下"e“键时,球就会出现并闪烁。它发射了一次,然后在塔诺斯重新出现,仍然闪烁。这是我的代码:(忽略无穷大的石头,我还没有开始处理它们,而且我可能粘贴了错误的代码这是我在这里的第一篇文章)

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-29 01:02:16

你应该只有一个update(),而且你必须在fill()updated()之间选择

fill()清除缓冲区,update()将缓冲区发送到视频卡,并将其显示在屏幕上。

如果你在fill()之前进行blit,那么fill()会将它从buffer中移除,你将看不到它。但是你不应该使用update()来解决这个问题。

如果你在blit缓冲区中的所有元素之前使用update(),那么它将显示不完整的图像,下一个update()将显示完整的图像,因此您可能会看到翻转的元素。

代码语言:javascript
复制
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)

我没有测试它,但它应该可以工作。

我还必须添加代码:

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55892483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档