繁体   English   中英

怎么让图片从天上掉下来

[英]How do I make an image fall from the sky

我目前有我想要使用的图像,并且了解您需要在 y 方向上使用重力和速度,但想知道如何将它们放在一起,最好放在 class 中。

如果可能的话,我也想知道如何制作它,以便随机出现图像出现和掉落的机会。

这是一个可能看起来如何的示例:

import pygame
import random


pygame.init()
screen = pygame.display.set_mode((900, 600))
clock = pygame.time.Clock()


class FallingImage:
    def __init__(self, file, x, y, width, height):
        self.file = file
        self.x = x
        self.y = y
        self.width = width
        self.height = height

        self.image = pygame.transform.scale(pygame.image.load(self.file).convert_alpha(), (self.width, self.height))

    def draw(self):
        screen.blit(self.image, (self.x, self.y))


falling_img = FallingImage('your_image.png', 300, -100, 50, 100)

run = True
while run:

    clock.tick(60)

    screen.fill((0, 0, 0))

    falling_img.draw()
    falling_img.y += 25
    if falling_img.y > 600:
        falling_img.y = -150
        falling_img.x = random.randint(0, 900)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    pygame.display.update()

pygame.quit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM