|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pygame
- import sys #退出程序时要用
- #初始化pygame,他是一个包
- pygame.init()
- size= width,height = 600,400
- speed = [-2,1] #x每次往左走2,Y向下偏移1格
- bg=(255,255,255) #背景填充为白色
- #创建指定大小的窗口
- screen=pygame.display.set_mode(size)
- #设置窗口的标题
- pygame.display.set_caption("初次见面,请多指教")
- #加载图片
- a_cartoon=pygame.image.load("a.gif")
- #获取图像的位置矩形
- position = a_cartoon.get_rect()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- #移动图像
- position = position.move(speed)
- if position.left<0 or position.right > width:
- #反转图像,True表示水平翻转,False表示不垂直翻转
- a_cartoon=pygame.transform.flip(a_cartoon,True,False)
- #反方向移动
- speed[0] = -speed[0]
- if position.top<0 or position.bottom > height:
- speed[1] = -speed[1]
-
- #填充背景色
- screen.fill(bg)
- #更新图片,blit将一个图像画到另一个图像上去,a_cartoon画到screen)
- screen.blit(a_cartoon,position)
- #延迟
- pygame.time.delay(10)
-
复制代码
可能是电脑配置问题,我这边黑屏一会就好了
- import pygame
- import sys
- # 初始化pygame
- pygame.init()
- size = width, height = 600, 400
- speed= [-2, 1]
- bg = (255, 255, 255)
- clock = pygame.time.Clock()
- # 创建指定大小地窗口 Surface
- screen = pygame.display.set_mode(size)
- # 设置窗口标题
- pygame.display.set_caption("初次见面,请大家多多关照!")
- # 加载图片
- turtle = pygame.image.load("turtle.png")
- # 获得图像的位置矩形
- position = turtle.get_rect()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- # 移动图像
- position = position.move(speed)
- if position.left < 0 or position.right > width:
- # 翻转图像
- turtle = pygame.transform.flip(turtle, True, False)
- # 反方向移动
- speed[0] = -speed[0]
- if position.top < 0 or position.bottom > height:
- speed[1] = -speed[1]
- # 填充背景
- screen.fill(bg)
- # 更新图像
- screen.blit(turtle, position)
- # 更新界面
- pygame.display.flip()
- # 延迟10毫秒
- # pygame.time.delay(10)
- clock.tick(200)
复制代码
|
|