猫史档案馆


【Python作品分享】捡金币【作品秀】

用户:PX一个努力的人PX一个努力的人查看:0 回复:0 评论:0 创建时间:2020-07-17T11:00:21


【作品展示】

center_image

 

【作品介绍】

这是一款简单游戏

pygame库代码制作(我太难了)

但是你们可能运行不起来

因为你们没有图片文件

 

【作品源代码】

# 库
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)  # 每10毫秒检查一次按键状态
coin_num=0

# 游戏素材
pygame.mixer.music.load("BGM.mp3")  # 加载mp3文件
pygame.mixer.music.play(-1)  # 循环放音乐
background = "background.png"  # 背景
m_l = "mao_left.png"  # 左方向编程猫
m_r = "mao_right.png"  # 右方向编程猫
money = "coin_喵all.png"  # 金币

# 加载基本素材
background_image = pygame.image.load(background)
mao_image = pygame.image.load(m_l)

coin_image = pygame.image.load(money)
success = pygame.mixer.Sound("success.wav")  # 加载音效

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

#创建字体对象
font=pygame.font.SysFont("SimHei",24)#黑体,大小24

# pygame的循环
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(m_l)
                mao_move_x = -4

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                mao_move_x = 0

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                mao_image = pygame.image.load(m_r)
                mao_move_x = 4

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                mao_move_x = 0

    if mao_x < 0:
        mao_x = 0
    elif mao_x > 500-mao_image.get_width():
        mao_x = 500-mao_image.get_width()
    # 金币下落速度
    coin_y += 6
    mao_x += mao_move_x
    # 金币跑出,重置
    if coin_y > 700:
        coin_y = -150
        coin_x = random.randint(5, 345)
    # 接住金币,重置
    if coin_y > mao_y and coin_y < 700:
        if coin_x+coin_image.get_width() > mao_x and coin_x < mao_x+mao_image.get_width():
            coin_num+=1#金币数加1
            coin_y = -150
            coin_x = random.randint(5, 345)
            success.play()  # 播放音效

    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(coin_num),True,(255,215,0),(255,255,255))#字样,是否抗锯齿(是),文字三原色(金色),文字背景三原色(白色)
    screen.blit(text,(360,15))

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

pygame.quit()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页