我一直在研究一个项目,该项目的角色由加载的图像组成,可以射击弹丸。在这种情况下,我的角色是Thanos,我让他发射紫色能量球。我将其设置为当我按“ e”时,该球出现并且应该发射出去。但是,当我按“ e”时,球出现并闪烁。它发射一次,然后重新出现在Thanos,仍然闪烁。这是我的代码:(忽略无穷大的石头,我还没有开始研究它们,也可能我把代码粘贴错了,这是我的第一篇文章)
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()
答案 0 :(得分:0)
您应该只有一个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