烟花代码编程python(玫瑰花代码编程python)

http://www.itjxue.com  2023-04-05 06:35  来源:未知  点击次数: 

2022年跨年烟花代码可复制

烟花代码如下:

package love;

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.net.URL;

import java.util.Random;

烟花

@author enjoy

@SuppressWarnings("serial")

public class Q extends Applet implements Runnable

public int speed, variability, Max_Number, Max_Energy, Max_Patch,

Max_Length, G;

public String sound;

private int width, height;

private Thread thread = null;

private BeaClassDemo bcd[];

public void init()? ? ? ? ? ? ? ? ? ? ?

int i;

this.setSize(1900, 900);

width = getSize().width - 1;

height = getSize().height - 1;

speed = 1;??????????????????????? // 烟花绽放的速度

variability = 10;

Max_Number = 980;??????????????????? // 可发出烟花的最大数目

Max_Energy = width + 50;

Max_Patch = 90;??????????????????? // 最大的斑点数

Max_Length = 90;??????????????????? // 斑点的最大距离

G = 150;??????????????????????????? // 向地面弯曲的力度

bcd = new BeaClassDemo[Max_Number];

for (i = 0; i Max_Number; i++)

bcd[i] = new BeaClassDemo(width, height, G);

}

public void start() {

if (thread == null) {

thread = new Thread(this);

thread.start();

}

}

@SuppressWarnings("deprecation")

public void stop() {

if (thread != null) {

thread.stop();

thread = null;

}

}

@SuppressWarnings({ "unused", "static-access" })

