Python Pygame 基本程序

发布于 2024-10-20 05:04:12 字数 1220 浏览 3 评论 0原文

我在最后一天左右一直在尝试学习 pygame,并尝试编写一个基本程序,该程序只有一个从屏幕顶部落下的叶子的小图像。 当我运行它时,什么也没有出现,我想我在执行此操作时遗漏了一些明显的东西。 (我可以说这也是一种非常低效的方法,因此我们将不胜感激!)

这是代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-10-27 05:04:12

您可能不想要这个顺序:

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


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


pygame.display.flip()

绘制叶子,然后用白色填充整个屏幕,然后显示屏幕。

填充屏幕,然后绘制叶子,然后flip()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文