【问题标题】:How can I have multiple drawings on the screen at once without them flashing?我怎样才能在屏幕上同时显示多个绘图而不闪烁?
【发布时间】:2020-02-20 16:42:39
【问题描述】:

我目前正在为学校课程项目编写一个简单的电脑游戏,但在 pygame 中遇到了问题。在我的游戏中,玩家只需向空中飞行的目标射击,几乎是逆向太空入侵者。我当前的问题是,我不能在竞技场中同时拥有两个形状,而其中一个形状会闪烁和滞后。以下是我的代码,欢迎任何帮助。干杯。

另外,有谁知道我可以如何在每幅画之间添加延迟。正如我在上面暗示的那样,我的目标是让目标在没有重叠的情况下保持相当的空间,但我确实希望同时在屏幕上出现多个。干杯。

import pygame

#Setting window dimensions and caption (Module 1)

pygame.init()
window = pygame.display.set_mode((800, 575))
pygame.display.set_caption("TARGET PRACTICE")

#Colour variables (Module 1)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)

#Target coordinates and dimensions (Module 3)

#target_x = 0
#target_y = 80
#target_w = 60
#target_h = 40
#target_v = 1

exec = True

class Target:
  def __init__(self, x, y, h, w, v):
    self.x = x
    self.y = y
    self.h = h
    self.w = w
    self.v = v

target_1 = Target(0, 80, 60, 40, 0.1)
target_2 = Target(0, 100, 60, 40, 0.1)
target_3 = Target(0, 50, 60, 40, 1)

clock = 0

while exec:
  pygame.time.delay(1)
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      exec = False

  #Target Movement (Module 4)

  window.fill(RED)

  #Background and target drawing (Module 2)

  target_1.x += target_1.v
  pygame.draw.rect(window, BLUE, (target_1.x, target_1.y, target_1.h, target_1.w))
  clock += 1

  if clock%2 == 0:
    target_2.x += target_2.v
    pygame.draw.rect(window, BLUE, (target_2.x, target_2.y, target_2.h, target_2.w))


  pygame.display.update()




pygame.quit()```

【问题讨论】:

    标签: python oop pygame draw shapes


    【解决方案1】:

    target_2 正在闪烁,因为它只是在每 2 帧中绘制一次。在每 2 帧更改 target_2 的位置,但在每一帧中绘制它:

    while exec:
        # [...]
    
        # update positions 
        target_1.x += target_1.v
        if clock%2 == 0:
            target_2.x += target_2.v
        clock += 1
    
        # clear window (fill in rED)
        window.fill(RED)
    
        # draw all the objects of the scene in every frame 
        pygame.draw.rect(window, BLUE, (target_1.x, target_1.y, target_1.h, target_1.w))
        pygame.draw.rect(window, BLUE, (target_2.x, target_2.y, target_2.h, target_2.w))   
    
        # update display
        pygame.display.update()
    

    注意,场景必须在每一帧中重新绘制。首先清除背景填充window.fill(RED),然后绘制整个场景,最后更新显示(pygame.display.update())。


    如果你想延迟target_2,那么你必须在clock达到一定限制后绘制并更新位置:

    clock = 0
    target_2_threshold = 500
    
    while exec:
        # [...]
    
        # update positions 
        clock += 1
        target_1.x += target_1.v
        if clock > target_2_threshold and clock % 2 == 0:
            target_2.x += target_2.v
    
        # clear window (fill in rED)
        window.fill(RED)
    
        # draw all the objects of the scene in every frame 
        pygame.draw.rect(window, BLUE, (target_1.x, target_1.y, target_1.h, target_1.w))
        if clock > target_2_threshold:
            pygame.draw.rect(window, BLUE, (target_2.x, target_2.y, target_2.h, target_2.w))   
    
        # update display
        pygame.display.update()
    

    【讨论】:

    • 干杯,更好。不知道您是否可以进一步帮助我(我的问题措辞非常糟糕),但我的目标是在绘制的形状或“生成”之间有某种延迟,否则的话。我想这就是我打算用时钟变量做的事情,但目前这两个形状基本上开始同步移动。显然,这对于试图击中目标的玩家来说并不是很好。知道如何添加一些延迟吗?干杯。
    • 仍然没有增加足够的延迟。也许我不确定这是不可能的,但有没有办法在循环中延迟特定的函数?
    • 干杯,完美的伙伴,你帮了我一个大忙
    猜你喜欢
    • 1970-01-01
    • 2023-01-03
    • 2021-04-22
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多