0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pygameで紫の雨を降らせる方法

Last updated at Posted at 2020-07-22

#0.はじめに
今回作るものがどういう感じで動くのか見てみたい、この記事を読むのが面倒くさい方はこちら(Youtubeの動画)をご覧ください。

#1.実装

purple_rain.py
import pygame
import random

WIDTH = 640
HEIGHT = 360

win = pygame.display.set_mode((WIDTH,HEIGHT))

class Drop:
    def __init__(self,win):
        self.x = random.uniform(0,WIDTH)
        self.y = -1
        self.yspeed = random.uniform(1,3)
        self.win = win
        self.length = random.uniform(10,20)

    def fall(self):
        self.y += self.yspeed

    def show(self):
        pygame.draw.rect(self.win,(138,43,226),(self.x,self.y,2,self.length))

def drawWindow(rain):
    for water in rain:
        water.show()
        if water.y <= 360:
            water.fall()
        else:
            rain.pop(rain.index(water))

    pygame.display.update()

rain = []
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    win.fill((230,230,250))
    
    if len(rain) < 500:
        rain.append(Drop(win))

    drawWindow(rain)
pygame.quit()

クラスを使ったので長くなってしまいましたが、やってることは雨をランダムな長さ、位置、速度で落としていき画面の外に消えたら追加というのをやっています。簡単ですね!
#最後に
今回作ったものはYoutubeでも解説しているのでそちらも良かったらご覧ください。質問、アドバイスがあればぜひコメントをよろしくお願いします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?