Skip to content

Commit

Permalink
yn
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jul 9, 2016
1 parent ee1e80b commit 8bfc555
Showing 1 changed file with 168 additions and 124 deletions.
292 changes: 168 additions & 124 deletions python2-scripts/mcpipy/minetris.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,55 @@
(('XX', '.XX'), ('.X','XX', 'X.')),
(('.XX', 'XX'), ('X.', 'XX', '.X')) )

############################################################
## The following key-check functions are Windows specific ##
def inputMoveDown():
return (win32api.GetAsyncKeyState(win32con.VK_DOWN)&1)

def inputMoveLeft():
return (win32api.GetAsyncKeyState(win32con.VK_LEFT)&1)

def inputMoveRight():
return (win32api.GetAsyncKeyState(win32con.VK_RIGHT)&1)

def inputRotateLeft():
return (win32api.GetAsyncKeyState(win32con.VK_PRIOR)&1)

def inputRotateRight():
return ((win32api.GetAsyncKeyState(win32con.VK_NEXT)&1) or
(win32api.GetAsyncKeyState(win32con.VK_UP)&1))

def inputNext():
return (win32api.GetAsyncKeyState(ord('N'))&1)

def inputLevelUp():
return (win32api.GetAsyncKeyState(ord('L'))&1)

def inputPause():
return (win32api.GetAsyncKeyState(ord('P'))&1)

def answerYes():
clearState(ord('Y'))
clearState(ord('N'))
while True:
if win32api.GetAsyncKeyState(ord('Y')) & 1:
return True
if win32api.GetAsyncKeyState(ord('N')) & 1:
return False
sleep(0.1)

def clearState(k):
while win32api.GetAsyncKeyState(k) & 1:
pass

def clearInput():
for k in (win32con.VK_DOWN, win32con.VK_LEFT, win32con.VK_RIGHT,
win32con.VK_PRIOR, win32con.VK_NEXT, win32con.VK_UP,
ord('N'), ord('L'), ord('P'), ord('Y')):
clearState(k)
## End of Windows specific code ##
############################################################

def drawBoard():
mc.setBlocks(left-1, bottom-1, plane, left+WIDTH, bottom-1, plane, BORDER)
mc.setBlocks(left-1, bottom+HEIGHT, plane, left+WIDTH, bottom+HEIGHT, plane, BORDER)
Expand Down Expand Up @@ -75,7 +124,7 @@ def makeNext():
nextOrientation = randint(0, len(nextFamily)-1)

def placePiece():
global color, family, orientation, x, y, fall, descendDelay, droppedFrom, didShowNext
global color, family, orientation, descendDelay, droppedFrom, didShowNext
family = nextFamily
orientation = nextOrientation
color = nextColor
Expand All @@ -84,11 +133,11 @@ def placePiece():
x = WIDTH // 2 - pieceWidth(piece)
y = HEIGHT + len(piece) - 2
descendDelay = currentDescendDelay
fall = False
droppedFrom = None
didShowNext = showNext
if showNext:
drawNext()
return (x,y)

def fit(x, y, piece):
for (xx,yy) in enumeratePiece(x, y, piece):
Expand All @@ -103,35 +152,6 @@ def descend():
return True
return False

############################################################
## The following key-check functions are Windows specific ##
def moveDown():
return (win32api.GetAsyncKeyState(win32con.VK_DOWN)&1)

def moveLeft():
return (win32api.GetAsyncKeyState(win32con.VK_LEFT)&1)

def moveRight():
return (win32api.GetAsyncKeyState(win32con.VK_RIGHT)&1)

def rotateLeft():
return (win32api.GetAsyncKeyState(win32con.VK_PRIOR)&1)

def rotateRight():
return ((win32api.GetAsyncKeyState(win32con.VK_NEXT)&1) or
(win32api.GetAsyncKeyState(win32con.VK_UP)&1))

def next():
return (win32api.GetAsyncKeyState(ord('N'))&1)

def levelUp():
return (win32api.GetAsyncKeyState(ord('L'))&1)

def pause():
return (win32api.GetAsyncKeyState(ord('P'))&1)
## End of Windows specific code ##
############################################################

def hide():
mc.setBlocks(left, bottom, plane, left+WIDTH-1, bottom+HEIGHT-1, plane, GLASS)
text.drawText(mc, FONTS['nicefontbold'],
Expand All @@ -140,7 +160,7 @@ def hide():
"P", SEA_LANTERN, align=text.ALIGN_CENTER)


def restore():
def restore(x, y):
for xx in range(WIDTH):
for yy in range(HEIGHT):
mc.setBlock(xx+left,yy+bottom,plane,board[xx][yy] or AIR)
Expand Down Expand Up @@ -187,10 +207,11 @@ def addPiece(x, y, piece, color):

def updateText(buffer,x,y,s,align):
newBuffer = {}
text.drawText(mc, FONTS['thin9pt'],
Vec3(x,y,plane),
Vec3(1,0,0), Vec3(0,1,0),
s, SEA_LANTERN, background=None, align=align, buffer=newBuffer)
if s is not None:
text.drawText(mc, FONTS['thin9pt'],
Vec3(x,y,plane),
Vec3(1,0,0), Vec3(0,1,0),
s, SEA_LANTERN, background=None, align=align, buffer=newBuffer)
for pos in buffer:
if pos not in newBuffer:
mc.setBlock(pos, AIR)
Expand All @@ -207,6 +228,109 @@ def updateScoreAndLevel():
levelBuffer = updateText(levelBuffer,left-1,bottom+HEIGHT-10,str(level),text.ALIGN_RIGHT)
currentDescendDelay = DELAYS[level-1]

