如何在不闪烁的情况下同时在屏幕上显示多个图形?
How can I have multiple drawings on the screen at once without them flashing?
我目前正在为一个学校课程项目编写一个简单的电脑游戏,在 pygame 中遇到了一个问题。在我的游戏中,玩家只需射击在空中飞行的目标,几乎是一个反向 space 入侵者。我目前的问题是,我不能同时在竞技场中拥有两种形状,而其中一种形状不会闪烁和滞后。下面是我的代码,欢迎任何帮助。干杯。
另外,有谁知道如何在绘制的每张图之间添加延迟。正如我在上面暗示的那样,我的目标是让目标相当 space 不重叠,但我确实希望同时在屏幕上显示多个目标。干杯。
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()```
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()
我目前正在为一个学校课程项目编写一个简单的电脑游戏,在 pygame 中遇到了一个问题。在我的游戏中,玩家只需射击在空中飞行的目标,几乎是一个反向 space 入侵者。我目前的问题是,我不能同时在竞技场中拥有两种形状,而其中一种形状不会闪烁和滞后。下面是我的代码,欢迎任何帮助。干杯。
另外,有谁知道如何在绘制的每张图之间添加延迟。正如我在上面暗示的那样,我的目标是让目标相当 space 不重叠,但我确实希望同时在屏幕上显示多个目标。干杯。
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()```
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()