Animation Elves and Collision Detection

Keywords: PHP

Animation Wizard: A group of pixels that are moved and displayed as a unit. This is a graphical object.

1.1 The animation Genie imagines a small picture, a graphic object that can move on the screen and interact with other objects.

1.2 Animation Elves generally have two basic attributes: image (picture displayed for animation elves) rectangular area (rectangular area containing animation elves)

2. Sprite class

# author : wang cheng hua
# sprite
#Program for Moving Balls Using Animated Elves
import  pygame ,sys
from random import *

class MYBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)             # Initialize animation wizard
        self.image = pygame.image.load(image_file)      #  Load image files into it
        self.rect = self.image.get_rect()               # Get the rectangle that defines the image boundary
        self.rect.left,self.rect.top = location         # Setting the initial position of the ball
        self.speed = speed

    def move(self):
        self.rect = self.rect.move(self.speed)          # Animation genie (actually one of them) rect)There is a built-in move Function. This method requires a speed The parameter tells it how far to move the object (move multiple blocks)
        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]


size = width,height = 640,480                           # Set the window size and define two variables
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])                              # Background fill color
image_file = "beach_ball.png"                           # give image_file Contents of References
balls =[]                                               #  Create a list of placed balls
speed = [2,2]                                           # Create a speed list
for row in range(0,3):                                  # Make the first outer loop of three layers
    for column in range(0,3):                           # Three runs of internal circulation
        location = [row*180 +10,column*180+10]          # Change the initial position of each running ball
        speed = [choice([-2,2]),choice([-2,2])]                 # random Li choice Random Selection of Functions speed
        ball = MYBallClass(image_file,location,speed)         # Create a ball Examples
        balls.append(ball)                              # Add the ball to the list
for ball in balls:                                      # Extract each ball from the table for replication
    screen.blit(ball.image,ball.rect)
pygame.display.flip()                                   # Display the replicated information in the window

3. Collision detection

# collision detection

import  pygame ,sys
from random import *

class MYBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)             # Initialize animation wizard
        self.image = pygame.image.load(image_file)      #  Load image files into it
        self.rect = self.image.get_rect()               # Get the rectangle that defines the image boundary
        self.rect.left,self.rect.top = location         # Setting the initial position of the ball
        self.speed = speed

    def move(self):
        self.rect = self.rect.move(self.speed)          # Animation genie (actually one of them) rect)There is a built-in move Function. This method requires a speed The parameter tells it how far to move the object (move multiple blocks)
        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]
def animate(group):
    screen.fill([255,255,255])
    for ball in group:
        ball.move()             # Move all the balls first
    for ball in group:
        group.remove(ball)      # Delete Elves from Groups
        if pygame.sprite.spritecollide(ball,group,False):    # Check for collisions between elves and groups
            ball.speed[0] = -ball.speed[0]
            ball.speed[1] = -ball.speed[1]
        group.add(ball)        # Add the ball back to the original group
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    pygame.time.delay(20)

size = width,height = 640,480                           # Start of the main program: Set the window size and define two variables
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])                              # Background fill color
image_file = "b_ball_rect.png"                           # give image_file Contents of References
group = pygame.sprite.Group()                          # Create Elf Groups

for row in range(0,2):                                  # Make the first outer loop of two layers
    for column in range(0,2):                           # Two runs of internal circulation
        location = [row*180 +10,column*180+10]          # The initial position of each running ball is changed; the ball is sorted in three rows with an interval of 90)
        speed = [choice([-2,2]),choice([-2,2])]                 # random Li choice Random Selection of Functions speed
        ball = MYBallClass(image_file,location,speed)         # Create a ball Examples
        group.add(ball)                             # Add the ball to the list
for ball in balls:                                      # Extract each ball from the table for replication
    screen.blit(ball.image,ball.rect)
pygame.display.flip()                                   # Display the replicated information in the window


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

    animate(group)
pygame.quit()
collision detection

4. Clock and get_fps() are used in beach ball program.

# Use in Beach Ball Program clock()and get_fps()

import pygame,sys
from random import  *

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,speed):
        pygame.sprite.Sprite__init__(self)
        self.image_file = 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]
def animate(group):
    screen.fill([255,255,255])
    for ball in group:
        ball.move()             # Move all the balls first
    for ball in group:
        group.remove(ball)      # Delete Elves from Groups
        if pygame.sprite.spritecollide(ball,group,False):    # Check for collisions between elves and groups
            ball.speed[0] = -ball.speed[0]
            ball.speed[1] = -ball.speed[1]
        group.add(ball)        # Add the ball back to the original group
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()                # Deleted time.delay()

size = width,height=640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
image_file =  "b_ball_rect.png"
clock = pygame.time.Clock()            # Establish clock Example
group = pygame.sprite.Group()
for row in range (2):
    for column in range (2):
        location = [column*180+10,row*180+10]
        speed=[choice([-5,5]),choice([-5,5])]
        ball = MYBallClass(image_file,location,speed)
        group.add(ball)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            frame_rate = clock.get_fps()
            print("frame_rate = ",frame_rate)
        animate(group)
        clock.tick(30)
pygame.quit()
Clock and get_fps()

Posted by tastro on Wed, 31 Jul 2019 07:06:49 -0700