猫史档案馆


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

用户:滚动的天空大神滚动的天空大神查看:0 回复:0 评论:0 创建时间:2020-03-25T21:23:15


【作品展示】

center_image

 

【作品介绍】

小游戏

 

【作品源代码】

#
import pygame
import random
import time

#
timer = pygame.time.Clock()

pygame.init()
screen = pygame.display.set_mode((500, 700))
pygame.display.set_caption("夺宝奇兵1")
#
pygame.key.set_repeat(1)


#
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",29)


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_a:
                mao_move_x = -4
                mao_image = pygame.image.load(mao_left)
            if event.key == pygame.K_d:
                mao_image = pygame.image.load(mao_right)
                mao_move_x = +4

        #
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                mao_move_x = 0
            if event.key == pygame.K_d:
                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_x < mao_x + mao_image.get_width()):
            coin_x = random.randint(0, 350)
            coin_y = -150
            print("捡到金币了!")
            success.play()
            score +=1
            print("你现在有:",score,"个金币!")

    #


    #
    if coin_y > 700:
        coin_x = random.randint(0, 350)
        coin_y = -150

    screen.blit(background_image, (0, 0))
    screen.blit(mao_image, (mao_x, mao_y))
    screen.blit(coin_image, (coin_x, coin_y))
    if score <= 1:
        print("1")
        # text = font.render("青铜阶段:您现有的金币数:" + str(score), True, (255, 0, 0), "个")
    elif score <= 2:
        text = font.render("白银阶段:您现有的金币数:" + str(score),True, (255, 0, 0), "个")
    elif score <= 3:
        text = font.render("黄金阶段:您现有的金币数:" + str(score),True, (255, 0, 0), "个")
    elif score <= 4:
        text = font.render("铂金阶段:您现有的金币数:" + str(score),True, (255, 0, 0), "个")
    elif score <= 5:
        text = font.render("钻石阶段:您现有的金币数:" + str(score),True, (255, 0, 0), "个")
    else:
        text = font.render("王者阶段:您现有的金币数:" + str(score),True, (255, 0, 0), "个")

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


#
pygame.quit()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页