用Python创建动态图像

2024-04-24 16:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试着用它来学习python,我开始编写著名的DVD\u 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()

Tags: trueiftimebackrandomrgbpygamewin