與小卡特一起學python 第17章動畫精靈和碰撞檢測
#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()
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 與小卡特一起學python 第14章 物件Python物件
- 與小卡特一起學python 第19章 聲音Python
- 與小卡特一起學python 第20章 使用pyqtPythonQT
- 與小卡特一起學python 第16章 圖形 Pygame學習PythonGAM
- 與小卡特一起學python 第5章 輸入 測試題和動手試一試Python
- 與小卡特一起學python 第13章 函式-積木Python函式
- 與小卡特一起學python 第3章 基本數學運算Python
- 與小卡特一起學python 第21章 列印格式化與字串Python字串
- 與小卡特一起學python 第4章 資料的型別Python型別
- 與小卡特一起學python 第11章 巢狀與可變迴圈Python巢狀
- 與小卡特一起學python 第22章 檔案輸入與輸出Python
- 與小卡特一起學python 第8章 轉圈圈 FOR迴圈和條件迴圈Python
- 與小卡特一起學python 第14章 物件 動手試一試Python物件
- 與小卡特一起學python 第9章 全都為了你-註釋Python
- 與小卡特一起學Python 第15章 模組 及動手試一試Python
- 與小卡特一起學python 第18章 一種新的輸入-事件Python事件
- 與小卡特一起學python 第8章 動手試一試原始碼Python原始碼
- 與小卡特一起學python 第6章 gui-圖形使用者介面 測試和動手試一試PythonGUI
- 與小卡特一起學python 第10章 遊戲時間到了 程式碼清單Python遊戲
- 與小卡特一起學python 第1章 出發吧 課後練習題Python
- 與小卡特一起學python 第13章 函式-積木 動手試一試Python函式
- 與小卡特一起學python 第1章 出發吧 1-2猜數遊戲Python遊戲
- 與小卡特一起學python 第12章 收集起來,列表與字典 動手試一試Python
- Html5 Canvas動畫基礎(碰撞檢測)HTMLCanvas動畫
- 與小卡特一起學python 第2章 記住記憶體和變數 2-1練習Python記憶體變數
- 學習 PixiJS — 動畫精靈JS動畫
- 與小卡特一起學python 第11章 巢狀與可變迴圈 動手試一試Python巢狀
- 碰撞檢測
- 與小卡特一起學python 第2章 記住記憶體和變數 課後 動手試一試Python記憶體變數
- 學習 PixiJS — 碰撞檢測JS
- SpriteSheet精靈動畫引擎動畫
- 與小卡特一起學python 第7章 判斷再判斷 7-1-2-3-6-7Python
- python如何檢測pygame中的碰撞PythonGAM
- 與小卡特一起學python 第5章 輸入 5-1,2,3,4 input()輸入函式Python函式
- 與小卡特一起學python 第10章 遊戲時間到了 pygame安裝及素材圖片準備Python遊戲GAM
- 簡單前端——圓與圓碰撞檢測前端
- 簡單介紹Pygame 精準檢測影像碰撞的問題GAM
- 網頁小實驗——用canvas生成精靈動畫圖片網頁Canvas動畫