|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
from pygame.locals import *
import sys
#初始化pygame
pygame.init()
size = width, height = 600, 400
speed = [-2,1]
bg = [255, 255, 255] #RGB
fenbian = pygame.display.list_modes()
fullscreen = False
#创建指定大小窗口 surface
screen = pygame.display.set_mode(size, RESIZABLE)
#设置窗口标题
pygame.display.set_caption("第一个小游戏")
#加载图片
opic = pygame.image.load("E:\\pycharmproject\\SomeProject\\MyPygame\\gui.png")
pic = opic #创建图片副本
#获取图像位置矩形
opic_rect = opic.get_rect()
#获取远矩形宽,高
owidth = opic_rect.width
oheight = opic_rect.height
position = pic_rect = opic_rect #创建图片矩形位置副本
#设置图片朝向
turn_left = pic
turn_right = pygame.transform.flip(pic,True,False)
#图片放大倍数参数
radio = 1.0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
# 图片移动控制
if event.key == pygame.K_LEFT:
pic = turn_left
speed = [-1,0]
if event.key == pygame.K_RIGHT:
pic = turn_right
speed = [1, 0]
if event.key == pygame.K_DOWN:
speed = [0, 1]
if event.key == pygame.K_UP:
speed = [0, -1]
#全屏模式(Q)
if event.key == K_q:
fullscreen = not fullscreen
if fullscreen:
size = width, height = fenbian[0]
screen = pygame.display.set_mode(size, FULLSCREEN | HWSURFACE)
print(fenbian[0])
else:
size = width, height = 600, 400
screen = pygame.display.set_mode(size)
if position.right > 600 or position.bottom > 400:
position.topleft = (0,0)
#图片缩放控制(n,m,空格)
if event.key == K_n or event.key == K_m or event.key == K_SPACE:
#放大
if event.key == K_n and radio <= 2.0:
radio += 0.1
#缩小
if event.key == K_m and radio >= 0.5:
radio -= 0.1
#还原
if event.key == K_SPACE:
radio = 1.0
pic = pygame.transform.smoothscale(opic, (int(opic_rect.width * radio), int(opic_rect.height * radio)))
#调整矩形宽,高
position.width = owidth * radio
position.height = oheight * radio
turn_left = pygame.transform.flip(pic, True, False)
turn_right = pic
#调整窗口大小,自动适应
if event.type == VIDEORESIZE:
size = event.size
width, height = size
if size == fenbian[0]:
screen = pygame.display.set_mode((fenbian[0]), FULLSCREEN | HWSURFACE)
size = (600, 400)
else:
screen = pygame.display.set_mode(size, RESIZABLE)
#移动图像
position = position.move(speed)
#print(position)
#判断是否出界
if position.left < 0 or position.right > width:
#翻转图片
pic = pygame.transform.flip(pic,True,False)
#反方向移动
speed[0] = -speed[0]
if position.top < 0 or position.bottom > height:
speed[1] = -speed[1]
#填充背景
screen.fill(bg)
#更新图像
screen.blit(pic,position)
#更新界面
pygame.display.flip()
#延迟十毫秒
pygame.time.delay(20)
在缩放后调整图片尺寸的后面加上对当前运动方向的判断,决定turn_left turn_right 的赋值:
如果向左走:
if speed[0] < 0:
turn_left = pic
turn_right = pygame.transform.flip(pic, True, False)
else:
turn_right = pic
turn_left = pygame.transform.flip(pic, True, False)
|
|