Python苹果机游戏pygame版


前言

前段时间写过print()版的,发现很难玩,操作起来相当繁琐,只能供新手了解基础代码的运用。对于游戏本身没什么可玩性。

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

现在写了个pygame版,相对前面那个版本操作性大大提升。
同样复制代码就能玩。希望大家有什么问题多多交流。

游戏代码

代码如下(示例):



# 复制代码就能运行,如果无法运行就可能是下面两种情况
# 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,pygame
from pygame.locals import *


def print_text(font,x,y,text,color=(255,255,255)):     
    imgText = font.render(text,True,color)      # 文字
    screen.blit(imgText,(x,y))                  # 显示


def scores (bomb_y,speed,end):                # 得分函数 
    score = 0
    if  bomb_y == 100:
        score += stake_1*5
    if  bomb_y == 200:
        score += stake_1*2
    if  bomb_y == 300:    
        score += stake_3*10
    if  bomb_y == 400:    
        score += stake_5*20
    if  bomb_y == 500:
        score += stake_5*2
    if  bomb_y == 600:
        if end == 5:
            score += speed
            speed = 0
        else:
            score += 0    
    if  bomb_y == 700:
        score += stake_1*5
    if  bomb_y == 800:
        score += stake_2*2
    if  bomb_y == 900:
        score += stake_2*10
    if  bomb_y == 1000:
        score += stake_4*10
    if  bomb_y == 1100:
        score += stake_6*2
    if  bomb_y == 1200:
        score += stake_6*20
    if  bomb_y == 1300:
        score += stake_1*5
    if  bomb_y == 1400:
        score += stake_3*2
    if  bomb_y == 1500:
        score += stake_3*10
    if  bomb_y == 1600:
        score += stake_7*20
    if  bomb_y == 1700:
        score += stake_7*2


    if  bomb_y == 1900:
        score += stake_1*5
    if  bomb_y == 2000:
        score += stake_4*2
    if  bomb_y == 2100:
        score += stake_2*10
    if  bomb_y == 2200:
        score += stake_4*10
    if  bomb_y == 2300:
        score += stake_8*50
    if  bomb_y == 2400:
        score += stake_8*100

    if bomb_y == 1800 and rans == 0:                      # 大四喜赔率
        score = score+stake_1*5*4 
    if bomb_y == 1800 and rans == 1:                      # 小三元赔率
        score = score+stake_2*10+stake_3*10+stake_4*10 
    if bomb_y == 1800 and rans == 2:                      # 大三元赔率
        score = score+stake_5*20+stake_6*20+stake_7*20 
    if bomb_y == 1800 and rans == 3:                      # 小熊猫赔率
        score += stake_8*50
    if bomb_y == 1800 and rans == 4:                      # 大熊猫赔率
        score += stake_8*100
    if bomb_y == 1800 and rans == 5:                      # 大满贯赔率
        score += stake_1*5*4+stake_1*2+stake_2*10*2+stake_2*2+stake_3*10*2+stake_3*2+stake_4*10*2+stake_4*2+stake_5*20+stake_5*2+stake_6*20+stake_6*2+stake_7*20+stake_7*2+stake_8*50+stake_8*100         
    
    
    return score              # 赔率

def implement(stake_1,stake_2,stake_3,stake_4,stake_5,stake_6,stake_7,stake_8):                     # 重复押注函数
    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
            


pygame.init()                                   #  游戏
screen = pygame.display.set_mode((600,700))     #  窗口
pygame.display.set_caption("苹果游戏机")     #  标题
font1 = pygame.font.SysFont("方正粗黑宋简体",24)               #  字体
font2 = pygame.font.SysFont("方正粗黑宋简体",20)               #  字体

# pygame.mouse.set_visible(False)       # 隐藏鼠标箭头
white = 255,255,255
yellow = 230,230,50
red = 220,50,50
black = 0,0,0
green = 0,255,0
kss = False
tips = False                              # 提示
tips_1= False
lives = 1000                              # 生命
score = 0                                 # 得分
score_c = score
game_ove = True                          # 游戏是否是结束状态
switch = True                            # 开关是否开启状态
mouse_x = mouse_y = 0
bomb_x = 0
bomb_y = 0
vel_y = 1
variable = 0                         # 圈数
speed = 0                              # 彩金
and_s = 0                         # 随机数字--比大小用的
rans = 0
end = 0

stake_1 = 0                       # 押注 苹果
stake_2 = 0                       # 押注 橘子
stake_3 = 0                       # 押注 芒果
stake_4 = 0                       # 押注 铃铛
stake_5 = 0                       # 押注 西瓜
stake_6 = 0                       # 押注 双柒
stake_7 = 0                       # 押注 双星
stake_8 = 0                       # 押注 熊猫
lis = [0,0,0,0,0,0,0,0]

rand = 0



while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == MOUSEMOTION:     #  定义鼠标事件,x,y
            mouse_x,mouse_y = event.pos     #  鼠标当前位置
            move_x,move_y = event.rel       #  鼠标移动格数
        
        elif event.type == MOUSEBUTTONUP and 520<mouse_x<570 and 530<mouse_y<560:   #  单点开始
            if game_ove:
                if kss:
                    if stake_1==0 and stake_2==0 and stake_3==0 and stake_4==0 and stake_5==0 and stake_6==0 and stake_7==0 and stake_8==0:
                        if lives > lis[0]+lis[1]+lis[2]+lis[3]+lis[4]+lis[5]+lis[6]+lis[7]:
                            stake_1 = lis[0]
                            stake_2 = lis[1]
                            stake_3 = lis[2]
                            stake_4 = lis[3]
                            stake_5 = lis[4]
                            stake_6 = lis[5]
                            stake_7 = lis[6]
                            stake_8 = lis[7] 
                            lives -= lis[0]+lis[1]+lis[2]+lis[3]+lis[4]+lis[5]+lis[6]+lis[7]
                            tips_1 = False
                        else:
                            if score==0:
                                tips_1 = True
                            game_ove = True 
                    if stake_1>0 or stake_2>0 or stake_3>0 or stake_4>0 or stake_5>0 or stake_6>0 or stake_7>0 or stake_8>0:
                        if score > 0: 
                            tips_1 = False                 
                            stake_1 = 0
                            stake_2 = 0
                            stake_3 = 0
                            stake_4 = 0
                            stake_5 = 0
                            stake_6 = 0
                            stake_7 = 0
                            stake_8 = 0
                            lives += score
                            score = 0
                            if end == 5 and bomb_y == 600:
                                speed = 0
                        else:
                            tips_1 = False
                            game_ove = False
                            rand = random.randint(1,24)*100
                            rans = random.randint(0,5)
                            end = random.randint(0,5)
                            bomb_x += rand
                            if bomb_x > 2400:
                                bomb_x -= 2400

                            if tips == False:
                                speed += 100
                else:
                    tips = True

        elif event.type == MOUSEBUTTONUP and 30<mouse_x<80 and 530<mouse_y<560:    #  ALL的操作
            if lives > score:
                if score > 0:
                    if switch:
                        if score < lives:
                            lives -= score
                            score = score*2
                            switch = False
                        else:
                            score = score+lives 
                            lives = 0
                            switch = False
            else:
                tips_1 = True                
        elif event.type == MOUSEBUTTONUP and 150<mouse_x<200 and 530<mouse_y<560:   # 左移
            if score > 0 and lives > 0 :
                if score < score_c*2:
                    score += 1
                    lives -= 1    
                switch = False
                tips 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值