1

Im trying to make it so when my picture bounces, the window changes to a random color, but when it changes, it only changes for a split second when it collides then the window changes back to white

when i remove screen.fill, it keeps the random color for every bounce, but then it leaves a trail of images

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()

1 Answer 1

2

Create a variable for the colur and use it to fill the window. Change the variable when there is a collision

fill_color = 'white'
trampoline = pygame.Rect(0, 440, 500, 10)

while running:  
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    clock.tick(30)
    
    gravity += 0.6 
    epic_rect.bottom += gravity
    collide = pygame.Rect.colliderect(trampoline, epic_rect)
    if collide:
        epic_rect.bottom = trampoline.top
        fill_color = random.choice(colors)
        gravity = -18 

    window.fill(fill_color)
    pygame.draw.rect(window, trampoline_color, trampoline)
    window.blit(epic_image, epic_rect.topleft)
    pygame.display.flip()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.