1. 程式人生 > >Python——pygame影象處理(二)

Python——pygame影象處理(二)

上接: Python——pygame影象處理(一)

4. 一直移到最右邊:

import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_dog = pygame.image.load("timg.jpg")
x = 50
y = 50
screen.blit(my_dog,[x,y])
pygame.display.flip()
for looper in range(1,100):
    pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
    x = x + 5
    screen.blit(my_dog,[x,y])
    pygame.display.flip()

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

在這裡插入圖片描述

如果增加x,也就是讓小狗一直往右移,會怎麼樣呢?即換為:

for looper in range(1,200):

最後發現小狗消失了!因為我們開啟的視窗x = 640就到頭了。

5. 一維反彈

如果我們想讓小狗在視窗的邊界反彈,就要知道它什麼時候“碰到”視窗邊界,然後讓它朝反方向移動。如果想讓它一直來回移動,就要在視窗左右兩邊都做同樣的處理。

左邊界很容易,只要檢查小狗的位置是不是等於0(或者某個很小的數)。

右邊界,就要檢視小狗是不是在視窗的右邊界上。不過小狗的邊界是按它的左邊界,而不是右邊界設定的,所以必須減去小狗的寬度。

程式如下:

import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_dog = pygame.image.load("timg.jpg")
x = 50
y = 50
x_speed = 10

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

    pygame.time.delay(20)
    pygame.draw.rect(screen,[255,255,255],[x,y,150,100],0)  #檢查小狗是否在視窗邊界
    x = x + x_speed #方向反轉
    if x > screen.get_width() - 100 or x < 0:
        x_speed = - x_speed

    screen.blit(my_dog,[x,y])
    pygame.display.flip()

pygame.quit()

6.在2-D空間中反彈

很簡單,只需要在x的基礎上加上y座標:

import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_dog = pygame.image.load("timg.jpg")
x = 50
y = 50
x_speed = 10
y_speed = 10

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


    pygame.time.delay(20)
    pygame.draw.rect(screen,[255,255,255],[x,y,100,80],0)
    x = x + x_speed
    y = y + y_speed
    if x > screen.get_width() - 100 or x < 0:
        x_speed = - x_speed
    if y > screen.get_height() - 80 or y < 0:
        y_speed = - y_speed   

    screen.blit(my_dog,[x,y])
    pygame.display.flip()

pygame.quit()

7. 讓影象翻轉

不讓小狗在螢幕邊界反彈,而是讓它翻轉。這表示,小狗在右邊界消失時,又會在左邊界重新出現。先看水平方向的情況:

import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_dog = pygame.image.load("timg.jpg")
x = 50
y = 50
x_speed = 5

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

    pygame.time.delay(20)
    pygame.draw.rect(screen,[255,255,255],[x,y,100,80],0)
    x = x + x_speed

    if x > screen.get_width():
        x = 0
    
    screen.blit(my_dog,[x,y])
    pygame.display.flip()

pygame.quit()

但這是它是突然“閃入”到左邊,強迫症看著特別不舒服,修改程式為“滑入”試試:

import pygame,sys
pygame.init()
screen = pygame.display.set_mode([640,480])
screen.fill([255,255,255])
my_dog = pygame.image.load("timg.jpg")
x = 50
y = 50
x_speed = 5

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

    pygame.time.delay(20)
    pygame.draw.rect(screen,[255,255,255],[x,y,100,80],0)
    x = x + x_speed

    if x > screen.get_width():
        x = -100

    screen.blit(my_dog,[x,y])
    pygame.display.flip()

pygame.quit()