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


    前言

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

     

    相关准备 💞

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

    游戏规则

    1.游戏地图为自己使用各种网络素材制作;各种按钮和选项,小图标等也是使用PS制作。
    2.声音效果主要为背景音乐和几种游戏中音效;
    3.游戏设定了两个类:玩家和建筑
      玩家的参数和方法都在代码中给出;
      具体有:移动方法、位置判断方法、购买房屋方法、添加小房子方法、事件判断方法。
    4.玩家在大富翁的主要操作是投掷骰子,由随机函数进行判定然后进行移动,进行位置判断,然后开始进行相关的判定。
    5.游戏中的按键有:是、否、和结束回合;每个按键由没按下与按下两种状态的图片组成,
      这个设计花费了一定时间。还有 开始游戏 和 扔骰子 的两个明暗按钮,由pygame优化后的一个函数实现。
    6.玩家的位置与电脑重叠时会将双方的位置进行一定偏移,防止进行覆盖,分不清自己的位置。
    7.游戏基础功能有移动,购买房子,在已经购买的房子下搭建新的小房子增加过路费,被收费,判断胜负的基础功能,此外还加入了幸运事件:
        财神 - 免收费一次
        衰神 - 双倍被收费一次
        破坏神 - 直接破坏一个建筑 无论敌我
        土地神 - 强占对面建筑
      这四项功能在位置处于左上角和右下角的时候会被触发,
      添加了很多游戏乐趣哦~~~ ^_^

    展示部分素材

     

     

     

    游戏效果展示 

     

     

     

    主要代码 

    1. # 初始化各种模块
    2. import pygame
    3. import random
    4. import sys
    5. # 定义类
    6. class Player():
    7. def __init__(self, image, name, isPlayer):
    8. self.name = name
    9. self.money = 10000
    10. self.isGoingToMove = False
    11. self.movable = True
    12. self.image = image
    13. self.position = 0
    14. self.temp_position = False
    15. self.dice_value = 0
    16. self.locatedBuilding = 0
    17. self.showText = []
    18. self.isPlayer = isPlayer
    19. self.ownedBuildings = []
    20. self.isShowText = False
    21. self.soundPlayList = 0
    22. self.caishen = 0
    23. self.shuaishen = 0
    24. self.tudishen = 0
    25. self.pohuaishen = 0
    26. def judgePosition(self, buildings): # 位置判断 返回值是所在位置的建筑
    27. for each in buildings:
    28. for every in each.location:
    29. if self.position == every:
    30. return each
    31. # 当使用元组时 当元组中只有一个元素时 发现该元素不可迭代
    32. # 出现错误 换成列表后解决
    33. '''
    34. try:
    35. for every in each.location:
    36. if self.position == every:
    37. print(each.name)
    38. except:
    39. if self.position == every:
    40. print(each.name)
    41. '''
    42. def buyaBuilding(self, isPressYes): # 购买方法
    43. if isPressYes and self.locatedBuilding.owner != self.name:
    44. self.locatedBuilding.owner = self.name
    45. self.locatedBuilding.wasBought = True
    46. self.ownedBuildings.append(self.locatedBuilding)
    47. self.money -= self.locatedBuilding.price
    48. self.showText = [self.name + '购买了' + self.locatedBuilding.name + '!']
    49. self.soundPlayList = 1
    50. return True
    51. else:
    52. return False
    53. def addaHouse(self, isPressYes): # 在建筑物上添加一个房子
    54. try:
    55. if isPressYes and self.locatedBuilding.owner == self.name:
    56. self.locatedBuilding.builtRoom += 1
    57. self.money -= self.locatedBuilding.payment
    58. self.showText = [self.name + '在' + self.locatedBuilding.name + '上!', '盖了一座房子!', \
    59. '有%d' % self.locatedBuilding.builtRoom + '个房子了!', \
    60. "它的过路费是%d" % (self.locatedBuilding.payment * \
    61. (self.locatedBuilding.builtRoom + 1))]
    62. self.soundPlayList = 2
    63. return True
    64. else:
    65. return False
    66. except:
    67. pass
    68. def move(self, buildings, allplayers): # 移动方法 返回值是所在的建筑位置
    69. self.dice_value = random.randint(1, 6)
    70. self.position += self.dice_value
    71. if self.position >= 16:
    72. self.position -= 16
    73. self.locatedBuilding = self.judgePosition(buildings)
    74. self.isShowText = True
    75. return self.eventInPosition(allplayers)
    76. def eventInPosition(self, allplayers): # 判断在建筑位置应该发生的事件
    77. building = self.locatedBuilding
    78. if building.name != '空地':
    79. if self.locatedBuilding.wasBought == False: # 未购买的时候显示建筑的数据!
    80. if self.isPlayer == True:
    81. textLine0 = self.name + '扔出了' + '%d' % self.dice_value + '点!'
    82. textLine1 = self.name + '来到了' + building.name + '!'
    83. textLine2 = '购买价格:%d' % building.price
    84. textLine3 = '过路收费:%d' % building.payment
    85. textLine4 = '是否购买?'
    86. self.showText = [textLine0, textLine1, textLine2, textLine3, textLine4]
    87. return True
    88. else:
    89. self.addaHouse(not self.buyaBuilding(True))
    90. # ----- 动画 -------
    91. # ----- 是否购买 ------
    92. elif building.owner == self.name: # 路过自己的房子开始加盖建筑!
    93. if self.pohuaishen == 1:
    94. textLine0 = self.name + '破坏神附体!'
    95. textLine1 = '摧毁了自己的房子!'
    96. building.owner = 'no'
    97. building.wasBought = False
    98. self.showText = [textLine0, textLine1]
    99. self.pohuaishen = 0
    100. else:
    101. if self.isPlayer == True:
    102. textLine0 = self.name + '扔出了' + '%d' % self.dice_value + '点!'
    103. textLine1 = '来到了ta的' + self.locatedBuilding.name + '!'
    104. textLine2 = '可以加盖小房子!'
    105. textLine3 = '加盖收费:%d' % building.payment
    106. textLine4 = '是否加盖?'
    107. self.showText = [textLine0, textLine1, textLine2, textLine3, textLine4]
    108. return True
    109. # ----- 动画-------
    110. else:
    111. self.addaHouse(True)
    112. else:
    113. for each in allplayers: # 被收费!
    114. if self.locatedBuilding.owner == each.name and each.name != self.name:
    115. if self.caishen == 1:
    116. textLine0 = self.name + '财神附体!'
    117. textLine1 = '免除过路费%d!' % (building.payment * (building.builtRoom + 1))
    118. self.showText = [textLine0, textLine1]
    119. self.caishen = 0
    120. else:
    121. if self.tudishen == 1:
    122. textLine0 = self.name + '土地神附体!'
    123. textLine1 = '强占土地!'
    124. textLine2 = building.name + '现在属于' + self.name
    125. self.locatedBuilding.owner = self.name
    126. self.showText = [textLine0, textLine1, textLine2]
    127. self.tudishen = 0
    128. else:
    129. if self.pohuaishen == 1:
    130. textLine0 = self.name + '破坏神附体!'
    131. textLine1 = '摧毁了对手的房子!'
    132. building.owner = 'no'
    133. building.wasBought = False
    134. self.showText = [textLine0, textLine1]
    135. self.pohuaishen = 0
    136. else:
    137. textLine0 = self.name + '扔出了' + '%d' % self.dice_value + '点!'
    138. textLine1 = self.name + '来到了' + each.name + '的:'
    139. textLine2 = building.name + ',被收费!'
    140. if self.shuaishen == 1:
    141. textLine3 = '过路收费:%d*2!' % (building.payment * (building.builtRoom + 1) * 2)
    142. self.shuaishen = 0
    143. else:
    144. textLine3 = '过路收费:%d' % (building.payment * (building.builtRoom + 1))
    145. textLine4 = '哦!' + self.name + '好倒霉!'
    146. self.showText = [textLine0, textLine1, textLine2, textLine3, textLine4]
    147. # 收费!
    148. self.money -= building.payment * (building.builtRoom + 1)
    149. each.money += building.payment * (building.builtRoom + 1)
    150. self.soundPlayList = 3
    151. # ----- 动画-------
    152. else:
    153. # 发现不能处理在空地上的情况 于是使用 try & except 来解决!然后加入了幸运事件功能!
    154. # 后来发现 try except 弊端太大 找不到错误的根源 换为if else嵌套。。
    155. whichone = self.dice_value % 4
    156. if whichone == 0:
    157. self.caishen = 1
    158. textLine2 = '遇到了财神!'
    159. textLine3 = '免一次过路费!'
    160. if whichone == 1:
    161. self.shuaishen = 1
    162. textLine2 = '遇到了衰神!'
    163. textLine3 = '过路费加倍一次!'
    164. if whichone == 2:
    165. self.tudishen = 1
    166. textLine2 = '遇到了土地神!'
    167. textLine3 = '强占一次房子!'
    168. if whichone == 3:
    169. self.pohuaishen = 1
    170. textLine3 = '摧毁路过的房子!'
    171. textLine2 = '遇到了破坏神!'
    172. textLine0 = self.name + '扔出了' + '%d' % self.dice_value + '点!'
    173. textLine1 = '来到了运气地点!'
    174. self.showText = [textLine0, textLine1, textLine2, textLine3]
    175. class Building(): # 好像所有功能都在Player类里实现了=_=
    176. def __init__(self, name, price, payment, location):
    177. self.name = name
    178. self.price = price
    179. self.payment = payment
    180. self.location = location
    181. self.wasBought = False # 是否被购买
    182. self.builtRoom = 0 # 小房子建造的数目
    183. self.owner = 'no'
    184. # 带透明度的绘图方法 by turtle 2333
    185. def blit_alpha(target, source, location, opacity):
    186. x = location[0]
    187. y = location[1]
    188. temp = pygame.Surface((source.get_width(), source.get_height())).convert()
    189. temp.blit(target, (-x, -y))
    190. temp.blit(source, (0, 0))
    191. temp.set_alpha(opacity)
    192. target.blit(temp, location)

     文章首发于公众号【编程乐趣】,欢迎大家关注。

  • 相关阅读:
    springboot+音乐播放小程序 毕业设计-附源码191730
    web网页设计期末课程大作业:家乡旅游主题网站设计——河北8页HTML+CSS+JavaScript
    SaaSBase:什么是CDP集团?
    【Python面试题收录】Python的深浅拷贝
    论文阅读:《Ad Click Prediction: a View from the Trenches》
    JMeter—(二)如何做接口测试入门篇
    vuepress 打包后左侧菜单链接 404 问题解决办法
    SQL:数据去重的三种方法
    Golang 变量作用域陷阱 误用短声明导致变量覆盖
    Java 内部类 面试“变态题”
  • 原文地址:https://blog.csdn.net/daremeself/article/details/125488924