我想在背景上保留图像一秒钟(或更长时间)然后它应该消失,我尝试在 if 语句内部调用的函数(image_draw)中使用 blit,但图像出现的时间很短,如果我在函数 image_draw 中更改了图像的时间,我没有看到任何变化。
在代码中,我有一条粗线连续向右和向左移动,每次它触及其中一个框架边框时,它都会改变方向,当方向改变发生时会出现图像,我的问题是如何控制时间背景上的图像?函数 image_draw 没有帮助,这里是代码。
import pygame
import sys
import time
import math
from pygame.locals import *
def image_draw(IMAGE, RECT_FOR_IMAGE, IMAGE_TIME ):
while IMAGE_TIME > 0:
windowSurface.blit(IMAGE, RECT_FOR_IMAGE)
IMAGE_TIME -= 1
pygame.init()
windowSurface = pygame.display.set_mode((300, 600), 0, 32)
pygame.display.set_caption("line and image")
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
MYCOLOR= (200, 240, 50)
OTHER_LINE_COLOR = (180, 0, 0)
OUR_LINE_START_X = 50
OUR_LINE_START_Y = 400
OUR_LINE_END_X = 250
OUR_LINE_END_Y = 400
OTHER_LINE_START_X = 0
OTHER_LINE_START_Y = 415
OTHER_LINE_END_X = 300
OTHER_LINE_END_Y = 415
IMAGE = pygame.image.load("cherry.png")
LEFT_IMAGE = 250
TOP_IMAGE = 530
WIDTH_IMAGE = 40
HIGHT_IMAGE = 40
IMAGE_TIME = 10
RECT_FOR_IMAGE_1 = pygame.Rect(LEFT_IMAGE, TOP_IMAGE, WIDTH_IMAGE, HIGHT_IMAGE)
RECT_FOR_IMAGE_2 = pygame.Rect(LEFT_IMAGE - 200, TOP_IMAGE, WIDTH_IMAGE, HIGHT_IMAGE)
SOUND = pygame.mixer.Sound("pickup.wav")
MOVE_SPEED = 1
DIRECTION = "left"
while True:
windowSurface.fill(WHITE)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if DIRECTION == "right" :
OUR_LINE_START_X += MOVE_SPEED
OUR_LINE_END_X += MOVE_SPEED
elif DIRECTION == "left":
OUR_LINE_START_X -= MOVE_SPEED
OUR_LINE_END_X -= MOVE_SPEED
if OUR_LINE_START_X < 0:
DIRECTION = "right"
SOUND.play()
image_draw(IMAGE, RECT_FOR_IMAGE_1, IMAGE_TIME )
if OUR_LINE_END_X > 300:
DIRECTION = "left"
SOUND.play()
image_draw(IMAGE, RECT_FOR_IMAGE_2, IMAGE_TIME )
pygame.draw.line(windowSurface, MYCOLOR, (OUR_LINE_START_X, OUR_LINE_START_Y), (OUR_LINE_END_X, OUR_LINE_END_Y), 30)
pygame.draw.line(windowSurface, OTHER_LINE_COLOR, (OTHER_LINE_START_X, OTHER_LINE_START_Y), (OTHER_LINE_END_X, OTHER_LINE_END_Y), 3)
pygame.display.update()
time.sleep(0.02)
先感谢您。