【python游戏制作】大富翁游戏源码

简介: 【python游戏制作】大富翁游戏源码

前言

大富翁,又名地产大亨。是一种多人策略图版游戏。参与者分得游戏金钱,凭运气(掷骰子)及交易策略,买地、建楼以赚取租金。英文原名monopoly意为“垄断”,因为最后只得一个胜利者,其余均破产收场。游戏的设计当初旨在暴露放任资本主义的弊端,但是推出之后却受到大众欢迎。

c48ff8a381e8455083f2347e2c44bdef.png

相关准备 💞

在开始之前,我们要准备好游戏的相关素材~没有(不想找)的小伙伴可以找我领取呐`

游戏规则


1.游戏地图为自己使用各种网络素材制作;各种按钮和选项,小图标等也是使用PS制作。

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

3.游戏设定了两个类:玩家和建筑

 玩家的参数和方法都在代码中给出;

 具体有:移动方法、位置判断方法、购买房屋方法、添加小房子方法、事件判断方法。

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

5.游戏中的按键有:是、否、和结束回合;每个按键由没按下与按下两种状态的图片组成,

 这个设计花费了一定时间。还有 开始游戏 和 扔骰子 的两个明暗按钮,由pygame优化后的一个函数实现。

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

7.游戏基础功能有移动,购买房子,在已经购买的房子下搭建新的小房子增加过路费,被收费,判断胜负的基础功能,此外还加入了幸运事件:

   财神 - 免收费一次

   衰神 - 双倍被收费一次

   破坏神 - 直接破坏一个建筑 无论敌我

   土地神 - 强占对面建筑

 这四项功能在位置处于左上角和右下角的时候会被触发,

 添加了很多游戏乐趣哦~~~ ^_^展示部分素材


游戏效果展示


主要代码

# 初始化各种模块
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 = '加盖收费:%d' % building.payment
                        textLine4 = '是否加盖?'
                        self.showText = [textLine0, textLine1, textLine2, textLine3, textLine4]
                        return True
                    # ----- 动画-------
                    else:
                        self.addaHouse(True)
            else:
                for each in allplayers:  # 被收费!
                    if self.locatedBuilding.owner == each.name and each.name != self.name:
                        if self.caishen == 1:
                            textLine0 = self.name + '财神附体!'
                            textLine1 = '免除过路费%d!' % (building.payment * (building.builtRoom + 1))
                            self.showText = [textLine0, textLine1]
                            self.caishen = 0
                        else:
                            if self.tudishen == 1:
                                textLine0 = self.name + '土地神附体!'
                                textLine1 = '强占土地!'
                                textLine2 = building.name + '现在属于' + self.name
                                self.locatedBuilding.owner = self.name
                                self.showText = [textLine0, textLine1, textLine2]
                                self.tudishen = 0
                            else:
                                if self.pohuaishen == 1:
                                    textLine0 = self.name + '破坏神附体!'
                                    textLine1 = '摧毁了对手的房子!'
                                    building.owner = 'no'
                                    building.wasBought = False
                                    self.showText = [textLine0, textLine1]
                                    self.pohuaishen = 0
                                else:
                                    textLine0 = self.name + '扔出了' + '%d' % self.dice_value + '点!'
                                    textLine1 = self.name + '来到了' + each.name + '的:'
                                    textLine2 = building.name + ',被收费!'
                                    if self.shuaishen == 1:
                                        textLine3 = '过路收费:%d*2!' % (building.payment * (building.builtRoom + 1) * 2)
                                        self.shuaishen = 0
                                    else:
                                        textLine3 = '过路收费:%d' % (building.payment * (building.builtRoom + 1))
                                    textLine4 = '哦!' + self.name + '好倒霉!'
                                    self.showText = [textLine0, textLine1, textLine2, textLine3, textLine4]
                                    # 收费!
                                    self.money -= building.payment * (building.builtRoom + 1)
                                    each.money += building.payment * (building.builtRoom + 1)
                                    self.soundPlayList = 3
                                    # ----- 动画-------
        else:
            # 发现不能处理在空地上的情况 于是使用 try & except 来解决!然后加入了幸运事件功能!
            # 后来发现 try except 弊端太大 找不到错误的根源 换为if else嵌套。。
            whichone = self.dice_value % 4
            if whichone == 0:
                self.caishen = 1
                textLine2 = '遇到了财神!'
                textLine3 = '免一次过路费!'
            if whichone == 1:
                self.shuaishen = 1
                textLine2 = '遇到了衰神!'
                textLine3 = '过路费加倍一次!'
            if whichone == 2:
                self.tudishen = 1
                textLine2 = '遇到了土地神!'
                textLine3 = '强占一次房子!'
            if whichone == 3:
                self.pohuaishen = 1
                textLine3 = '摧毁路过的房子!'
                textLine2 = '遇到了破坏神!'
            textLine0 = self.name + '扔出了' + '%d' % self.dice_value + '点!'
            textLine1 = '来到了运气地点!'
            self.showText = [textLine0, textLine1, textLine2, textLine3]
class Building():  # 好像所有功能都在Player类里实现了=_=
    def __init__(self, name, price, payment, location):
        self.name = name
        self.price = price
        self.payment = payment
        self.location = location
        self.wasBought = False  # 是否被购买
        self.builtRoom = 0  # 小房子建造的数目
        self.owner = 'no'
# 带透明度的绘图方法 by turtle 2333
def blit_alpha(target, source, location, opacity):
    x = location[0]
    y = location[1]
    temp = pygame.Surface((source.get_width(), source.get_height())).convert()
    temp.blit(target, (-x, -y))
    temp.blit(source, (0, 0))
    temp.set_alpha(opacity)
    target.blit(temp, location)


相关文章
|
2天前
|
机器学习/深度学习 人工智能 自然语言处理
【机器学习】python之人工智能应用篇--游戏生成技术
游戏生成技术,特别是生成式人工智能(Generative Artificial Intelligence, 简称Generative AI),正逐步革新游戏开发的多个层面,从内容创作到体验设计。这些技术主要利用机器学习、深度学习以及程序化内容生成(Procedural Content Generation, PCG)来自动创造游戏内的各种元素,显著提高了开发效率、丰富了游戏内容并增强了玩家体验。以下是生成式AI在游戏开发中的几个关键应用场景概述
6 2
|
7天前
|
JSON 算法 API
京东以图搜图功能API接口调用算法源码python
京东图搜接口是一款强大工具,通过上传图片即可搜索京东平台上的商品。适合电商平台、比价应用及需商品识别服务的场景。使用前需了解接口功能并注册开发者账号获取Key和Secret;准备好图片的Base64编码和AppKey;生成安全签名后,利用HTTP客户端发送POST请求至接口URL;最后解析JSON响应数据以获取商品信息。
|
7天前
|
开发者 Python
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
29 1
|
7天前
|
开发者 Python
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
21 1
|
8天前
|
数据可视化 数据挖掘 索引
【python】Python马铃薯批发市场交易价格数据分析可视化(源码+数据集)【独一无二】
【python】Python马铃薯批发市场交易价格数据分析可视化(源码+数据集)【独一无二】
|
8天前
|
机器学习/深度学习 数据采集 数据可视化
【python】python心理健康医学数据分析与逻辑回归预测(源码+数据集+论文)【独一无二】
【python】python心理健康医学数据分析与逻辑回归预测(源码+数据集+论文)【独一无二】
|
8天前
|
存储 数据可视化 数据挖掘
【python】Python考研分数 线性回归模型预测(源码+论文)【独一无二】
【python】Python考研分数 线性回归模型预测(源码+论文)【独一无二】
|
8天前
|
存储 数据可视化 数据挖掘
【Python】Tkinter电器销售有限公司销售数据分析(源码)【独一无二】
【Python】Tkinter电器销售有限公司销售数据分析(源码)【独一无二】
|
8天前
|
存储 数据可视化 UED
【Python】Tkinter超市商品选购系统 [简易版] (源码)【独一无二】
【Python】Tkinter超市商品选购系统 [简易版] (源码)【独一无二】
|
8天前
|
存储 Python
【python】python生活管理费系统(源码+论文)【独一无二】
【python】python生活管理费系统(源码+论文)【独一无二】