public void run() {

int i;

int E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4 + 1;

int P = (int) (Math.random() * Max_Patch * 3 / 4)??? // 烟花的斑点数

+ Max_Patch / 4 + 1;

int L = (int) (Math.random() * Max_Length * 3 / 4)??? // 烟花可发射出的距离

+ Max_Length / 4 + 1;

long S = (long) (Math.random() * 10000);

boolean sleep;

Graphics g = getGraphics();

URL u = null;

while (true) {

try {

thread.sleep(1000 / speed);

catch (InterruptedException x) {

sleep = true;

for (i = 0; i Max_Number; i++)

sleep = sleep bcd[i].sleep;

if (sleep Math.random() * 100 variability) {

E = (int) (Math.random() * Max_Energy * 3 / 4) + Max_Energy / 4

+ 1;

P = (int) (Math.random() * Max_Patch * 3 / 4) + Max_Patch / 4

+ 1;

L = (int) (Math.random() * Max_Length * 3 / 4) + Max_Length / 4

+ 1;

S = (long) (Math.random() * 10000);

for (i = 0; i Max_Number; i++) {

if (bcd[i].sleep Math.random() * Max_Number * L 1)

bcd[i].init(E, P, L, S);

bcd[i].start();

bcd[i].show(g);

public void paint(Graphics g)? ? ? ? ? ? ? ? ?

g.setColor(Color.black);

g.fillRect(0, 0, width + 1, height + 1);

class BeaClassDemo

public boolean sleep = true;

private int energy, patch, length, width, height, G, Xx, Xy, Ex[], Ey[], x,

y, Red, Blue, Green, t;

private Random random;

public BeaClassDemo(int a, int b, int g)? ? ? ?

width = a;

height = b;

G = g;

public void init(int e, int p, int l, long seed)?

int i;

energy = e;

patch = p;

length = l;

// 创建一个带种子的随机数生成器

random = new Random(seed);

Ex = new int[patch];

Ey = new int[patch];

Red = (int) (random.nextDouble() * 128) + 128;

Blue = (int) (random.nextDouble() * 128) + 128;

Green = (int) (random.nextDouble() * 128) + 128;

Xx = (int) (Math.random() * width / 2) + width / 4;

Xy = (int) (Math.random() * height / 2) + height / 4;

for (i = 0; i patch; i++) {

Ex[i] = (int) (Math.random() * energy) - energy / 2;

Ey[i] = (int) (Math.random() * energy * 7 / 8) - energy / 8;

public void start

t = 0;

sleep = false;

public void show(Graphics g)? ? ?

if (!sleep)??????????????????

if (t length)

int i, c;

double s;

Color color;

c = (int) (random.nextDouble() * 64) - 32 + Red;

if (c = 0 c 256)

Red = c;

c = (int) (random.nextDouble() * 64) - 32 + Blue;

if (c = 0 c 256)

Blue = c;

c = (int) (random.nextDouble() * 64) - 32 + Green;

if (c = 0 c 256)

Green = c;

color = new Color(Red, Blue, Green);

for (i = 0; i patch; i++)

s = (double) t / 100;

x = (int) (Ex[i] * s);

y = (int) (Ey[i] * s - G * s * s);

g.setColor(color);

g.drawLine(Xx + x, Xy - y, Xx + x, Xy - y);

if (t = length / 2)

int j;

for (j = 0; j 2; j++)

s = (double) ((t - length / 2) * 2 + j) / 100;

x = (int) (Ex[i] * s);

y = (int) (Ey[i] * s - G * s * s);

g.setColor(Color.black);

g.drawLine(Xx + x, Xy - y, Xx + x, Xy - y);

常用的编程语言。

编程语言一:C语言

C语言是世界上最流行、使用最广泛的高级程序设计语言之一。在操作系统和系统使用程序以及需要对硬件进行操作的场合,用C语言明显优于其它高级语言,许多大型应用软件都是用C语言编写的。

编程语言二:java

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE, JavaEE, JavaME)的总称。

编程语言三:c++

C++这个词在中国大陆的程序员圈子中通常被读做“C加加”,而西方的程序员通常读做“C plus plus" , "CPP”。 它是一种使用非常广泛的计算机编程语言。C++是一种静态数据类型检查的、支持多重编程范式的通用程序设计语言。

放烟花的代码

# -*- coding: utf-8 -*-import math, random,timeimport threadingimport tkinter as tkimport re uuidFireworks=[]maxFireworks=8height,width=600,600class firework(object):def __init__(self,color,speed,width,height):=uuid.uuid1()self.radius=random.randint(2,4) ~4像素self.color=color self.speed=speed .5-3.5秒self.status=0 ,status=0;爆炸后,status=1;当status100时,烟花的生命期终止self.nParticle=random.randint(20,30) self.center=[random.randint(0,width-1),random.randint(0,height-1)] self.oneParticle=[] (100%状态时)self.rotTheta=random.uniform(0,2*math.pi) :x=a*cos(theta),y=b*sin(theta)=[a,b]

2022跨年烟花代码|用Python送你一场跨年烟花秀

2021 已经接近尾声了,2022 即将到来,本文我们用 Python 送你一场跨年烟花秀。

我们用到的 Python 模块包括:tkinter、PIL、time、random、math,如果第三方模块没有装的话,pip install 一下即可,下面看一下代码实现。

导库

烟花颜色

定义烟花类

燃放烟花

启动

看一下效果:

表白烟花代码

天天敲代码的朋友,有没有想过代码也可以变得很酷炫又浪漫?今天就教大家用Python模拟出绽放的烟花,工作之余也可以随时让程序为自己放一场烟花秀。

python炫酷烟花表白源代码

这个有趣的小项目并不复杂,只需一点可视化技巧,100余行Python代码和程序库Tkinter,最后我们就能达到下面这个效果:

学完本教程后,你也能做出这样的烟花秀。

整体概念梳理

我们的整个理念比较简单。

我们这里通过让画面上一个粒子分裂为X数量的粒子来模拟爆炸效果。粒子会发生"膨胀”,意思是它们会以恒速移动且相互之间的角度相等。这样就能让我们以一个向外膨胀的圆圈形式模拟出烟花绽放的画面。经过一定时间后,粒子会进入"自由落体”阶段,也就是由于重力因素它们开始坠落到地面,仿若绽放后熄灭的烟花。

python炫酷烟花表白源代码是多少?

学完本教程后,你也能做出这样的烟花秀。

如上图示,我们这里通过让画面上一个粒子分裂为X数量的粒子来模拟爆炸效果。粒子会发生"膨胀”,意思是它们会以恒速移动且相互之间的角度相等。这样就能让我们以一个向外膨胀的圆圈形式模拟出烟花绽放的画面。

经过一定时间后,粒子会进入"自由落体”阶段,也就是由于重力因素它们开始坠落到地面,仿若绽放后熄灭的烟花。

基本知识:用Python和Tkinter设计烟花。

这里不再一股脑把数学知识全丢出来,我们边写代码边说理论。首先,确保你安装和导入了Tkinter,它是Python的标准GUI库,广泛应用于各种各样的项目和程序开发,在Python中使用Tkinter可以快速的创建GUI应用程序。

import tkinter as tk

from PIL import Image, ImageTk

from time import time, sleep

from random import choice, uniform, randint

from math import sin, cos, radians

除了Tkinter之外,为了能让界面有漂亮的背景,我们也导入PIL用于图像处理,以及导入其它一些包,比如time,random和math。它们能让我们更容易的控制烟花粒子的运动轨迹。

Tkinter应用的基本设置如下:

root = tk.Tk()

为了能初始化Tkinter,我们必须创建一个Tk()根部件(root widget),它是一个窗口,带有标题栏和由窗口管理器提供的其它装饰物。该根部件必须在我们创建其它小部件之前就创建完毕,而且只能有一个根部件。

w = tk.Label(root, text="Hello Tkinter!")

这一行代码包含了Label部件。该Label调用中的第一个参数就是父窗口的名字,即我们这里用的"根”。关键字参数"text”指明显示的文字内容。你也可以调用其它小部件:Button,Canvas等等。

w.pack()

root.mainloop()

接下来的这两行代码很重要。这里的打包方法是告诉Tkinter调整窗口大小以适应所用的小部件。窗口直到我们进入Tkinter事件循环,被root.mainloop()调用时才会出现。在我们关闭窗口前,脚本会一直在停留在事件循环。

将烟花绽放转译成代码

现在我们设计一个对象,表示烟花事件中的每个粒子。每个粒子都会有一些重要的属性,支配了它的外观和移动状况:大小,颜色,位置,速度等等。

(责任编辑:IT教学网)

更多

推荐Flash动画制作教程文章