|  | 
 
| 
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册    
 
 复制代码import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
    def __init__(self,image,position,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(image).convert_alpha()
        self.rect=self.image.get_rect()
        self.rect.left, self.rect.top=position
        self.speed=speed
    def move(self):
        self.rect=self.rect.move(self.speed)
def main():
    pygame.init()
    ball_img='ball.png'
    bg_img='bg.png'
    running=True
    bg_size=width,height=1357,637
    screen=pygame.display.set_mode(bg_size)
    pygame.display.set_caption('play the ball')
    bg=pygame.image.load(bg_img).convert_alpha()
    balls=[]
    for i in range(3):
        position=randint(0, width-100),randint(0,height-100)
        speed=randint(-10, 10),randint(-10, 10)
        ball=Ball(ball_img, position, speed)
        balls.append(ball)
    clock=pygame.time.Clock()
    while running:
        for event in pygame.event.get():
            if event.type ==QUIT:
                sys.exit()
        
        screen.blit(bg, (0,0))
        for each in balls:
            each.image=pygame.transform.smoothscale(each.image, (each.rect.width*0.5,each.rect.height*0.5))
            each.move()
            screen.blit(each.image, each.rect)
        pygame.display.flip()
        clock.tick(30)
if __name__ =="__main__":
    main()
将你的代码 28 行改成     bg = pygame.image.load(bg_img).convert(),参考代码:
 
 
 复制代码import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
    def __init__(self, image, position, speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = position
        self.speed = speed
    def move(self):
        self.rect = self.rect.move(self.speed)
def main():
    pygame.init()
    ball_img = 'ball.png'
    bg_img = 'bg.png'
    running = True
    bg_size = width, height = 1357, 637
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption('play the ball')
    bg = pygame.image.load(bg_img).convert()
    balls = []
    for i in range(3):
        position = randint(0, width - 100), randint(0, height - 100)
        speed = randint(-10, 10), randint(-10, 10)
        ball = Ball(ball_img, position, speed)
        balls.append(ball)
    clock = pygame.time.Clock()
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
        screen.blit(bg, (0, 0))
        for each in balls:
            each.image = pygame.transform.smoothscale(each.image, (each.rect.width * 0.5, each.rect.height * 0.5))
            each.move()
            screen.blit(each.image, each.rect)
        pygame.display.flip()
        clock.tick(30)
if __name__ == "__main__":
    main()
 
 | 
 |