|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pygame
- import sys
- import random
- #from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
- #初始化pygame
- pygame.init()
- size = width,height = 600,400
- speed = [-3,1]
- bg = (255,255,255)
- #创建窗口指定的大小
- screen = pygame.display.set_mode(size)
- #标题
- pygame.display.set_caption("案例")
- #加载图片
- picture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")
- #获取图像位置矩形
- position = picture.get_rect()
- fullscreen = False
- right_head = picture
- left_head = pygame.transform.flip(picture,True,False)
- up_head = pygame.transform.flip(picture,270,False)
- down_head = pygame.transform.flip(picture,False,270)
- 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_RIGHT:
- picture = left_head
- speed = [5,0]
- if event.key == pygame.K_LEFT:
- picture = right_head
- speed = [-5,0]
- if event.key == pygame.K_UP:
- picture = up_head
- speed = [0,-5]
- if event.key == pygame.K_DOWN:
- picture = down_head
- speed = [0,5]
- #全屏
- if event.key == pygame.K_F11:
- fullscreen = not fullscreen
- if fullscreen:
- size = width,height =1920,1080
- screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
- if position.bottom > height:
- positon.bottom = height
- if position.right > width:
- position.right = width
-
- [color=Red] else:
- #full screen = pygame.display.list_modes()
- size = width,height = 600,400
- screen = pygame.display.set_mode(size)
- screen.blit(picture,(random.randint(0,width),(random.randint(0,height))))[/color]
-
-
- #移动图像
- position = position.move(speed)
- if position.left < 0 or position.right > width:
- #翻转图像
- picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
- speed[0] = -speed[0]
- if position.top < 0 or position.bottom > height:
- speed[1] = -speed[1]
- #填充背景
- screen.fill(bg)
- #更新图像
- screen.blit(picture,position)
- #更新界面
- pygame.display.flip()
- #延时10毫秒
- pygame.time.delay(10)
复制代码
else 中当重新按下F11时 怎么把图像重置在退出全屏后的原屏幕里 求大佬求大佬求大佬
- import pygame
- import sys
- import random
- #from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
- #初始化pygame
- pygame.init()
- size = width,height = 600,400
- speed = [-3,1]
- bg = (255,255,255)
- #创建窗口指定的大小
- screen = pygame.display.set_mode(size,pygame.RESIZABLE)
- #标题
- pygame.display.set_caption("案例")
- #加载图片
- picture = pygame.image.load('C:/Users/ASUS/Desktop/1-14031F05541a7.png')
- #获取图像位置矩形
- position = picture.get_rect()
- fullscreen = False
- right_head = picture
- left_head = pygame.transform.flip(picture,True,False)
- up_head = pygame.transform.flip(picture,270,False)
- down_head = pygame.transform.flip(picture,False,270)
- 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_RIGHT:
- picture = left_head
- speed = [5,0]
- if event.key == pygame.K_LEFT:
- picture = right_head
- speed = [-5,0]
- if event.key == pygame.K_UP:
- picture = up_head
- speed = [0,-5]
- if event.key == pygame.K_DOWN:
- picture = down_head
- speed = [0,5]
- #全屏
- if event.key == pygame.K_F11:
- fullscreen = not fullscreen
- if fullscreen:
- size = width,height =1920,1080
- screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
- if position.bottom > height:
- positon.bottom = height
- if position.right > width:
- position.right = width
-
-
- else:
- size = width,height = 600,400
- screen = pygame.display.set_mode(size)
- if picture.get_width() < width and picture.get_height() < height:
- picture = picture
- position=picture.get_rect()
- #填充背景
- screen.fill(bg)
- screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
- #更新界面
- pygame.display.flip()
- pygame.time.delay(10)
-
- #用户调整窗口尺寸
- if event.type == pygame.VIDEORESIZE:
- size = event.size
- width,height = size
- screen = pygame.display.set_mode(size,pygame.RESIZABLE)
-
- #移动图像
-
- position = position.move(speed)
- if position.left < 0 or position.right > width:
- #翻转图像
- picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
- speed[0] = -speed[0]
- if position.top < 0 or position.bottom > height:
- speed[1] = -speed[1]
- #填充背景
- screen.fill(bg)
- #更新图像
- screen.blit(picture,position)
- #更新界面
- pygame.display.flip()
- #延时10毫秒
- pygame.time.delay(10)
复制代码
试试这样
记得把路径改下哦~
|
|