pygame机器人跳舞动画演示程序

"""pygame机器人跳舞动画演示程序,这个程序用到了图像变形功能,让几个机器人的大小和移动速度不一样。"""

import pygame
from pygame.locals import *
from random import randint

class Robot():
    def __init__(self,image,scale,x,y):
        rect = image.get_rect()        
        self.image = pygame.transform.scale(image,(rect.width*scale//100,rect.height*scale//100))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y        
        self.水平速度 = scale//10
        
    def move(self):
        self.rect.x = self.rect.x + self.水平速度
        if self.rect.x<0 or self.rect.right>screen_width: # 此处用到了全局变量
            self.水平速度 = - self.水平速度        
            
    def draw(self):
        screen.blit(self.image,self.rect)

pygame.init()
screen_width,screen_height=480,360
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("pygame机器人跳舞_作者:李兴球")
背景图片 = pygame.image.load("舞台背景.gif")
robotimage = pygame.image.load("robot1.gif")

allrobots=[]
r1 = Robot(robotimage,20,randint(50,screen_width-150),screen_height-180)
allrobots.append(r1)
r2 = Robot(robotimage,30,randint(50,screen_width-150),screen_height-170)
allrobots.append(r2)
r3 = Robot(robotimage,40,randint(50,screen_width-150),screen_height-160)
allrobots.append(r3)
r4 = Robot(robotimage,60,randint(50,screen_width-150),screen_height-150)
allrobots.append(r4)

You must log in to read the rest of this article. Please log in or register as a user.