用python的pygame写一个大富翁游戏(单机版) 二 :地图初始化
往期:
#用python的pygame写一个大富翁游戏(单机版)# 一 : 初步架构
上次的博客,我们说到了准备好所有程序所需的材料,包括图片、地图、人物形象、初始化玩家类和地图格子类等。
这一篇文章,我们将对地图进行初始化,包括以下内容:
- 根据我们画好的地图,初始化每一格所对应的位置,以及玩家在这个地图的移动规则
- 完成显示玩家信息的对象类
- 完成获取鼠标点击操作的对象类
第一步是为了在之后能够把人物形象的图标正确移动到地图格上的对应位置
第二步是为了之后实时显示玩家的金钱、姓名等信息
第三步是为了能够准确获取鼠标在图中的点击位置,这一步其实是偷了个懒,没有设置button类,而是通过固定图片含有功能的图标的点击位置,同时获取鼠标位置来实现按钮效果。 由于本次的程序为大富翁,按钮简单,所以第三步完全可行。
接下来进行程序实现:
首先完成第一步
根据自己的地图,首先获取每一格的中心位置坐标(x,y),之后我们移动小人到对应位置就是根据这个位置来移动。
我的地图中的对应位置是(括号里的数字单位是像素):
{1:(190,40),2:(325,35),3:(435,35),4:(530,35),5:(630,35),6:(730,35),7:(830,35),8:(945,40),
9:(985,135),10:(985,270),11:(985,365),12:(985,475),13:(940,550),14:(850,550),15:(760,550),
16:(650,550),17:(540,550),18:(435,550),19:(330,550),20:(225,550),21:(120,485),22:(120,390),
23:(120,230),24:(120,115)}
我的玩家移动规则:
走完一圈补充2000元游戏资金。
为了保证玩家的游戏资金补充,每次玩家移动,都需要判断玩家是否走完一圈跨越起点了,如果跨越了起点,那么就需要给玩家的玩家池中的资金类加入2000元。
- 这里的实现是:
因为地图一共只有24个格子,我们设置玩家每走过一个格子就给玩家的对应位置状态变量+1,如果此变量超过了24,那么就说明玩家走完了一圈,跨越了起点,这时给玩家的位置变量减去24,就可以实现位置是否跨越起点的判断了。
由于我们每次移动玩家的格子数是根据玩家掷出的骰子点数决定的,所以,我们还需要完成一个骰子类,包括骰子的动画、骰子的点数获取。
- 这里的实现是:在网上找到了一个骰子动画的分帧图片,然后轮流显示,最后根据程序随机出来的1到6的一个数来显示最终的点数对应的图:
(图中,带有下划线命名的是结果点数图,不带下划线的是动画的分帧图)
- 骰子动画实现:
#骰子动画实现程序
import sys
sys.path.append(r'F:/python文件库/richman')
import pygame
import time
from setting import *
def dice_gif(screen):#骰子动画
list=[dice1,dice2,dice3,dice4,dice5,dice6,dice7,dice8]
for x in range(0,1):
for t in list:
screen.blit(t,(290,250))
pygame.display.update()
time.sleep(0.08)
def dice_number_display(screen,dice_number):
list={'0':dice_1,'1':dice_1,'2':dice_2,'3':dice_3,'4':dice_4,'5':dice_5,'6':dice_6}
screen.blit (list['%d'%dice_number],(290,250))
pygame.display.update()
- 对于它的调用:
dice_gif (screen) #骰子动画
dice_number = random.randint (1,6) #随机得到一个数字
dice_number_display (screen,dice_number) #显示骰子
第一步的最后,我们根据已经写好的部分完成玩家图标的移动显示:
if last_loction < play_pool[turn_key].loction: #判断连续两次位置是否跨一圈
for t in range (last_loction,play_pool[turn_key].loction + 1):
x,y = list[t]
screen.blit (background,(0,0))
dice_number_display (screen,dice_number)
display_play_data (screen,play_pool)
screen.blit (list_player_picture[play_pool[turn_key].character],
(x + turn_key * 10,y + turn_key * 20))
for p in play_pool:
if p != play_pool[turn_key]:
screen.blit (list_player_picture[p.character],(p.x,p.y))
display_player_name_dice (screen,play_pool,x,y,turn_key)
display_host_name (screen,list_grid_pool)
pygame.display.update ()
time.sleep (0.1)
elif last_loction > play_pool[turn_key].loction:
money=money+2000
for t in range (last_loction,25):
x,y = list[t]
screen.blit (background,(0,0))
display_play_data (screen,play_pool)
dice_number_display (screen,dice_number)
screen.blit (list_player_picture[play_pool[turn_key].character],
(x + turn_key * 10,y + turn_key * 20))
for p in play_pool:
if p != play_pool[turn_key]:
screen.blit (list_player_picture[p.character],(p.x,p.y))
display_player_name_dice (screen,play_pool,x,y,turn_key)
display_host_name (screen,list_grid_pool)
pygame.display.update ()
time.sleep (0.1)
for t in range (1,play_pool[turn_key].loction + 1):
x,y = list[t]
screen.blit (background,(0,0))
display_play_data (screen,play_pool)
dice_number_display (screen,dice_number)
screen.blit (list_player_picture[play_pool[turn_key].character],
(x + turn_key * 10,y + turn_key * 20))
for p in play_pool:
if p != play_pool[turn_key]:
screen.blit (list_player_picture[p.character],(p.x,p.y))
display_player_name_dice (screen,play_pool,x,y,turn_key)
display_host_name (screen,list_grid_pool)
pygame.display.update ()
time.sleep (0.1)
最后记得更新玩家池的数据:
play_pool[turn_key].x=x+turn_key*10
play_pool[turn_key].y=y+turn_key*20
play_pool[turn_key].money=money
第二步
显示玩家的信息,主要是调用系统自带的字体,并且找到对应位置即可,比较简单:
显示名字:
def display_player_name(screen,play_pool):#显示玩家名字
text_font = pygame.font.Font ('C:\Windows\Fonts\simhei.ttf',15)
t=0
for i in play_pool:
text = text_font.render (i.name,True,(255-t*85,t*85,t*85))
t=t+1
screen.blit(text,(i.x-5,i.y-20))
def display_player_name_dice(screen,play_pool,x,y,turn_key):#显示玩家掷出的骰子名字
text_font = pygame.font.Font ('C:\Windows\Fonts\simhei.ttf',15)
t=0
for i in play_pool:
if i!=play_pool[turn_key]:
text = text_font.render (i.name,True,(255-t*85,t*85,t*85))
screen.blit(text,(i.x-5,i.y-20))
else:
text = text_font.render (play_pool[turn_key].name,True,(255-t*85,t*85,t*85))
screen.blit (text,(x - 5+turn_key * 10,y - 20+turn_key*20))
t = t + 1
显示信息:
def display_play_data(screen,play_pool):#显示玩家数据
number=1
for t in play_pool:
text='玩家'+'%d'%number + ':' + '%d'%play_pool[number-1].money
text_fmt=font_x(text)
screen.blit(text_fmt,(250,370+number*30))
number=number+1
第三步
获取鼠标位置,这一步也很简单,只需要根据pygame自带的鼠标获取接口配合好就可以了,同时要注意判断鼠标点击位置在不在自己的地图标定区域内部。
这里举例一部分代码:
def key_control_select (): #游戏操作——获取按键操作
k = 0
x,y = pygame.mouse.get_pos ()
for event in pygame.event.get ():
if event.type == pygame.QUIT:
exit ()
elif event.type == pygame.MOUSEBUTTONDOWN:
print ('x=',x) # 显示鼠标点击位置
print ('y=',y)
if x >= 5 and x <= 180 and y >= 185 and y <= 800:
k = 1
elif True:
k = 0
return k
代码中的print是debug信息,不会显示在pygame的窗口里,
||||||||||||||||||||||||||||||- e n d -||||||||||||||||||||||||||||||||||||||