Python 7 Levels, L6, Unit 59, Animated Sprites

Powered By EmbedPress

Exercise 1:

import pygame
class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location):
        pygame.sprite.Sprite.__init__(self)#Initiize animation sprite
        self.image = pygame.image.load(image_file)#load image
        self.rect = self.image.get_rect()#get the rectangle of the image
        self.rect.left,self.rect.top = location#initial position of the ball

pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
balls = []
for x in range(3):
    for y in range(3):
        ball = MyBallClass("ball.png",[x*150+100,y*150+50])
        balls.append(ball)
for ball in balls:
    screen.blit(ball.image,ball.rect)
pygame.display.update()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

Exercise 2:

import pygame
class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)#Initiize animation sprite
        self.image = pygame.image.load(image_file)#load image
        self.rect = self.image.get_rect()#get the rectangle of the image
        self.rect.left,self.rect.top = location#initial position of the ball
        self.speed = speed

    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = -self.speed[0]
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

pygame.init()
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
pygame.display.update()
balls = []
for x in range(3):
    for y in range(3):
        ball = MyBallClass("ball.png",[x*150+100,y*150+50],[20,-20])
        balls.append(ball)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    for ball in balls:
        screen.blit(ball.image,ball.rect)
        ball.move()
    pygame.display.update()
    pygame.time.delay(200)
    screen.fill([255,255,255])
pygame.quit()

Exercise 3:

import pygame
class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)#初始化动画精灵
        self.image = pygame.image.load(image_file)#加载图片文件
        self.rect = self.image.get_rect()#通过图片属性得到图像边界的矩形
        self.rect.left,self.rect.top = location#设置球的初始位置
        self.speed = speed
    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = -self.speed[0]
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]

pygame.init()
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
#balls = []#空列表,用于存储球对象
group = pygame.sprite.Group()
for x in range(3):
    for y in range(3):
        ball = MyBallClass("ball.png",[100+x*150,50+y*150],[2,-2])
        group.add(ball)

pygame.display.update()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    for ball in group:
        screen.blit(ball.image,ball.rect)
        ball.move()
    for ball in group:
        group.remove(ball)
        if pygame.sprite.spritecollide(ball,group,False):
            ball.speed[0] = -ball.speed[0]
            ball.speed[1] = -ball.speed[1]
        group.add(ball)
        
    pygame.display.update()
    pygame.time.delay(20)
    screen.fill([255,255,255])
pygame.quit()

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts