Hi I am new to this Forum and Panda3D.
Can somebody of you guys please help me with this peace of code? I am pretty new to Panda3D and Python programming in general. I want to implement this game in Panda3D:
mekanimoquickhelp.s3.amazonaws.c … uzzle.html
At the beginning everything looks right but after a while the buttons jumps into wrong locations and start overlapping with each other.
Here is the code:
import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText
from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode
import sys, random
from direct.task import Task
class PuzzleGame():
text = "Tile Puzzle Game"
empty_row = 3
empty_col = 3
def __init__(self):
textObject = OnscreenText(text = PuzzleGame.text, pos = (0.95,-0.95), scale = 0.07,fg=(1,1,1,1),align=TextNode.ACenter,mayChange=1)
self.createButtons()
def slide(self, xpara, ypara, button):
if(button == "btn01"):
x, y, col, row = self.slideInside(xpara,ypara, self.button01.getCol(), self.button01.getRow())
self.button01.setColRow(col, row)
self.button01.posButton(x,y)
if(button == "btn02"):
x, y, col, row = self.slideInside(xpara,ypara, self.button02.getCol(), self.button02.getRow())
self.button02.setColRow(col, row)
self.button02.posButton(x,y)
if(button == "btn03"):
x, y, col, row = self.slideInside(xpara,ypara, self.button03.getCol(), self.button03.getRow())
self.button03.setColRow(col, row)
self.button03.posButton(x,y)
if(button == "btn04"):
x, y, col, row = self.slideInside(xpara,ypara, self.button04.getCol(), self.button04.getRow())
self.button04.setColRow(col, row)
self.button04.posButton(x,y)
if(button == "btn05"):
x, y, col, row = self.slideInside(xpara,ypara, self.button05.getCol(), self.button05.getRow())
self.button05.setColRow(col, row)
self.button05.posButton(x,y)
if(button == "btn06"):
x, y, col, row = self.slideInside(xpara,ypara, self.button06.getCol(), self.button06.getRow())
self.button06.setColRow(col, row)
self.button06.posButton(x,y)
if(button == "btn07"):
x, y, col, row = self.slideInside(xpara,ypara, self.button07.getCol(), self.button07.getRow())
self.button07.setColRow(col, row)
self.button07.posButton(x,y)
if(button == "btn08"):
x, y, col, row = self.slideInside(xpara,ypara, self.button08.getCol(), self.button08.getRow())
self.button08.setColRow(col, row)
self.button08.posButton(x,y)
if(button == "btn09"):
x, y, col, row = self.slideInside(xpara,ypara, self.button09.getCol(), self.button09.getRow())
self.button09.setColRow(col, row)
self.button09.posButton(x,y)
if(button == "btn10"):
x, y, col, row = self.slideInside(xpara,ypara, self.button10.getCol(), self.button10.getRow())
self.button10.setColRow(col, row)
self.button10.posButton(x,y)
if(button == "btn11"):
x, y, col, row = self.slideInside(xpara,ypara, self.button11.getCol(), self.button11.getRow())
self.button11.setColRow(col, row)
self.button11.posButton(x,y)
if(button == "btn12"):
x, y, col, row = self.slideInside(xpara,ypara, self.button12.getCol(), self.button12.getRow())
self.button12.setColRow(col, row)
self.button12.posButton(x,y)
if(button == "btn13"):
x, y, col, row = self.slideInside(xpara,ypara, self.button13.getCol(), self.button13.getRow())
self.button13.setColRow(col, row)
self.button13.posButton(x,y)
if(button == "btn14"):
x, y, col, row = self.slideInside(xpara,ypara, self.button14.getCol(), self.button14.getRow())
self.button14.setColRow(col, row)
self.button14.posButton(x,y)
if(button == "btn15"):
x, y, col, row = self.slideInside(xpara,ypara, self.button15.getCol(), self.button15.getRow())
self.button15.setColRow(col, row)
self.button15.posButton(x,y)
def slideInside(self, x, y, col, row): # somewhere here must be the mistake
if PuzzleGame.empty_row == row and abs(PuzzleGame.empty_col - col) == 1:
PuzzleGame.empty_col, col = col, PuzzleGame.empty_col
x = col * SlideButton.length * 0.05 + SlideButton.offset
elif PuzzleGame.empty_col == col and abs(PuzzleGame.empty_row - row) == 1:
PuzzleGame.empty_row, row = row, PuzzleGame.empty_row
y = row * SlideButton.length * 0.05 + SlideButton.offset
return x, y, col, row
def createButtons(self):
self.button01 = SlideButton(0,0, "btn01", self)
self.button02 = SlideButton(0,1, "btn02", self)
self.button03 = SlideButton(0,2, "btn03", self)
self.button04 = SlideButton(0,3, "btn04", self)
self.button05 = SlideButton(1,0, "btn05", self)
self.button06 = SlideButton(1,1, "btn06", self)
self.button07 = SlideButton(1,2, "btn07", self)
self.button08 = SlideButton(1,3, "btn08", self)
self.button09 = SlideButton(2,0, "btn09", self)
self.button10 = SlideButton(2,1, "btn10", self)
self.button11 = SlideButton(2,2, "btn11", self)
self.button12 = SlideButton(2,3, "btn12", self)
self.button13 = SlideButton(3,0, "btn13", self)
self.button14 = SlideButton(3,1, "btn14", self)
self.button15 = SlideButton(3,2, "btn15", self)
DestroyButton(self.button01, self.button02,self.button03,self.button04,self.button05,self.button06,self.button07,self.button08,self.button09,self.button10,self.button11,self.button12,self.button13,self.button14,self.button15)
class DestroyButton():
def __init__(self, b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15):
self.b1 = b1
self.b2 = b2
self.b3 = b3
self.b4 = b4
self.b5 = b5
self.b6 = b6
self.b7 = b7
self.b8 = b8
self.b9 = b9
self.b10 = b10
self.b11 = b11
self.b12 = b12
self.b13 = b13
self.b14 = b14
self.b15 = b15
self.button = DirectButton(text = "Enough",pos = (0.75, 0, -0.3), scale = 0.05, frameSize = (-SlideButton.length,SlideButton.length,-SlideButton.length/2,SlideButton.length/2), command = self.destroyB)
def destroyB(self):
self.button.destroy()
self.b1.destroy()
self.b2.destroy()
self.b3.destroy()
self.b4.destroy()
self.b5.destroy()
self.b6.destroy()
self.b7.destroy()
self.b8.destroy()
self.b9.destroy()
self.b10.destroy()
self.b11.destroy()
self.b12.destroy()
self.b13.destroy()
self.b14.destroy()
self.b15.destroy()
class SlideButton():
rows = cols = 4
length = 4
offset = -0.3
choiseList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
random.shuffle(choiseList)
def __init__(self, col, row, name, oClass):
self.col = col
self.row = row
self.oClass = oClass
self.name = name
self.createButton()
def createButton(self):
self.x = self.col*SlideButton.length * 0.05 + SlideButton.offset
self.y = self.row*SlideButton.length * 0.05 + SlideButton.offset
value = SlideButton.choiseList[(self.row * SlideButton.cols + self.col + 1)-1]
self.button = DirectButton(text = (str(value)),pos = (self.x, 0, self.y), scale = 0.05, frameSize = (-SlideButton.length/2,SlideButton.length/2,-SlideButton.length/2,SlideButton.length/2), command = self.oClass.slide, extraArgs = [self.x, self.y, self.name])
def posButton(self,x,y):
self.button.setPos(x,1,y)
def setColRow(self,c,r):
self.col = c
self.row = r
def getCol(self):
return self.col
def getRow(self):
return self.row
def destroy(self):
self.button.destroy()
PuzzleGame()
run()
The problem must be somewhere inside the slideInside() funktion. Also maybe it has something to do with the extra scaling of 0.05 and the offset that I am using. I have got no clue! This peace of code drives me crazy.
That’s for your help.