最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)煙花小程序

 更新時間:2019年01月30日 11:47:03   作者:Dachao1013  
這篇文章主要為大家詳細介紹了python實現(xiàn)煙花小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python實現(xiàn)煙花小程序的具體代碼,供大家參考,具體內容如下

'''
FIREWORKS SIMULATION WITH TKINTER
*self-containing code
*to run: simply type python simple.py in your console
*compatible with both Python 2 and Python 3
*Dependencies: tkinter, Pillow (only for background image)
*The design is based on high school physics, with some small twists only for aesthetics purpose
 
import tkinter as tk
#from tkinter import messagebox
#from tkinter import PhotoImage
from PIL import Image, ImageTk
from time import time, sleep
from random import choice, uniform, randint
from math import sin, cos, radians
# gravity, act as our constant g, you can experiment by changing it
GRAVITY = 0.05
# list of color, can choose randomly or use as a queue (FIFO)
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen','indigo', 'cornflowerblue']
Generic class for particles
particles are emitted almost randomly on the sky, forming a round of circle (a star) before falling and getting removed
from canvas
Attributes:
 - id: identifier of a particular particle in a star
 - x, y: x,y-coordinate of a star (point of explosion)
 - vx, vy: speed of particle in x, y coordinate
 - total: total number of particle in a star
 - age: how long has the particle last on canvas
 - color: self-explantory
 - cv: canvas
 - lifespan: how long a particle will last on canvas
class part:
 def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx = 0., vy = 0., size=2., color = 'red', lifespan = 2, **kwargs):
  self.id = idx
  self.x = x
  self.y = y
  self.initial_speed = explosion_speed
  self.vx = vx
  self.vy = vy
  self.total = total
  self.age = 0
  self.color = color
  self.cv = cv
  self.cid = self.cv.create_oval(
   x - size, y - size, x + size,
   y + size, fill=self.color)
  self.lifespan = lifespan
 def update(self, dt):
  self.age += dt
  # particle expansions
  if self.alive() and self.expand():
   move_x = cos(radians(self.id*360/self.total))*self.initial_speed
   move_y = sin(radians(self.id*360/self.total))*self.initial_speed
   self.cv.move(self.cid, move_x, move_y)
   self.vx = move_x/(float(dt)*1000)
  # falling down in projectile motion
  elif self.alive():
   move_x = cos(radians(self.id*360/self.total))
   # we technically don't need to update x, y because move will do the job
   self.cv.move(self.cid, self.vx + move_x, self.vy+GRAVITY*dt)
   self.vy += GRAVITY*dt
  # remove article if it is over the lifespan
  elif self.cid is not None:
   cv.delete(self.cid)
   self.cid = None
 # define time frame for expansion
 def expand (self):
  return self.age <= 1.2
 # check if particle is still alive in lifespan
 def alive(self):
  return self.age <= self.lifespan
Firework simulation loop:
Recursively call to repeatedly emit new fireworks on canvas
a list of list (list of stars, each of which is a list of particles)
is created and drawn on canvas at every call, 
via update protocol inside each 'part' object 
def simulate(cv):
 t = time()
 explode_points = []
 wait_time = randint(10,100)
 numb_explode = randint(6,10)
 # create list of list of all particles in all simultaneous explosion
 for point in range(numb_explode):
  objects = []
  x_cordi = randint(50,550)
  y_cordi = randint(50, 150)
  speed = uniform (0.5, 1.5)   
  size = uniform (0.5,3)
  color = choice(colors)
  explosion_speed = uniform(0.2, 1)
  total_particles = randint(10,50)
  for i in range(1,total_particles):
   r = part(cv, idx = i, total = total_particles, explosion_speed = explosion_speed, x = x_cordi, y = y_cordi, 
    vx = speed, vy = speed, color=color, size = size, lifespan = uniform(0.6,1.75))
   objects.append(r)
  explode_points.append(objects)
 total_time = .0
 # keeps undate within a timeframe of 1.8 second
 while total_time < 1.8:
  sleep(0.01)
  tnew = time()
  t, dt = tnew, tnew - t
  for point in explode_points:
   for item in point:
    item.update(dt)
  cv.update()
  total_time += dt
 # recursive call to continue adding new explosion on canvas
 root.after(wait_time, simulate, cv)
def close(*ignore):
 """Stops simulation loop and closes the window."""
 global root
 root.quit()
 
if __name__ == '__main__':
 root = tk.Tk()
 cv = tk.Canvas(root, height=600, width=600)
 # use a nice background image
 image = Image.open("./image1.jpg")#背景照片路徑自行選擇,可以選擇酷炫一點的,看起來效果會#更好
 photo = ImageTk.PhotoImage(image)
 cv.create_image(0, 0, image=photo, anchor='nw')
 cv.pack()
 root.protocol("WM_DELETE_WINDOW", close)
 root.after(100, simulate, cv)
 root.mainloop()

注意:這里需要安裝tkinter,安裝過程:

step1:

>>> import _tkinter # with underscore, and lowercase 't'

step2:

>>> import Tkinter # no underscore, uppercase 'T' for versions prior to V3.0

>>> import tkinter # no underscore, lowercase 't' for V3.0 and later

step3:

>>> Tkinter._test() # note underscore in _test and uppercase 'T' for versions prior to V3.0 

>>> tkinter._test() # note underscore in _test and lowercase 'T' for V3.0 and later

然后就可以運行了,在代碼中有一個背景照片部分,路徑可自行選擇!我這里就不修改了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python數(shù)據(jù)結構與算法之跳表詳解

    Python數(shù)據(jù)結構與算法之跳表詳解

    跳表是帶有附加指針的鏈表,使用這些附加指針可以跳過一些中間結點,用以快速完成查找、插入和刪除等操作。本節(jié)將詳細介紹跳表的相關概念及其具體實現(xiàn),需要的可以參考一下
    2022-02-02
  • Python數(shù)據(jù)持久化存儲實現(xiàn)方法分析

    Python數(shù)據(jù)持久化存儲實現(xiàn)方法分析

    這篇文章主要介紹了Python數(shù)據(jù)持久化存儲實現(xiàn)方法,結合實例形式分析了Python基于pymongo及mysql模塊的數(shù)據(jù)持久化存儲操作相關實現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12
  • Python3 全自動更新已安裝的模塊實現(xiàn)

    Python3 全自動更新已安裝的模塊實現(xiàn)

    這篇文章主要介紹了Python3 全自動更新已安裝的模塊實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • python中的協(xié)程深入理解

    python中的協(xié)程深入理解

    這篇文章主要給大家介紹了關于python中協(xié)程的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • Python中的defaultdict模塊和namedtuple模塊的簡單入門指南

    Python中的defaultdict模塊和namedtuple模塊的簡單入門指南

    這篇文章主要介紹了Python中的defaultdict模塊和namedtuple模塊的簡單入門指南,efaultdict繼承自dict、namedtuple繼承自tuple,是Python中內置的數(shù)據(jù)類型,需要的朋友可以參考下
    2015-04-04
  • 對python同一個文件夾里面不同.py文件的交叉引用方法詳解

    對python同一個文件夾里面不同.py文件的交叉引用方法詳解

    今天小編就為大家分享一篇對python同一個文件夾里面不同.py文件的交叉引用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Django框架中處理URLconf中特定的URL的方法

    Django框架中處理URLconf中特定的URL的方法

    這篇文章主要介紹了Django框架中處理URLconf中特定的URL的方法,Django是豐富多彩的Python框架中最具人氣的一個,需要的朋友可以參考下
    2015-07-07
  • python 全文檢索引擎詳解

    python 全文檢索引擎詳解

    這篇文章主要介紹了python 全文檢索引擎詳解的相關資料,需要的朋友可以參考下
    2017-04-04
  • 使用Python寫個小監(jiān)控

    使用Python寫個小監(jiān)控

    最近使用python寫了個小監(jiān)控,為什么使用python?簡單、方便、好管理,Python如何實現(xiàn)簡單的小監(jiān)控,感興趣的小伙伴們可以參考一下
    2016-01-01
  • python中urllib.request和requests的使用及區(qū)別詳解

    python中urllib.request和requests的使用及區(qū)別詳解

    這篇文章主要介紹了python中urllib.request和requests的使用及區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05

最新評論

罗源县| 达孜县| 永寿县| 泰兴市| 温泉县| 平度市| 大英县| 黄龙县| 济宁市| 荣昌县| 托里县| 出国| 绥滨县| 屏南县| 广昌县| 桂东县| 江达县| 清水县| 尉犁县| 镇巴县| 浦县| 射洪县| 枣庄市| 藁城市| 怀来县| 乌什县| 元谋县| 武胜县| 弋阳县| 彰武县| 林芝县| 泰兴市| 广元市| 朔州市| 玛多县| 光山县| 田林县| 泰来县| 宕昌县| 荣成市| 托克逊县|