前言
说到童年爱玩的电脑游戏,你会想到什么?
搞错了,以前没得王者、吃鸡的——那时候最国民的莫过于金山打字通,接着是扫雷、红心大战,
而红极一时的单机游戏当属《大富翁》。
扫雷是刚给大家讲过的AI自动扫雷,今天的话就给大家将讲讲《大富翁》游戏吧~
(这可是我搞了好久好久了的,看完给个吧~万分感谢!恭喜发财恭喜发财~)
一、《大富翁》思路
本文是基于python的一个2D大富翁游戏 1.游戏地图为自己使用各种网络素材制作: 各种按钮和选项,小图标等也是使用PS制作。 2.声音效果主要为背景音乐和几种游戏中的音效。 3.游戏设定了两个类:玩家和建筑,玩家的参数和方法都在代码中给出,具体有:移动方法、位置判断方法、 购买房屋方法、添加小房子方法、事件判断方法。 4.玩家在大富翁的主要操作是投掷骰子,由随机函数进行判定然后进行移动,进行位置判断,然后开始进行相 关的判定。 5.游戏中的按键有:是、否、和结束回合,每个按键由没按下与按下两种状态的图片组成, 这个设计花费了 一定时间。还有 开始游戏 和 扔骰子 的两个明暗按钮,由pygame优化后的一个函数实现。 6.玩家的位置与电脑重叠时会将双方的位置进行一定偏移,防止被覆盖,分不清自己的位置。 7.游戏基础功能有移动,购买房子,在已经购买的房子下搭建新的小房子增加过路费,被收费,判断胜负的基 项功能,此外还加入了幸运事件: 财神 - 免收费一次 衰神 - 双倍被收费一次 破坏神 - 直接破坏一个建筑 无论敌我 土地神 - 强占对面建筑 这四项功能在位置处于左上角和右下角的时候会被触发,添加了很多游戏乐趣啦~希望大家能喜欢嘿嘿!
二、环境准备
环境安装
Python3、 Pycharm 、Pygame。
第三方库的安装:pip install pygame
素材图片(音乐就不截图了)
三、
效果展示:
游戏规则:
1, 扔的骰子点数为你可走几格。
2, 当走到任一格,其经营权没有被拥有时,可以按棋盘中标的价格买下并拥有它。如你的钱不
够,或不想买,则此时其它的参与者可竞价买下。如果这一经营权是你自己拥有,则不用付钱,比如
果是另人拥有,则按经营权上的价格付给经营权拥有者。
.......大家可以百度下游戏规则哈哈哈 不会玩儿的话......我就随便地给大家介绍一下下啦
开始界面一一
游戏界面——
游戏中掷色子——
还有很多界面我就不一一截图了哈!大家自己玩儿!需要代码的都是滴滴我就行了!
四、代码演示
# -*- coding: utf-8 -*-
# 初始化各种模块
import pygame
import random
import sys
# 定义类
class Player():
def __init__(self, image, name, isPlayer):
= name
= 10000
= False
= True
self.image = image
= 0
= False
= 0
= 0
= []
= isPlayer
= []
= False
= 0
= 0
= 0
= 0
= 0
def judgePosition(self, buildings): # 位置判断 返回值是所在位置的建筑
for each in buildings:
for every in each.location:
if == every:
return each
# 当使用元组时 当元组中只有一个元素时 发现该元素不可迭代
# 出现错误 换成列表后解决
'''
try:
for every in each.location:
if == every:
prin)
except:
if == every:
prin)
'''
def buyaBuilding(self, isPressYes): # 购买方法
if isPressYes and .owner != :
.owner =
.wasBought = True
.append()
-= .price
= [ + '购买了' + .name + '!']
= 1
return True
else:
return False
def addaHouse(self, isPressYes): # 在建筑物上添加一个房子
try:
if isPressYes and .owner == :
.builtRoom += 1
-= .payment
= [ + '在' + .name + '上!', '盖了一座房子!', \
'有%d' % .builtRoom + '个房子了!', \
"它的过路费是%d" % (.payment * \
(.builtRoom + 1))]
= 2
return True
else:
return False
except:
pass
def move(self, buildings, allplayers): # 移动方法 返回值是所在的建筑位置
= random.randint(1, 6)
+=
if >= 16:
-= 16
= (buildings)
= True
return (allplayers)
def eventInPosition(self, allplayers): # 判断在建筑位置应该发生的事件
building =
if building.name != '空地':
if .wasBought == False: # 未购买的时候显示建筑的数据!
if == True:
textLine0 = + '扔出了' + '%d' % + '点!'
textLine1 = + '来到了' + building.name + '!'
textLine2 = '购买价格:%d' % building.price
textLine3 = '过路收费:%d' % building.payment
textLine4 = '是否购买?'
= [textLine0, textLine1, textLine2, textLine3, textLine4]
return True
else:
(not (True))
# ----- 动画 -------
# ----- 是否购买 ------
elif building.owner == : # 路过自己的房子开始加盖建筑!
if == 1:
textLine0 = + '破坏神附体!'
textLine1 = '摧毁了自己的房子!'
building.owner = 'no'
building.wasBought = False
= [textLine0, textLine1]
= 0
else:
if == True:
textLine0 = + '扔出了' + '%d' % + '点!'
textLine1 = '来到了ta的' + .name + '!'
textLine2 = '可以加盖小房子!'
textLine3 = '加盖收费:%d' % building.payment
textLine4 = '是否加盖?'
= [textLine0, textLine1, textLine2, textLine3, textLine4]
return True
# ----- 动画-------
else:
(True)
else:
for each in allplayers: # 被收费!
if .owner == each.name and each.name != :
if == 1:
textLine0 = + '财神附体!'
textLine1 = '免除过路费%d!' % * + 1))
= [textLine0, textLine1]
= 0
else:
if == 1:
textLine0 = + '土地神附体!'
textLine1 = '强占土地!'
textLine2 = building.name + '现在属于' +
.owner =
= [textLine0, textLine1, textLine2]
= 0
else:
if == 1:
textLine0 = + '破坏神附体!'
textLine1 = '摧毁了对手的房子!'
building.owner = 'no'
building.wasBought = False
= [textLine0, textLine1]
= 0
else:
textLine0 = + '扔出了' + '%d' % + '点!'
textLine1 = + '来到了' + each.name + '的:'
textLine2 = building.name + ',被收费!'
if == 1:
textLine3 = '过路收费:%d*2!' % * + 1) * 2)
= 0
else:
textLine3 = '过路收费:%d' % * + 1))
textLine4 = '哦!' + + '好倒霉!'
= [textLine0, textLine1, textLine2, textLine3, textLine4]
# 收费!
-= building.payment * + 1)
each.money += building.payment * + 1)
= 3
# ----- 动画-------
else:
# 发现不能处理在空地上的情况 于是使用 try & except 来解决!然后加入了幸运事件功能!
# 后来发现 try except 弊端太大 找不到错误的根源 换为if else嵌套。。
whichone = % 4
if whichone == 0:
= 1
textLine2 = '遇到了财神!'
textLine3 = '免一次过路费!'
if whichone == 1:
= 1
textLine2 = '遇到了衰神!'
textLine3 = '过路费加倍一次!'
if whichone == 2:
= 1
textLine2 = '遇到了土地神!'
textLine3 = '强占一次房子!'
if whichone == 3:
= 1
textLine3 = '摧毁路过的房子!'
textLine2 = '遇到了破坏神!'
textLine0 = + '扔出了' + '%d' % + '点!'
textLine1 = '来到了运气地点!'
= [textLine0, textLine1, textLine2, textLine3]
class Building(): # 好像所有功能都在Player类里实现了=_=
def __init__(self, name, price, payment, location):
= name
= price
= payment
= location
= False # 是否被购买
= 0 # 小房子建造的数目
= 'no'
# 带透明度的绘图方法 by turtle 2333
def blit_alpha(target, source, location, opacity):
x = location[0]
y = location[1]
temp = (), ())).convert()
(target, (-x, -y))
(source, (0, 0))
(opacity)
(temp, location)
########################################主函数###############################################
def main():
()
clock = ()
# 初始化屏幕
size = (1270, 768)
screen = (size)
("大富翁 - by 顾木子吖 ")
# 读取字体以及有关数据
textColorInMessageBox = (141, 146, 152)
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
font = ('resource\\font\\my;, 30)
# 读取资源
backgroud = pygame.image.load("resource\\pic\\GameMap.png")
chess = pygame.image.load("resource\\pic\\c;)
chess_com = pygame.image.load("resource\\pic\\c;)
bigdice_image = pygame.image.load("resource\\pic\\dice.png").convert_alpha()
dice_1 = pygame.image.load("resource\\pic\\dice_1.png")
dice_2 = pygame.image.load("resource\\pic\\dice_2.png")
dice_3 = pygame.image.load("resource\\pic\\dice_3.png")
dice_4 = pygame.image.load("resource\\pic\\dice_4.png")
dice_5 = pygame.image.load("resource\\pic\\dice_5.png")
dice_6 = pygame.image.load("resource\\pic\\dice_6.png")
dices = [dice_1, dice_2, dice_3, dice_4, dice_5, dice_6]
yes = pygame.image.load("resource\\pic\\yes.png")
yes2 = pygame.image.load("resource\\pic\\ye;)
no = pygame.image.load("resource\\pic\\no.png")
no2 = pygame.image.load("resource\\pic\\no2.png")
GameStart = pygame.image.load("resource\\pic\\GameS;)
StartGameButton = pygame.image.load("resource\\pic\\S;).convert_alpha()
turnover = pygame.image.load("resource\\pic\\;)
turnover2 = pygame.image.load("resource\\pic\\;)
shuaishen = pygame.image.load("resource\\pic\\;).convert_alpha()
tudishen = pygame.image.load("resource\\pic\\;).convert_alpha()
caishen = pygame.image.load("resource\\pic\\cai;).convert_alpha()
pohuaishen = pygame.image.load("resource\\pic\\pohuai;).convert_alpha()
rollDiceSound = ("resource\\sound\\rolldice;)
bgm = ("resource\\sound\\bgm.ogg")
throwcoin = ("resource\\sound\\;)
moneysound = ("resource\\sound\\money;)
aiyo = ("resource\\sound\\aiyo.wav")
didong = ("resource\\sound\\didong.wav")
# PlayList 在对象中设置应该播放的声音
playList = [moneysound, throwcoin, aiyo]
# 各种Surface的rect
bigdice_rect = bigdice_image.get_rect()
bigdice_rect.left, bigdice_rect.top = 50, 600
yes_rect = yes.get_rect()
ye, ye = 500, 438
no_rect = no.get_rect()
no_rect.left, no_rect.top = 630, 438
button_rect = S()
bu, bu = 1003, 30
turnover_rect = ()
, = 1035, 613
# 实例化对象
players = []
computers = []
allplayers = []
player_1 = Player(chess, '玩家', True)
player_com1 = Player(chess_com, '电脑', False)
(player_1)
com(player_com1)
all(player_1)
all(player_com1)
presentPlayer = player_com1
# 初始化建筑物数据
gate = Building('大门', 1000, 200, [1, 2])
fountain = Building('喷泉', 2000, 400, [3, 4])
path = Building('小道', 800, 160, [5])
library = Building('图书馆', 2000, 400, [6, 7])
kongdi1 = Building('空地', 0, 0, [8])
classroomTen = Building('教十', 1200, 240, [9, 10])
classroomNine = Building('教九', 1200, 240, [11, 12])
resOne = Building('三餐厅', 800, 160, [13])
resTwo = Building('二餐厅', 800, 160, [14])
resThree = Building('一餐厅', 800, 160, [15])
kongdi2 = Building('空地', 0, 0, [0])
buildings = [gate, fountain, path, library, classroomNine, \
classroomTen, resOne, resThree, resTwo, kongdi1, kongdi2]
# 坐标数据 同时处理坐标数据 使之合适
MapXYvalue = [, 231.5), , 231.5), , 231.5), , 231.5), , 231.5), \
, 317.0), , 405.5), , 484.5), , 558.5), , 558.5), \
, 558.5), , 556.5), , 556.5), , 479.5), , 399.0), \
, 315.5)
]
MapChessPosition_Player = []
MapChessPosition_Com = []
MapChessPosition_Original = []
MapChessPosition_Payment = []
MapMessageBoxPosition = , 276.9)
YesNoMessageBoxPosition = [(500, 438), (630, 438)]
StartGameButtonPosition = (1003, 30)
TurnOvwrButtonPosition = (1035, 613)
# 调整位置
for i in range(0, 16):
Ma((MapXYvalue[i][0] - 50, MapXYvalue[i][1] - 80))
Ma((MapXYvalue[i][0] - 70, MapXYvalue[i][1] - 60))
Ma((MapXYvalue[i][0] - 30, MapXYvalue[i][1] - 100))
Ma((MapXYvalue[i][0] - 30, MapXYvalue[i][1] - 15))
# 循环时所用的一些变量
running = True
image_alpha = 255
button_alpha = 255
half_alpha = 30
showdice = True
showYes2 = False
showNo2 = False
showYes_No = False
pressYes = False
whetherYes_NoJudge = False
gameStarted = False
showButton2 = False
# 播放背景音乐
(100)
########################################进入游戏循环!###############################################
# 循环开始!
while running:
if not gameStarted:
for event in ():
if event.type == :
()
# 明暗触发 鼠标位置判断
if event.type == :
if bu):
button_alpha = 255
else:
button_alpha = 120
if event.type == :
if bu): # 按下按钮
didong.play()
gameStarted = True
(GameStart, (0, 0))
blit_alpha(screen, StartGameButton, StartGameButtonPosition, button_alpha)
if gameStarted:
for event in ():
if event.type == :
()
# 明暗触发 鼠标位置判断
if event.type == :
if bigdice_rect.collidepoint):
image_alpha = 255
else:
image_alpha = 190
if event.type == :
if bigdice_rect.collidepoint): # 按骰子
if presentPlayer != player_1:
rollDiceSound.play(1, 2000)
(2000)
showYes_No = (buildings, allplayers)
whetherYes_NoJudge = showYes_No
presentPlayer = player_1
else:
= ['还没到你的回合!']
if ): # 按回合结束
showButton2 = True
if presentPlayer != player_com1:
showYes_No = (buildings, allplayers)
presentPlayer = player_com1
else:
= ['还没到你的回合!']
else:
showButton2 = False
# 不显示Yes_No的时候不能点击它们!
if whetherYes_NoJudge == True:
if ye): # 按是否
showYes2 = True
if no_rect.collidepoint): # 按是否
showNo2 = True
if event.type == :
if ): # 按回合结束
showButton2 = False
if ye): # 按是否
showYes2 = False
showYes_No = False
# 只有在可以判定的时候才能算按下了是 同时将判断条件置为空
if whetherYes_NoJudge == True:
pressYes = True
whetherYes_NoJudge = False
if no_rect.collidepoint): # 按是否
showNo2 = False
pressYes = False
showYes_No = False
whetherYes_NoJudge = False
# 测试事件选项
if event.type == :
if event.key == :
showYes_No = (buildings, allplayers)
whetherYes_NoJudge = showYes_No
presentPlayer = player_1
if event.key == :
showYes_No = (buildings, allplayers)
presentPlayer = player_com1
'''for each in allplayers:
if each.isGoingToMove == True and each.movable == True :
showYes_No = each.move(buildings,allplayers)
each.movable = False
each.isGoingToMove = False'''
'''
allisready = True
for each in allplayers:
if each.movable == True:
allisready = False
if allisready:
for each in allplayers:
each.movable = True
'''
# 购买房屋!!!!!!!!
if (pressYes) == True:
pressYes = False
if (pressYes) == True:
pressYes = False
#########################################################################
(backgroud, (0, 0))
blit_alpha(screen, bigdice_image, (50, 600), image_alpha)
textPosition = [MapMessageBoxPosition[0], MapMessageBoxPosition[1]]
# 打印信息
for each in :
text = (each, True, white, textColorInMessageBox)
(text, textPosition)
textPosition[1] += 30
# 播放行动声音
if != 0:
playList[ - 1].play()
= 0
# 在位置上显示过路费
for i in range(1, 8):
for each in buildings:
for every in each.location:
if i == every:
if each.owner == :
text = ('%d' % * + 1)) \
, True, red)
elif each.owner == 'no':
text = ('%d' % * + 1)) \
, True, white)
elif each.owner != and each.owner != 'no':
text = ('%d' % * + 1)) \
, True, black)
(text, MapChessPosition_Payment[i])
for i in range(9, 16):
for each in buildings:
for every in each.location:
if i == every:
if each.owner == :
text = ('%d' % * + 1)) \
, True, red)
elif each.owner == 'no':
text = ('%d' % * + 1)) \
, True, white)
elif each.owner != and each.owner != 'no':
text = ('%d' % * + 1)) \
, True, black)
(text, MapChessPosition_Payment[i])
# 打印金钱数和幸运状态
money_1 = + '金钱:%d' % , True, black, white)
(money_1, (0, 0))
if == True:
(pohuaishen, (0, 30))
else:
blit_alpha(screen, pohuaishen, (0, 30), half_alpha)
if == True:
(caishen, (55, 30))
else:
blit_alpha(screen, caishen, (55, 30), half_alpha)
if == True:
(shuaishen, (110, 30))
else:
blit_alpha(screen, shuaishen, (110, 30), half_alpha)
if == True:
(tudishen, (165, 30))
else:
blit_alpha(screen, tudishen, (165, 30), half_alpha)
money_2 = + '金钱:%d' % , True, black, white)
(money_2, (1000, 0))
if == True:
(pohuaishen, (1000, 30))
else:
blit_alpha(screen, pohuaishen, (1000, 30), half_alpha)
if == True:
(caishen, (1055, 30))
else:
blit_alpha(screen, caishen, (1055, 30), half_alpha)
if == True:
(shuaishen, (1110, 30))
else:
blit_alpha(screen, shuaishen, (1110, 30), half_alpha)
if == True:
(tudishen, (1165, 30))
else:
blit_alpha(screen, tudishen, (1165, 30), half_alpha)
# 放置扔出来的骰子
if != 0 and showdice:
(dices[ - 1], (70, 450))
# 放置回合结束按钮
if showButton2:
(turnover2, TurnOvwrButtonPosition)
else:
(turnover, TurnOvwrButtonPosition)
# 放置是否按钮
if showYes_No == True:
(yes, YesNoMessageBoxPosition[0])
(no, YesNoMessageBoxPosition[1])
if showYes2 == True:
(yes2, YesNoMessageBoxPosition[0])
if showNo2 == True:
(no2, YesNoMessageBoxPosition[1])
# 放置玩家与电脑的位置 如果重合则挪位
for each in players:
for every in computers:
if each.position == every.position:
(each.image, MapChessPosition_Player[each.position])
(every.image, MapChessPosition_Com[every.position])
each.temp_position = True
every.temp_position = True
for each in players:
if each.temp_position == False:
(each.image, MapChessPosition_Original[each.position])
each.temp_position = True
each.temp_position = not each.temp_position
for every in computers:
if every.temp_position == False:
(every.image, MapChessPosition_Original[every.position])
every.temp_position = True
every.temp_position = not every.temp_position
# 输赢判断
for each in allplayers:
if each.money <= 0:
font = ('resource\\font\\my;, 200)
loseText = + '输了!', True, red)
(black)
(loseText, (100, 100))
font = ('resource\\font\\my;, 30)
(3000)
# 画面运行
()
clock.tick(60) # 刷新率
# 双击打开运行
if __name__ == "__main__":
main()
总结
又是一个下雨的周末,小朋友说:妈妈,我们玩"大富翁"的游戏吧!
妈妈说:”没没图纸,等下次到超市买一副“
小朋友说:”妈妈,你等等啊,我现在就敲代码了” 然后他就做了一个Python版的《大富翁》游
戏!可以一直玩了!哈哈哈哈 这个代码有没有深得你♥。
完整的素材等:滴滴我吖!都免费地!
往期推荐阅读——
项目0.1 宝藏拼图神秘上线,三种玩法刷爆朋友圈—玩家直呼太上瘾了。
项目0.2 【Pygame小游戏】Python版有迷宫嘛?原来藏在个地方呀~
项目1.6 【Pygame小游戏】斗地主我见多了,BUT 这款开源欢乐斗地主,最让人服气~
项目1.7 【Pygame小游戏】神还原【欢乐无穷的双人坦克大战】小程序游戏,上手开玩~
还有更多更多源码等你来领区啦!
文章汇总——
汇总: Python文章合集 | (入门到实战、游戏、Turtle、案例等)