pygame.transform.rotate(Surface, angle)
逆时针旋转图片,角度为负数时表示顺时针。度数不是90°倍数时会自动用相近颜色进行填充。
rotate an image
rotate(surface, angle) -> Surface
Unfiltered counterclockwise rotation. The angle argument represents degrees and can be any floating point value. Negative angle amounts will rotate clockwise.
Unless rotating by 90 degree increments, the image will be padded larger to hold the new size. If the image has pixel alphas, the padded area will be transparent. Otherwise pygame will pick a color that matches the Surface colorkey or the topleft pixel value.
EG:
小黄人贴着边框移动:
import pygame
import sys
# initialize the pygame
pygame.init()
size = width, height = 1000, 800
bg = (255, 255, 255)
# set up the background
screen = pygame.display.set_mode(size)
# set up the title
pygame.display.set_caption('A Moving Minions')
# load the picture
minions = pygame.image.load('minions.jpg')
# get the location of picture
speed = [5, 0]
# rotate the pic
minions_right = pygame.transform.rotate(minions, 90)
minions_top = pygame.transform.rotate(minions, 180)
minions_left = pygame.transform.rotate(minions, 270)
minions_bottom = minions
position = minions.get_rect()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
# catch up the QUIT event to stop the loop
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
minions = pygame.transform.flip(minions, True, False)
speed[0] = -2
if event.key == pygame.K_RIGHT:
speed[0] = 2
if event.key == pygame.K_UP:
speed[1] = -2
if event.key == pygame.K_DOWN:
speed[1] = 2
# start moving
position = position.move(speed)
print(position.right, position.bottom, position.left, position.top)
# make the photo is parallel with border
if position.right > width:
minions = minions_right
position = minions.get_rect()
position.left = width - position.width
speed = [0, 5]
if position.bottom > height:
minions = minions_bottom
position = minions.get_rect()
position.left = width - position.width
position.top = height - position.height
speed = [-5, 0]
if position.left < 0:
minions = minions_left
position = minions.get_rect()
position.top = height - position.height
speed = [0, -5]
if position.top < 0:
minions = minions_top
position = minions.get_rect()
position.right = position.width
speed = [5, 0]
# fill the background
screen.fill(bg)
# put the pic above the screen
screen.blit(minions, position)
# refresh the screen using cache data
pygame.display.flip()
# change move speed
#pygame.time.delay(10)
clock.tick(90)