def clearScoreAndLevel():
global scoreBuffer, levelBuffer, currentDescendDelay, level
scoreBuffer = updateText(scoreBuffer,left+WIDTH+2,bottom+HEIGHT-10,None,text.ALIGN_LEFT)
levelBuffer = updateText(levelBuffer,left-1,bottom+HEIGHT-10,None,text.ALIGN_RIGHT)

def game():
global score, level, extraLevels, totalDropped, scoreBuffer, levelBuffer, showNext, didShowNext
global orientation, board, descendTimer, droppedFrom, descendDelay

board = [[None for i in range(HEIGHT)] for j in range(WIDTH)]

drawBoard()
score = 0
level = 1
extraLevels = 0
totalDropped = 0
scoreBuffer = {}
levelBuffer = {}
showNext = False
updateScoreAndLevel()
makeNext()

newPiece = True

while True:
if newPiece:
x,y = placePiece()
oldPiece = None
if not fit(x, y, family[orientation]):
break
draw = True
newPiece = False
fall = False
clearInput()
descendTimer = time()
else:
oldPiece = family[orientation]
draw = False
oldX = x
oldY = y

if inputPause():
t0 = time()
hide()
while not inputPause():
sleep(0.025)
clearInput()
restore(x, y)
descendTimer += time() - t0

if not fall:
if inputLevelUp():
extraLevels += 1
level += 1
updateScoreAndLevel()
descendDelay = currentDescendDelay

if inputMoveLeft() and fit(x-1, y, family[orientation]):
x -= 1
draw = True

if inputMoveRight() and fit(x+1, y, family[orientation]):
x += 1
draw = True

if inputRotateLeft() and fit(x, y, family[(orientation-1)%len(family)]):
orientation = (orientation-1)%len(family)
draw = True

if inputRotateRight() and fit(x, y, family[(orientation+1)%len(family)]):
orientation = (orientation+1)%len(family)
draw = True

if inputMoveDown():
fall = True
droppedFrom = y+1-len(family[orientation])
descendDelay = 0.05

if inputNext():
showNext = not showNext
if showNext:
didShowNext = True
drawNext()
else:
eraseNext()

if descend():
if not fit(x, y-1, family[orientation]):
if droppedFrom is None:
droppedFrom = y+1-len(family[orientation])
addPiece(x, y, family[orientation], color)
newPiece = True
else:
draw = True
y -= 1

if draw:
movePiece(oldX, oldY, oldPiece, x, y, family[orientation], color)

sleep(0.025)

return score

mc = Minecraft()

mc.postToChat("Left/Right arrow: move")
Expand All @@ -220,96 +344,16 @@ def updateScoreAndLevel():
mc.player.setRotation(180)
mc.player.setPitch(-26)
mc.player.setTilePos(playerPos.x, playerPos.y, playerPos.z + 14)

left = playerPos.x - WIDTH // 2
plane = playerPos.z
bottom = playerPos.y + 1
board = [[None for i in range(HEIGHT)] for j in range(WIDTH)]

drawBoard()
score = 0
level = 1
extraLevels = 0
totalDropped = 0
scoreBuffer = {}
levelBuffer = {}
showNext = False
updateScoreAndLevel()
makeNext()

newPiece = True

while True:
if newPiece:
placePiece()
oldPiece = None
if not fit(x, y, family[orientation]):
mc.postToChat("Doesn't fit: End of game")
break
draw = True
newPiece = False
descendTimer = time()
else:
oldPiece = family[orientation]
draw = False
oldX = x
oldY = y

if pause():
t0 = time()
hide()
while not pause():
sleep(0.025)
restore()
descendTimer += time() - t0

if not fall:
if levelUp():
extraLevels += 1
level += 1
updateScoreAndLevel()
descendDelay = currentDescendDelay

if moveLeft() and fit(x-1, y, family[orientation]):
x -= 1
draw = True

if moveRight() and fit(x+1, y, family[orientation]):
x += 1
draw = True

if rotateLeft() and fit(x, y, family[(orientation-1)%len(family)]):
orientation = (orientation-1)%len(family)
draw = True

if rotateRight() and fit(x, y, family[(orientation+1)%len(family)]):
orientation = (orientation+1)%len(family)
draw = True

if moveDown():
fall = True
droppedFrom = y+1-len(family[orientation])
descendDelay = 0.05

if next():
showNext = not showNext
if showNext:
didShowNext = True
drawNext()
else:
eraseNext()

if descend():
if not fit(x, y-1, family[orientation]):
if droppedFrom is None:
droppedFrom = y+1-len(family[orientation])
addPiece(x, y, family[orientation], color)
newPiece = True
else:
draw = True
y -= 1

if draw:
movePiece(oldX, oldY, oldPiece, x, y, family[orientation], color)

sleep(0.025)

s = game()
mc.postToChat("Game Over: You got %d points" % s)
mc.postToChat("Play again? (Y/N)")
if not answerYes():
mc.postToChat("Goodbye!")
break
clearScoreAndLevel()

0 comments on commit 8bfc555

Please sign in to comment.