使用python

时间:2019-03-20 11:44:12

标签: python animation graphics

所以我试图通过使用它来学习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()

0 个答案:

没有答案