我试图做到这一点,当我的图片弹起时,窗口会变成随机颜色,但是当它发生变化时,它只会在碰撞时改变一瞬间,然后窗口会变回白色
当我删除 screen.fill 时,它会保留每次弹跳的随机颜色,但随后会留下图像痕迹
import pygame
import random
height = 500
width = 500
pygame.init()
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption('epic trampoline')
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink']
running = True
win_color = 'white'
trampoline_color = 'black'
clock = pygame.time.Clock()
image = pygame.image.load('epicface.png').convert_alpha()
epic_image = pygame.transform.scale(image, (50, 50)) # changing size
epic_rect = epic_image.get_rect(center=(250, 0))
gravity = 0
acceleration = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(30)
gravity += 0.6
window.fill('white')
epic_rect.bottom += gravity
# rect = pygame.draw.rect(window, 'white', epic_rect)
trampoline = pygame.draw.rect(window, trampoline_color, pygame.Rect(0, 440, 500, 10))
collide = pygame.Rect.colliderect(trampoline, epic_rect)
if collide:
epic_rect.bottom = trampoline.top
win_color = window.fill(random.choice(colors))
gravity = -18
window.blit(epic_image, epic_rect.topleft)
pygame.display.flip()
pygame.display.flip()
您正在
window.fill('white')
循环中调用 while running
。
因此,更改颜色后,下一个循环将再次将其设置为白色。
您应该在启动时调用
window.fill('white')
一次(在 while
之外),然后在循环内调用 window.fill
。