Python版大富翁源代码

这是一个使用Python编程语言实现的大富翁游戏源代码项目,包含了完整的图片资源。项目托管在GitHub上,地址为https://github.com/KhalilWang/TheRich,如果你对此感兴趣,可以去查看并为项目点赞。
摘要由CSDN通过智能技术生成
</pre><pre name="code" class="python"># -*- coding: utf-8 -*-


#  code by:  Khalil
#  name:     理工大富翁beta2.0
#  describe: 基于python的一个2D大富翁游戏
'''
1.游戏地图为自己使用各种网络素材制作;
  各种按钮和选项,小图标等也是使用PS制作。

2.声音效果主要为背景音乐和几种游戏中音效;

3.游戏设定了两个类:玩家和建筑
  玩家的参数和方法都在代码中给出;
  具体有:移动方法、位置判断方法、
  	 购买房屋方法、添加小房子方法、
	 事件判断方法。

4.玩家在大富翁的主要操作是投掷骰子,由随机函数
  进行判定然后进行移动,进行位置判断,然后开始
  进行相关的判定。

5.游戏中的按键有:是、否、和结束回合;
  每个按键由没按下与按下两种状态的图片组成,
  这个设计花费了一定时间。
  还有 开始游戏 和 扔骰子 的两个明暗按钮,
  由pygame优化后的一个函数实现。

6.玩家的位置与电脑重叠时会将双方的位置进行一定
  偏移,防止进行覆盖,分不清自己的位置。

7.游戏基础功能有移动,购买房子,在已经购买的房子下
  搭建新的小房子增加过路费,被收费,判断胜负的基础
  功能,此外还加入了幸运事件:
    财神 - 免收费一次
    衰神 - 双倍被收费一次
    破坏神 - 直接破坏一个建筑 无论敌我
    土地神 - 强占对面建筑
  这四项功能在位置处于左上角和右下角的时候会被触发,
  添加了很多游戏乐趣哦~~~ ^_^

8.游戏基于python的一个模块pygame实现,给我提供了很
  多快乐的时光,谢谢老师的阅览与郭宁同学的协助答辩
  :)
'''

########################################准备工作###############################################

# 初始化各种模块
import pygame
import random
import sys

# 定义类
class Player():
    def __init__(self, image ,name , isPlayer):
        self.name = name
        self.money = 10000
        self.isGoingToMove = False 
        self.movable = True
        self.image = image
        self.position = 0  
        self.temp_position = False
        self.dice_value = 0
        self.locatedBuilding = 0
        self.showText = []
        self.isPlayer = isPlayer
        self.ownedBuildings = []
        self.isShowText = False
        self.soundPlayList = 0
        self.caishen = 0
        self.shuaishen = 0
        self.tudishen = 0
        self.pohuaishen = 0
        
    
    def judgePosition(self,buildings): # 位置判断 返回值是所在位置的建筑
        for each in buildings:
            for every in each.location:
                if self.position == every:
                    return each
                    
            
            # 当使用元组时 当元组中只有一个元素时 发现该元素不可迭代 
            # 出现错误 换成列表后解决
            ''' 
            try:
                for every in each.location:
                    if self.position == every:
                        print(each.name)
            except:
                if self.position == every:
                    print(each.name)
            '''
            
    def buyaBuilding(self,isPressYes):    # 购买方法
        if isPressYes and self.locatedBuilding.owner != self.name:
            self.locatedBuilding.owner = self.name
            self.locatedBuilding.wasBought = True
            self.ownedBuildings.append(self.locatedBuilding)
            self.money -= self.locatedBuilding.price
            self.showText = [self.name + '购买了' + self.locatedBuilding.name + '!']
            self.soundPlayList = 1
            return True
        else:
            return False
        
          
            
    def addaHouse(self,isPressYes): # 在建筑物上添加一个房子
        try:
            if isPressYes and self.locatedBuilding.owner == self.name:
                self.locatedBuilding.builtRoom += 1
                self.money -= self.locatedBuilding.payment
                self.showText = [self.name + '在' + self.locatedBuilding.name + '上!','盖了一座房子!',\
                                '有%d' % self.locatedBuilding.builtRoom + '个房子了!',\
                                "它的过路费是%d" % (self.locatedBuilding.payment * \
                                                (self.locatedBuilding.builtRoom + 1)) ]
                self.soundPlayList = 2
                return True
            else:
                return False
        except:
            pass
    
    def move(self,buildings,allplayers):   # 移动方法 返回值是所在的建筑位置
        self.dice_value =  random.randint(1,6)
        self.position += self.dice_value
        if self.position >= 16:
            self.position -= 16
        self.locatedBuilding = self.judgePosition(buildings)
        self.isShowText = True
        return self.eventInPosition(allplayers)
    
    
    def eventInPosition(self,allplayers):        # 判断在建筑位置应该发生的事件        
        building = self.locatedBuilding
        if building.name != '空地':
            if self.locatedBuilding.wasBought == False: # 未购买的时候显示建筑的数据!
                if self.isPlayer == True:
                    textLine0 = self.name +'扔出了' + '%d'% self.dice_value + '点!'
                    textLine1 = self.name +'来到了' + building.name + '!'
                    textLine2 = '购买价格:%d' % building.price
                    textLine3 = '过路收费:%d' % building.payment
                    textLine4 = '是否购买?'
                    self.showText = [textLine0,textLine1,textLine2,textLine3,textLine4]
                    return True
                else :
                    self.addaHouse(not self.buyaBuilding(True))
                    
                # ----- 动画 -------
                # ----- 是否购买 ------
            elif building.owner == self.name: # 路过自己的房子开始加盖建筑!
                if self.pohuaishen == 1:
                    textLine0 = self.name + '破坏神附体!'
                    textLine1 = '摧毁了自己的房子!'
                    building.owner = 'no'
                    building.wasBought = False
                    self.showText = [textLine0,textLine1]
                    self.pohuaishen = 0
                else:
                    if self.isPlayer == True:
                        textLine0 = self.name + '扔出了' + '%d'% self.dice_value + '点!'
                        textLine1 = '来到了ta的'+ self.locatedBuilding.name +'!'
                        textLine2 = '可以加盖小房子!' 
                        textLine3 = '加盖
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值