猫史档案馆


【Python作品分享】何任荣-绘制背景和人物

用户:小河鱼小河鱼查看:0 回复:0 评论:0 创建时间:2021-05-09T22:27:50


【作品展示】

center_image

 

【作品介绍】

看着课件制作的,不知道对不对。

 

【作品源代码】

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')


while True:
    #绘制背景和人物
    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))
    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. tranform.flip(hh, True, False)
    speed[0] = -speed[0]
if hh_pos.top < 0 or hh_pos.botton > height:   
    speed[1] = -speed[1]

#初始化音乐模块
pygame.mixer.init()
pygame.mixer_music.set_volume(0.8) # 设置音量为0.8
pygame.mixer_music.load('刘三姐-云南山歌.mp3') # 加载歌曲
pygame.mixer_music.play() # 播放音乐

while True:
    #绘制背景和人物
    screen.blit(bg, (0, 0))
    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. tranform.flip(hh, True, False)
        speed[0] = -speed[0]
    if hh_pos.top < 0 or hh_pos.botton > height:
        speed[1] = -speed[1]

    #检测关闭窗口按钮
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页