开发者

Python Pygame Basic Program

开发者 https://www.devze.com 2023-02-14 17:37 出处:网络
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.

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.开发者_如何学JAVAtype == 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() 


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

0

精彩评论

暂无评论...
验证码 换一张
取 消