python - 用python制作动画图像
问题描述
所以我尝试用它来学习python,我开始编写著名的DVD_logo动画,背景不断变化的颜色。我第一次这样做时使用了“Pygame”库,但它的运行速度有点慢。我不知道如何为我的目的搜索更好的库,也许是如何改进我的程序。我实际上对其进行了修改,以有限的表面处理令人耳目一新的背景,但似乎不起作用。有代码,请记住是我的第一次尝试,所以也许有一些奇怪的东西或一些评论声明(我只是在尝试)
import random
import sys
import os
import pygame
import random
from PIL import Image
time_exit = 3000
pygame.init()
pygame.display.init()
win_width = 800#1440#817#980
win_height = 420#855#417#500#
logo_s_w = 134
logo_s_h = 67
logo_w = random.Random()
logo_h = random.Random()
logo_w = logo_w.uniform(0 + logo_s_w, win_width - logo_s_w)
logo_h = logo_h.uniform(0 + logo_s_h, win_height - logo_s_h)
logo_w = int(logo_w)
logo_h = int(logo_h)
back_w = logo_w
back_h = logo_h
past_h = 0
past_w = 0
moving_pixel = 3
coverage_pixel_w = logo_s_w + moving_pixel * 10
coverage_pixel_h = logo_s_h + moving_pixel * 10
mov_cov = moving_pixel * 2
red = (
255, 0, 0)
rgb = random.Random()
rgb = [
0, 100, 0
]
r = random.Random()
b = random.Random()
g = random.Random()
x_flag = bool(random.getrandbits(1))
y_flag = bool(random.getrandbits(1))
hit = False
def logo(x,y):
win.blit(dvdImg, (x,y))
def back(x,y):
win.blit(back_rec, (x,y))
black = (
0, 0, 0)
#clock = pygame.time.Clock()
win = pygame.display.set_mode((win_width, win_height))
#win = pygame.Surface((win_width, win_height)).convert_alpha()
dvdImg = pygame.transform.scale(pygame.image.load('DVD_logo.gif'),[logo_s_w, logo_s_h])
pygame.display.set_caption('DVD')
background_colour = (rgb)
win.fill(background_colour)
#bg_image = pygame.Surface((logo_s_w, logo_s_h))
#win.fill(black)
bg_image = pygame.Surface((logo_s_w, logo_s_h))
logo(logo_w, logo_h)
back_rec = pygame.Surface((coverage_pixel_w, coverage_pixel_h))
pygame.display.flip()
time = 0
running = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
hit = False
#bg_image = pygame.Surface((logo_s_w, logo_s_h))
#pygame.draw.rect(bg_image, background_colour, [logo_w, logo_h, logo_s_w, logo_s_h])
#pygame.draw.rect(bg_image, (255,192,0), [200, 0, 200, 200])
#win.fill(background_colour)
back_rec.fill(rgb)
back(back_w, back_h)
logo(logo_w, logo_h)
pygame.display.update()
#clock.tick(60)
back_w = logo_w - past_w
back_h = logo_h + past_h
if logo_w < win_width and not x_flag:
logo_w = logo_w + moving_pixel
past_w = + moving_pixel + mov_cov
if logo_w > 0 and x_flag:
logo_w = logo_w - moving_pixel
past_w = - moving_pixel + mov_cov
if logo_w <= 0:
x_flag = False
hit = True
if logo_w >= (win_width - (logo_s_w)):
x_flag = True
hit = True
if logo_h < win_height and not y_flag:
logo_h = logo_h + moving_pixel
past_h = + moving_pixel - mov_cov
if logo_h > 0 and y_flag:
logo_h = logo_h - moving_pixel
past_h = 0 - moving_pixel - mov_cov
if logo_h <= 0:
y_flag = False
hit = True
if logo_h >= (win_height - (logo_s_h)):
y_flag = True
hit = True
#change color
if hit:
rgb[0] = r.uniform(0, 255)
rgb[1] = g.uniform(0, 255)
rgb[2] = b.uniform(0, 255)
background_colour = tuple(rgb)
win.fill(background_colour)
#background_colour = background_colour(rgb)
#rgb = list(rgb)
hit = False
#pygame.time.delay()
# time = time + 1
if time > time_exit:
sys.exit()
#running = True
#while running:
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# running = False
pygame.quit()
quit()
解决方案
推荐阅读
- vue.js - 从自定义 v-date-picker Vuetify 中获取选择的日期
- c# - 在 C# 中的日期选择器中设置最大日期
- blazor - Blazor 访问硬件
- angular - Angular类继承,如何扩展基类而不在子类中注入
- css - 如何将 css 应用于特定组件?
- python-3.x - numpy.corrcoef() 对返回值的质疑
- sql - 使用 postgres CTE 从 EXISTS 查询运行 sql 函数
- javascript - 处理向上滚动和向下滚动Jquery
- c++ - Union 的内存映射
- ubuntu - 使用 VLC 捕获连续网络流,拆分并存储到多个文件