pygame动画可爱的Pico 演示NPC人物,作者:李兴球

版权声明:所有文章皆为李兴球先生原创,转载请注明出处,否则保留权利,追究责任。 https://blog.csdn.net/avskya/article/details/81323752

import pygame
from pygame.locals import *
from random import choice,randint
import time

pygame.init()
screen = pygame.display.set_mode((480,360))
pygame.display.set_caption("可爱的Pico 演示NPC人物,作者:李兴球")

picoRight=[]
picoRight.append(pygame.image.load("0右.png"))
picoRight.append(pygame.image.load("1右.png"))
picoRight.append(pygame.image.load("2右.png"))
picoRight.append(pygame.image.load("3右.png"))


picoLeft=[]
picoLeft.append(pygame.image.load("0左.png"))
picoLeft.append(pygame.image.load("1左.png"))
picoLeft.append(pygame.image.load("2左.png"))
picoLeft.append(pygame.image.load("3左.png"))


class Pico():
    counter = 0
    def __init__(self,picoRight,picoLeft):
        self.rightList = picoRight           #向右走的图形列表
        self.leftList = picoLeft             #向左走的图形列表
        self.list = [self.leftList,self.rightList]
        self.heading = choice([0,1])        #朝向,1表示为右,0为左
        self.index = 0                      #走动的图形列表索引
        self.image = self.list[self.heading][self.index]
        self.rect = self.image.get_rect()
        self.rect.x = randint(0,430)
        self.rect.y = randint(150,300)
        if self.heading==0:
            self.xspeed = -2
        else:
            self.xspeed = 2
        self.walkStartTime=time.time()
        self.walkdelay = 0.1
        self.切换造型起始时间 = time.time()
        self.切换造型延时 = 0.1
        Pico.counter = Pico.counter + 1 
        self.暂停事件 = pygame.USEREVENT + Pico.counter
        self.暂停  = False
    def move(self):
        if self.暂停==False:
            if time.time()-self.walkStartTime > self.walkdelay:
               self.rect.x = self.rect.x + self.xspeed
               if self.rect.x<=0 or self.rect.right>480:
                   self.xspeed = -self.xspeed
                   self.heading = 1 - self.heading
               self.walkStartTime=time.time()
    def 下一个造型(self):
        if self.暂停==False:
            if time.time()-self.切换造型起始时间>self.切换造型延时:
                self.index  = self.index + 1
                self.index = self.index % 4
                self.image = self.list[self.heading][self.index]
                self.切换造型起始时间  = time.time()
    def draw(self):
        screen.blit(self.image,self.rect)

def 播放背景音乐():
    pygame.mixer.init()
    pygame.mixer.music.load("OpusOne.wav")
    pygame.mixer.music.play(-1,0)
    
def main():
    
    背景图 = pygame.image.load("stage.png")
    Picos = [Pico(picoRight,picoLeft),Pico(picoRight,picoLeft),Pico(picoRight,picoLeft),Pico(picoRight,picoLeft),Pico(picoRight,picoLeft)]
    for pico in Picos:
        pygame.time.set_timer(pico.暂停事件, randint(960,3600)) # 随机时间 触发

    clock = pygame.time.Clock()
    运行中= True
    while 运行中:
        for event in pygame.event.get():
            if event.type == QUIT:运行中 = False
            for pico in Picos:
                if event.type == pico.暂停事件:
                    pico.暂停 = not pico.暂停
                    pygame.time.set_timer(pico.暂停事件, randint(960,3600)) # 随机时间 触发
                    #下面这几句话是让pico有时改变方向
                    r = randint(0,1)
                    pico.heading = r
                    if pico.heading==0:
                        pico.xspeed = -2
                    else:
                        pico.xspeed = 2
                
        screen.blit(背景图,(0,0))
        for pico in Picos:
            pico.move()
            pico.下一个造型()
            pico.draw()
        pygame.display.update()
        clock.tick(30)
    pygame.quit()

if __name__ == "__main__":

    播放背景音乐()
    main()

    
        
        
 

猜你喜欢

转载自blog.csdn.net/avskya/article/details/81323752
今日推荐