0

I have been trying to learn pygame the last day or so, and tried to write a basic program that just has a small image of a leaf falling from the top of the screen. Nothing appears when I run it, and I imagine I'm missing something obvious with how I'm doing this. (I can tell this is a very inefficient way of doing this as well, so tips would be appreciated!)

Here's the code:

import pygame
from pygame.locals import *
import random

pygame.init()


class Leaf:
    def __init__(self):
        self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
        self.leafrect = self.leafimage.get_rect()
        xpos = random.randint(0, 640)
        self.leafrect.midtop = (xpos, 0)
    def move(self):
        self.leafrect = self.leafrect.move([0, 1])

def main():
    width= 640
    heigth = 480
    dimensions = (width, heigth)
    screen = pygame.display.set_mode(dimensions)
    pygame.display.set_caption('Some Epic Pygame Stuff')

    clock = pygame.time.Clock()

    leaves = []
    for i in range(5):
        leaves.append(Leaf())

    running = 1
    while running:
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
            running = 0



        for i in leaves:
            i.move()
            screen.blit(i.leafimage, i.leafrect)


        screen.fill((255, 255, 255))


        pygame.display.flip()





    if __name__ == '__main__': main() 
1

1 Answer 1

7

You probably don't want this sequence:

for i in leaves:
    i.move()
    screen.blit(i.leafimage, i.leafrect)


screen.fill((255, 255, 255))


pygame.display.flip()

You draw the leaves, and then fill the entire screen with white, and then show the screen.

fill the screen, then draw the leaves, then flip()

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

2 Comments

Thanks! That makes a lot more sense now.
if his answer works, then u should click the checkmark next to his answer.

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.