用户:mengOuMi查看:0 回复:0 评论:0 创建时间:2021-04-26T21:11:47
【作品展示】

【作品介绍】
这个作业让一个穿民族服装的人载歌载舞在一个方框内碰撞,碰到边缘就反弹,并且翻转。
【作品源代码】
#导入pygame库
import pygame
pygame.init()
#绘制窗口
width=450
height=400
screen=pygame.display.set_mode((width,height))
#加载人物
hh=pygame.image.load('欢欢.png')
hh=pygame.transform.scale(hh,(100,100))
bg = pygame.image.load('广西.jfif')
# 初始化音乐模块
pygame.mixer.init()
pygame.mixer.music.set_volume(0.8) # 设置音量为 0.8
# 加载歌曲
pygame.mixer.music.load('刘三姐-云南山歌.mp3')
pygame.mixer.music.play() # 播放音乐
#绘制人物
screen.blit(bg,(0,0))
screen.blit(hh, (20, 20))
pygame.display.update()
#设置运动
speed = [-1, 1]
#获取欢欢的坐标值
hh_pos = hh.get_rect()
while True:
#绘制背景和人物
screen.blit(bg, (0, 0))
#人物在运动当中,hh_pos是一个变量
screen.blit(hh, hh_pos)
pygame.display.update()
#人物移动起来
hh_pos = hh_pos.move(speed) # 更新角色的位置
#判断是否撞到左右边缘
if hh_pos.left < 0 or hh_pos.right > width:
hh = pygame.transform.flip(hh, True, False)
speed[0] = -speed[0]
#判断是否撞到上下边缘
if hh_pos.top < 0 or hh_pos.bottom > height:
speed[1] = -speed[1]
#检测关闭窗口按钮
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn