【无标题】

本文作者分享了自己学习Python和pygame的过程中制作水果机游戏的历程,从最初的print()版本到现在的美化版。文章介绍了游戏构思分析,包括押注键、分数显示区和运行原理,并提供了代码资源链接供读者下载体验。
摘要由CSDN通过智能技术生成

水果机美化版来了
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

玩游戏,玩游丧志,其实不然。
大家其实把玩游戏的定义误解了,什么叫玩游戏,开发游戏的人才叫玩游戏。那些把玩游戏的,才是被游戏玩的人。
其实写这个程序也是很偶然的。某天想在网络上下个水果机小游戏,发现没有,要吗是广告病毒,要吗是收费的代码。那时就有了自己写个的想法,也是那时起自摸学习python,pygame
一步步从print()打印版,到pygame无图版,到现在的美化版。虽然程序说不上完美,当这个过程中我学到了很多。
感兴趣的码友可以去下载
地址:https://download.csdn.net/download/weixin_52124806/86894117

下面来给大家讲解游戏的思路和步骤


提示:以下是本篇文章正文内容,下面案例可供参考

一、构思分析

一张水果机背景图,可控14个键:
8个押注键,←,→,大,小,开始,全部移分
5大分数显示区:得分区,积分区,彩金区,大小结果区,押注区
游戏的运行原理:
押注区押分,跑环,出结果,要吗得分,要吗没得分(废话)

整体代码如下




# 复制代码就能运行,如果无法运行就可能是下面两种情况
# 1:没装pygame插件
# 解决:运行此代码 安装
# pip3 install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple
# 2: pip 版本太低
# 解决:运行此代码 升级pip
# python -m pip install --upgrade pip  


import pygame,sys,random
from pygame.locals import *


class MyMap():           # 背景图
    def __init__(self):
        self.bg = pygame.image.load('image/beijin.png').convert_alpha()  # 背景图
    def map_update(self):
        screen.blit(self.bg,(0,0))

class Numbers():         # 数字显示
    def __init__(self): 
        self.numbers = (pygame.image.load('image/0.png').convert_alpha(),
                       pygame.image.load('image/1.png').convert_alpha(), 
                       pygame.image.load('image/2.png').convert_alpha(),
                       pygame.image.load('image/3.png').convert_alpha(),
                       pygame.image.load('image/4.png').convert_alpha(),
                       pygame.image.load('image/5.png').convert_alpha(),
                       pygame.image.load('image/6.png').convert_alpha(),
                       pygame.image.load('image/7.png').convert_alpha(),
                       pygame.image.load('image/8.png').convert_alpha(),
                       pygame.image.load('image/9.png').convert_alpha())  
    def showscore(self,numbe,x,y):
        dig = [int(x) for x in list(str(numbe))]         # 获取数字信息
        width = x                                        # x轴初始位置
        for digi in dig:
            width += self.numbers[digi].get_width()      # 获取数字图宽度
        xoffset = (600 - (width))                        # 数字在x轴的位置
        for digi in dig:
            screen.blit(self.numbers[digi],(xoffset,y))  # 显示数字图像
            xoffset += self.numbers[digi].get_width()    # 更新数字位置

class start_up():                                             # 跑环  
    def __init__(self):
        self.st_up_png1 = pygame.image.load('image/dak.png').convert_alpha()    # 大框
        self.st_up_png2 = pygame.image.load('image/xik.png').convert_alpha()    # 小框
        self.st_up_audio = pygame.mixer.Sound('audio/score.wav')                # 单击音
    
    def print_text(self,font,x,y,text,color=(255,255,255)):     
        imgText = font.render(text,True,color)      # 文字
        screen.blit(imgText,(x,y))                  # 显示

    def draw_png1(self,x,y):                                  # 绘制大框
        screen.blit(self.st_up_png1,(x,y))
    def draw_png2(self,x,y):                                  # 绘制小框
        screen.blit(self.st_up_png2,(x,y))

    def paoh(self,x,y):                                     # 正常跑环
        screen.blit(self.st_up_png1,(x,y))    

    def sanyao(self,syao,x,y):                          # 闪灯
        if syao == 1 :
            screen.blit(self.st_up_png1,(x,y))
       
    def scores_0 (self,rand,speed):                        # 普通跑环得分函数 
        score = 0
        score_nb =0
        score_s =(stake_1*5,stake_2*2,stake_2*10,stake_5*20,stake_5*2,
                  0,stake_1*5,stake_2*2,stake_2*10,stake_4*10,stake_6*2,
                  stake_6*20,stake_1*5,stake_3*2,stake_3*10,stake_7*20,
                  stake_7*2,0,stake_1*5,stake_4*2,stake_2*10,stake_4*10,
                  stake_8*50,stake_8*100
                  )
        for i in score_s :
            score_nb +=1
            if rand == score_nb:
                score += i

        if  rand == 6:
            if rans == 5:
                score += speed
                speed = 0
            else:
                score += 0 

        if rand == 18:
            if rans == 0:                      # 大四喜赔率
                score = score+stake_1*5*4 
            if rans == 1:                      # 小三元赔率
                score = score+stake_2*10+stake_3*10+stake_4*10 
            if rans == 2:                      # 大三元赔率
                score = score+stake_5*20+stake_6*20+stake_7*20 
            if rans == 3:                      # 小熊猫赔率
                score += stake_8*50
            if rans == 4:                      # 大熊猫赔率
                score += stake_8*100
            if rans == 5:                      # 大满贯赔率
                score_1 = stake_1*5*4+stake_1*2+stake_2*10*2+stake_2*2+stake_3*10*2
                score_2 = stake_3*2+stake_4*10*2+stake_4*2+stake_5*20+stake_5*2
                score_3 = stake_6*20+stake_6*2+stake_7*20+stake_7*2+stake_8*50+stake_8*100
                score += score_1+score_2+score_3
                gx_audio.play(-1)   
        if score>0:
            zj_audio.play()
        else:
            if rand ==6 :
                if rans ==4 or rans ==3:
                    zj_audio.play()
            else:
                jump_audio.play()    

        return score                                  # 赔率

    def scores_1 (self,rand):                              # 开火车得分函数 
        score = 0
        score_nb =0
        score_s =(stake_1*5+stake_5*2+stake_5*20,stake_2*2+stake_1*5+stake_5*2,
                  stake_2*10+stake_2*2+stake_1*5,stake_4*10+stake_2*10+stake_2*2+stake_1*5,
                  stake_6*2+stake_4*10+stake_2*10+stake_2*2,stake_6*20+stake_6*2+stake_4*10+stake_2*10,
                  stake_1*5+stake_6*20+stake_6*2+stake_4*10,stake_3*2+stake_1*5+stake_6*20+stake_6*2,
                  stake_3*10+stake_3*2+stake_1*5+stake_6*20,stake_7*20+stake_3*10+stake_3*2+stake_1*5,
                  stake_7*2+stake_7*20+stake_3*10+stake_3*2,stake_7*2+stake_7*20+stake_3*10,
                  stake_1*5+stake_7*2+stake_7*20,stake_4*2+stake_1*5+stake_7*2,
                  stake_2*10+stake_4*2+stake_1*5,stake_4*10+stake_2*10+stake_4*2+stake_1*5,
                  stake_8*50+stake_4*10+stake_2*10+stake_4*2,stake_8*100+stake_8*50+stake_4*10+stake_2*10,
                  stake_1*5+stake_8*100+stake_8*50+stake_4*10,stake_1*2+stake_1*5+stake_8*100+stake_8*50,
                  stake_3*10+stake_1*2+stake_1*5+stake_8*100,stake_5*20+stake_3*10+stake_1*2+stake_1*5,
                  stake_5*2+stake_5*20+stake_3*10+stake_1*2,stake_5*2+stake_5*20+stake_3*10
                  )
        for i in score_s :
            score_nb +=1
            if rand == score_nb:
                score += i          

        if score>0:
            zj_audio.play()
        else:
            jump_audio.play() 

        return score           

    def scores_2 (self,rand):                              # 天女散花得分函数 
        score = 0
        score_nb =0
        score_s =(stake_1*5,stake_2*2,stake_2*10,stake_4*10,stake_6*2,
                  stake_6*20,stake_1*5,stake_3*2,stake_3*10,stake_7*20,
                  stake_7*2,0,stake_1*5,stake_4*2,stake_2*10,stake_4*10,
                  stake_8*50,stake_8*100,stake_1*5,stake_2*2,stake_2*10,
                  stake_5*20,stake_5*2,0)
        for i in score_s :
            score_nb +=1
            if rand == score_nb:
                score += i
       
        return score                                  # 赔率

    def implement(self):                                   # 重复押注函数
        lis_c=[]
        lis_c.append(stake_1)
        lis_c.append(stake_2)
        lis_c.append(stake_3)
        lis_c.append(stake_4)
        lis_c.append(stake_5)
        lis_c.append(stake_6)
        lis_c.append(stake_7)
        lis_c.append(stake_8)
        return lis_c

    def diffic_2(self):
        di_r = [i for i in range(1,101)]                   # 获取1,100的列表
        random.shuffle(di_r)                               # 打乱位置
        di_rand = random.choice(di_r)                      # 随机提取1个数
        if di_rand in [i for i in range(1,21)]:
            randt = random.choice([2,5,8,11,14,17,20])
        elif di_rand in [i for i in range(21,41)]: 
            randt = random.choice([1,7,13,19])  
        elif di_rand in [i for i in range(41,61)]: 
            randt = random.choice([3,9,10,15,21,22])  
        elif di_rand in [i for i in range(61,81)]: 
            randt = random.choice([4,12,16]) 
        elif di_rand in [i for i in range(81,96)]: 
            randt = random.choice([23,24])                     
        elif di_rand in [i for i in range(96,101)]: 
            randt = random.choice([6,18])                
        return randt
        
    def diffic_3(self):
        di_r = [i for i in range(1,101)]                   # 获取1,100的列表
        random.shuffle(di_r)                               # 打乱位置
        di_rand = random.choice(di_r)                      # 随机提取1个数
        if di_rand in [i for i in range(21,41)]: 
            randt = random.choice([1,7,13,19])  
        elif di_rand in [i for i in range(51,66)]: 
            randt = random.choice([3,9,10,15,21,22])  
        elif di_rand in [i for i in range(76,85)]: 
            randt = random.choice([4,12,16]) 
        elif di_rand in [i for i in range(90,95)]: 
            randt = random.choice([23,24])                     
        elif di_rand in [i for i in range(95,96)]: 
            randt = random.choice([6,18])
        else:  
            randt = random.choice([2,5,8,11,14,17,20])                  
        return randt   

                

class FRUITSGAME():

    def paoh_rand(self,rand_s,syao):
        st_up =start_up()
        rand = rand_s
        if rand == 17 or rand == 18:         # 显示BAR
            st_up.sanyao(syao,10,720)  
        if rand == 10 or rand == 11:         # 显示双星
            st_up.sanyao(syao,84,720)   
        if rand == 5 or rand == 6:         # 显示双柒
            st_up.sanyao(syao,158,720
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值