关于pygame的游戏开发求辅助
pygame吧
全部回复
仅看楼主
level 9
最近想做一个pygame开发的2D游戏,游戏类型属于角色扮演类的,但是网上了解最多的都是射击类游戏,不知道这类角色扮演的怎么去做,游戏地图有了以后如何去根据图走,是根据数组还是啥?还有如果有迷宫如何解决?好多不懂啊,求解!
2014年07月12日 01点07分 1
level 4
哥们(姐们)如果你是初学者,劝你还是暂时放弃,我用pygame半年多,你的要求据我分析,要十分精通数学,所以你要在修炼一下才行
2014年11月28日 12点11分 2
数学不好我会去编程→_→
2014年11月30日 03点11分
level 1
用A*寻路算法
2015年03月04日 07点03分 3
level 1
地图可以用Tiled制作,通过pytmx这个扩展来读取。当然你也可以自己写程序来读取Tiled生成的tmx地图文件,其实就是个xml。
寻路嘛,用A*算法好了。这是我自己写的实现代码段,供你参考。对了,tmx的数据我预处理成了二维元组传入astart类。pathPoint是一个辅助类。
class point(object):
def __init__(self,x,y):
self.__x=x
self.__y=y
def getx(self):
return self.__x
def setx(self,x):
self.__x=x
x=property(getx,setx)
def gety(self):
return self.__y
def sety(self,y):
self.__y=y
y=property(gety,sety)
class pathPoint(point):
def __init__(self,x,y,parent=None,ValG=0,ValH=0):
point.__init__(self, x, y)
self.parent=parent
self._valF=ValG+ValH
self._valG=ValG
self._valH=ValH
def _getValG(self):
return self._valG
def _setValG(self, value):
self._valG = value
self._valF=self._valG+self._valH
ValG = property(_getValG, _setValG)
def _getValH(self):
return self._valH
def _setValH(self, value):
self._valH = value
self._valF=self._valG+self._valH
ValH = property(_getValH, _setValH)
def _getValF(self):
return self._valF
ValF = property(_getValF)
class aStar(object):
def __init__(self,startX,startY,endX,endY,aMap):
self.startPoint=pathPoint(startX,startY)
self.endPoint=pathPoint(endX,endY)
self.Map=aMap
self.width=len(self.Map[0])
self.height=len(self.Map)
self.openTable=[]
self.closeTable=[]
def searchPath(self):
self.closeTable.append(self.startPoint)
searching=True
parentPoint=self.startPoint
while searching:
nowPoint=self.checkAround(parentPoint)
if nowPoint==None:
id=self.checkTable(self.openTable,parentPoint.x,parentPoint.y)
if id>=0:
del self.openTable[id]
id=self.checkTable(self.closeTable,parentPoint.x,parentPoint.y)
if id>=0:
searching=False
else:
self.closeTable.append(parentPoint)
if parentPoint.parent==None:
searching=False
else:
nowPoint=parentPoint.parent
elif nowPoint.x==self.endPoint.x and nowPoint.y==self.endPoint.y:
self.endPoint=nowPoint
searching=False
parentPoint=nowPoint
#print "closeTable:"
#for i in self.closeTable:
# print "("+str(i.x)+","+str(i.y)+":"+str(i.ValF)+" ) "
if nowPoint !=None:
return self.getPath()
else:
return []
def checkAround(self,p):
result=None
minF=999999
checkW=((1,0),(0,1),(-1,0),(0,-1))
tp=None
if p==None:
return result
#print "checkAround:"+"("+str(p.x)+","+str(p.y)+":"+str(p.ValF)+" ) "
for i in checkW:
tx=p.x+i[0]
ty=p.y+i[1]
#print "checking:"+"("+str(tx)+","+str(ty)+":"+" )"
if tx<0 or tx>=self.width or ty<0 or ty>=self.height:
#print "is out"
continue
tg=self.Map[ty][tx]
if tg<9999:
tg+=p.ValG
if self.checkTable(self.closeTable,tx,ty)>=0:
#print "is closed"
continue
th=self.calH(tx,ty)
tf=tg+th
inOpen=self.checkTable(self.openTable,tx,ty)
if inOpen<0:
tp=pathPoint(tx,ty,p,tg,th)
self.openTable.append(tp)
elif self.openTable[inOpen].ValF>=tf:
self.openTable[inOpen].ValG=tg
self.openTable[inOpen].ValH=th
self.openTable[inOpen].parent=p
tp=self.openTable[inOpen]
#print "valF:"+str(tf)
if tp!=None:
if minF>tp.ValF:
result=tp
minF=tp.ValF
#else:
#print "is break"
#if result!=None:
# print "this point is good:"+"("+str(result.x)+","+str(result.y)+":"+str(result.ValF)+" )"
return result
def getPath(self):
result=[]
geting=True
tp=self.endPoint
while geting:
#print "("+str(tp.x)+","+str(tp.y)+"),"
result.append((tp.x,tp.y))
if tp==None:
geting=False
elif tp.parent!=None:
tp=tp.parent
else:
geting=False
return result
def checkTable(self,tb,x,y):
for id,p in enumerate(tb):
if p.x==x and p.y==y:
return id
return -1
def calH(self,nx,ny):
return math.sqrt((self.endPoint.x-nx)**2)+math.sqrt((self.endPoint.y-ny)**2)
这是我用pygame做的一个astar算法演示程序截图。
2015年03月23日 14点03分 4
kao!程序的缩进呢?
2015年03月23日 14点03分
@bd_lykyl 要淡定百度的编辑器……xml我是早知道了
2015年03月23日 16点03分
level 2
你可以翻翻RPGmaker系列的脚本。。虽然是ruby的但是随便看看ruby的核心语法也能看懂其中的精髓。。虽然我看不懂[汗]
2015年05月31日 15点05分 5
活捉活捉捉捉捉
2015年07月19日 01点07分
回复
С��è678
:[阴险][阴险][阴险]
2015年08月03日 00点08分
level 9
2016年03月03日 05点03分 6
level 3
判断是否可以走路可以用黑白的底层图片加上pil的getpixel寻路用楼上(大雾)的算法呗。我觉得黑白图可能比xml要简单点
2016年05月22日 08点05分 7
能处理简单点,为何要去烧CPU呢[呵呵]
2016年07月10日 09点07分
1