與小卡特一起學python 第17章動畫精靈和碰撞檢測

yarking207發表於2016-04-11
#17-1 使用動畫精靈在螢幕上放多個沙灘球影像
import sys,pygame

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location):
        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

size = width,heigh = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
balls = []
for row in range (0,3):
    for column in range (0,3):
        location = [column *180 +10,row * 180 +10]
        ball = MyBallClass(img_file,location)
        balls.append(ball)
for ball in balls:
    screen.blit(ball.image,ball.rect)
pygame.display.flip()

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

#17-2 使用動畫精靈移動球的程式
import sys,pygame
from random import *

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]
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
balls = []
for row in range (0,3):
    for column in range (0,3):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-2,2]),choice([-2,2])]
        ball = MyBallClass(img_file,location,speed)
        balls.append(ball)

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

#17-3 使用精靈組和不是列表

import sys,pygame
from random import *

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]

def animate(group):
    screen.fill([255,255,255])
    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)
        ball.move()
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    pygame.time.delay(50)
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
group = pygame.sprite.Group()

for row in range (0,2):
    for column in range (0,2):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-5,5]),choice([-5,5])]
        ball = MyBallClass(img_file,location,speed)
        group.add(ball)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    animate(group)
pygame.quit()

#17-3修改不會卡和顫抖

import sys,pygame
from random import *

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]

def animate(group):
    screen.fill([255,255,255])
    for ball in group: #先移動所有球
        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)
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    pygame.time.delay(20)
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
group = pygame.sprite.Group()

for row in range (0,2):
    for column in range (0,2):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-5,5]),choice([-5,5])]
        ball = MyBallClass(img_file,location,speed)
        group.add(ball)
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    animate(group)
pygame.quit()
#17-4沙灘球程式中使用clock和get_fps

import sys,pygame
from random import *

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]

def animate(group):
    screen.fill([255,255,255])
    for ball in group: #先移動所有球
        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)
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()
    
size = width,height = 640,480
screen =pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
clock = pygame.time.Clock()
group = pygame.sprite.Group()

for row in range (0,2):
    for column in range (0,2):
        location = [column *180 +10,row * 180 +10]
        speed = [choice([-5,5]),choice([-5,5])]
        ball = MyBallClass(img_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(10)
pygame.quit()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/220205/viewspace-2079302/,如需轉載,請註明出處,否則將追究法律責任。

相關文章