python小窗口展示照片 idors practice

偶像练习生照片在小窗口框框里快速移动

【代码】

import pygame
import sys
from pygame.locals import *

# 初始化Pygame
pygame.init()

clock = pygame.time.Clock()
size = width, height = 600, 600  # 窗口 实际上是元组
speed = [-2, 1]
bg = (155, 205, 255)  # rgb,自定义吧

biggestsize = pygame.display.list_modes()[0]
fullscreen = False

# 创建指定大小的窗口
screen = pygame.display.set_mode(size)
# 设置标题
pygame.display.set_caption("idolers")
# 加载图
idors = pygame.image.load("idol practice.png")
# 获得图像位置矩形
position = idors.get_rect()

l_head = idors
r_head = pygame.transform.flip(idors, True, False)

# 死循环,一直动
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == KEYDOWN:  # 需要from pygame.locals import *
            if event.key == K_LEFT:
                idors = l_head
                speed = [-1, 0]
            if event.key == K_RIGHT:
                idors = r_head
                speed = [1, 0]
            if event.key == K_UP:
                speed = [0, -1]
            if event.key == K_DOWN:
                speed = [0, 1]
                          # move
    position = position.move(speed)
#注意这里的条件是 while true 严格注意缩写,同时先前有报错, argument must contain two numbers是什么原因?

    if position.left < 0 or position.right > width:
        # 水平翻转
        idors = pygame.transform.flip(idors, True, False)# r_head 水平true,垂直false
        #思考如何让他发出boom的特效呢??
        # 反向
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]

        # 填充背景
    screen.fill(bg)
    # 更新图像
    screen.blit(idors, position)
    # 更新界面(双缓冲)
    pygame.display.flip()
    # 延迟10ms
    # pygame.time.delay(20)
    clock.tick(100)  # 不高于100帧

【实现结果】

python小窗口展示照片 idors practice

python小窗口展示照片 idors practice

思考:怎么在照片碰到左右边缘反弹的时候发出特效吧?

感想:还是要多掌握语法才能熟练使用。