猫史档案馆


【Python作品分享】夺宝奇兵【作品秀】

用户:开朗的董事长之晟唐CEO开朗的董事长之晟唐CEO查看:0 回复:0 评论:0 创建时间:2020-02-12T12:59:38


【作品展示】

center_image

 

【作品介绍】

这是一款“夺宝奇兵”游戏,欢迎大家来玩!也可以指出错误。

 

【作品源代码】

#导入库
import pygame
import random

#引入时钟
timer=pygame.time.Clock()

pygame.init()
#修改窗体大小
screen=pygame.display.set_mode((500,700))
pygame.display.set_caption("夺宝奇兵")
#减少阻塞
pygame.key.set_repeat(10)

#获取素材
background="background.png" #背景
mao_left="mao_left.png"     #编程猫(左)
mao_right="mao_right.png"   #编程猫(右)
coin="coin_喵all.png"       #金币


#加载基本素材
pygame.mixer.music.load("BGM.mp3")  #加载音乐
pygame.mixer.music.play(-1)         #重复不停播放

background_image=pygame.image.load(background)
mao_image = pygame.image.load(mao_left)
coin_image = pygame.image.load(coin)
success=pygame.mixer.Sound("success.wav")

font=pygame.font.SysFont("simHei",24)

coin_x=200
coin_y=-150
mao_x=200
mao_y=700-mao_image.get_height()
mao_move_x=0
score=0

keep_going=True
while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False

        if event.type==pygame.KEYDOWN:
            #按键判断
            if event.key==pygame.K_LEFT:
                mao_image=pygame.image.load(mao_left)
                mao_move_x=-4

            if event.key == pygame.K_RIGHT:
                mao_image = pygame.image.load(mao_right)
                mao_move_x =+4

        #左键抬起,就重置mao_move_x
        if event.type==pygame.KEYUP:
            if event.key==pygame.K_LEFT:
                mao_move_x=0
            if event.key == pygame.K_RIGHT:
                mao_move_x = 0

    #金币下落
    coin_y+=6
    mao_x+=mao_move_x

    #检测边缘
    if mao_x<0:
        mao_x=0
    elif mao_x>500-mao_image.get_width():
        mao_x=500-mao_image.get_width()

    if coin_y>mao_y:
        if(coin_x+coin_image.get_width()>mao_x) and (coin_x700:
        coin_x=random.randint(0,350)
        coin_y=-200

    screen.blit(background_image,(0,0))
    screen.blit(mao_image,(mao_x,mao_y))
    screen.blit(coin_image,(coin_x,coin_y))

    text=font.render("金币数:"+str(score),True,(255,215,0),(255,0,0))
    screen.blit(text,(350,10))

    pygame.display.update()
    timer.tick(60)

#退出游戏
pygame.quit()



 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页