Skip to content

Commit

Permalink
minetris works
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jul 9, 2016
1 parent 9032ed8 commit ee1e80b
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 45 deletions.
Binary file modified python2-scripts.zip
Binary file not shown.
8 changes: 6 additions & 2 deletions python2-scripts/mcpipy/minetris.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from random import randint
import text
from fonts import FONTS
import win32con,win32api
import win32con,win32api ## Windows-specific

FONT = 'thin9pt' #metrix7pt
HEIGHT = 20
Expand Down Expand Up @@ -102,7 +102,9 @@ def descend():
descendTimer += descendDelay
return True
return False


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

Expand All @@ -127,6 +129,8 @@ def levelUp():

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)
Expand Down
Binary file modified python3-scripts.zip
Binary file not shown.
132 changes: 98 additions & 34 deletions python3-scripts/mcpipy/minetris.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from mc import *
from time import sleep,time
from random import randint
from text import drawText
import text
from fonts import FONTS
import win32con,win32api
import win32con,win32api ## Windows-specific

FONT = 'thin9pt' #metrix7pt
HEIGHT = 20
WIDTH = 10
BORDER = WOOL_BLACK
Expand Down Expand Up @@ -54,49 +55,56 @@ def movePiece(oldX, oldY, oldPiece, x, y, piece, color):
for (x,y) in new:
mc.setBlock(x+left, y+bottom, plane, color)

def erasePiece(buffer, x, y, piece):
for (xx,yy) in enumeratePiece(x,y, piece):
buffer[(left+xx,bottom+yy)] = AIR

def drawPiece(buffer, x, y, piece, color):
for (xx,yy) in enumeratePiece(x,y, piece):
if (left+xx,bottom+yy) in buffer and buffer[(left+xx,bottom+yy)] == AIR:
del buffer[(left+xx,bottom+yy)]
else:
buffer[(left+xx,bottom+yy)] = color

def eraseNext():
mc.setBlocks(left+WIDTH+2,bottom+3,plane,left+WIDTH+2+3,bottom+6,plane,AIR)

def drawNext():
eraseNext()
for (x,y) in enumeratePiece(WIDTH+2, 6, nextFamily[nextOrientation]):
mc.setBlock(x+left, y+bottom, plane, nextColor)

def drawBuffer(buffer):
for x,y in buffer:
mc.setBlock(x,y,plane,buffer[(x,y)])

def makeNext():
global nextFamily, nextColor, nextOrientation
n = randint(0, len(PIECES)-1)
nextFamily = PIECES[n]
nextColor = Block(WOOL.id, (n+1) % 16)
nextOrientation = randint(0, len(nextFamily)-1)

def placePiece():
global pieceNum, color, family, orientation, x, y, fall, descendDelay, droppedFrom, didShowNext
pieceNum = randint(0, len(PIECES)-1)
family = PIECES[pieceNum]
color = Block(WOOL.id, (pieceNum+1) % 16)
orientation = randint(0, len(family)-1)
global color, family, orientation, x, y, fall, descendDelay, droppedFrom, didShowNext
family = nextFamily
orientation = nextOrientation
color = nextColor
makeNext()
piece = family[orientation]
x = WIDTH // 2 - pieceWidth(piece)
y = HEIGHT + len(piece) - 2
descendDelay = currentDescendDelay
fall = False
droppedFrom = None
didShowNext = showNext
if showNext:
drawNext()

def fit(x, y, piece):
for (xx,yy) in enumeratePiece(x, y, piece):
if yy < 0 or xx >= WIDTH or xx < 0 or board[xx][yy] is not None:
return False
return True

return True

def descend():
global descendTimer
if descendTimer + descendDelay <= time():
descendTimer += descendDelay
return True
return False


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

Expand All @@ -112,6 +120,31 @@ def rotateLeft():
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'],
Vec3(left+WIDTH//2,bottom+5,plane),
Vec3(1,0,0), Vec3(0,1,0),
"P", SEA_LANTERN, align=text.ALIGN_CENTER)


def restore():
for xx in range(WIDTH):
for yy in range(HEIGHT):
mc.setBlock(xx+left,yy+bottom,plane,board[xx][yy] or AIR)
movePiece(None, None, None, x, y, family[orientation], color)

def addPiece(x, y, piece, color):
global score,level,totalDropped
Expand Down Expand Up @@ -144,22 +177,20 @@ def addPiece(x, y, piece, color):
break

if didShowNext:
score += 3 + (3*level)//2 + droppedFrom
score += 3 + (3*(level-1))//2 + droppedFrom
else:
score += 5 + 2*level + droppedFrom
score += 5 + 2*(level-1) + droppedFrom
if dropCount:
totalDropped += dropCount
level = 1 + totalDropped // 10
if level > 10:
level = 10
updateScoreAndLevel()
level = 1 + totalDropped // 10 + extraLevels
updateScoreAndLevel()

def updateText(buffer,x,y,text):
def updateText(buffer,x,y,s,align):
newBuffer = {}
drawText(mc, FONTS['metrix7pt'],
text.drawText(mc, FONTS['thin9pt'],
Vec3(x,y,plane),
Vec3(1,0,0), Vec3(0,1,0),
text, SEA_LANTERN, background=None, buffer=newBuffer)
s, SEA_LANTERN, background=None, align=align, buffer=newBuffer)
for pos in buffer:
if pos not in newBuffer:
mc.setBlock(pos, AIR)
Expand All @@ -169,17 +200,26 @@ def updateText(buffer,x,y,text):
return newBuffer

def updateScoreAndLevel():
global scoreBuffer, levelBuffer, currentDescendDelay
scoreBuffer = updateText(scoreBuffer,left+WIDTH+2,bottom+HEIGHT-10,str(score))
levelBuffer = updateText(levelBuffer,left-7,bottom+HEIGHT-10,str(level))
global scoreBuffer, levelBuffer, currentDescendDelay, level
if level > 10:
level = 10
scoreBuffer = updateText(scoreBuffer,left+WIDTH+2,bottom+HEIGHT-10,str(score),text.ALIGN_LEFT)
levelBuffer = updateText(levelBuffer,left-1,bottom+HEIGHT-10,str(level),text.ALIGN_RIGHT)
currentDescendDelay = DELAYS[level-1]

mc = Minecraft()

mc.postToChat("Left/Right arrow: move")
mc.postToChat("Up: rotate right")
mc.postToChat("PageUp/PageDown: rotate left/right")
mc.postToChat("N: toggle view next")
mc.postToChat("P: pause")
mc.postToChat("L: next level")

playerPos = mc.player.getTilePos()
mc.player.setRotation(180)
mc.player.setPitch(-26)
mc.player.setTilePos(playerPos.x+WIDTH//2, playerPos.y, playerPos.z + 13)
mc.player.setTilePos(playerPos.x, playerPos.y, playerPos.z + 14)
left = playerPos.x - WIDTH // 2
plane = playerPos.z
bottom = playerPos.y + 1
Expand All @@ -188,11 +228,13 @@ def updateScoreAndLevel():
drawBoard()
score = 0
level = 1
extraLevels = 0
totalDropped = 0
scoreBuffer = {}
levelBuffer = {}
showNext = False
updateScoreAndLevel()
makeNext()

newPiece = True

Expand All @@ -211,8 +253,22 @@ def updateScoreAndLevel():
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
Expand All @@ -233,6 +289,14 @@ def updateScoreAndLevel():
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]):
Expand Down
36 changes: 27 additions & 9 deletions python3-scripts/mcpipy/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import time
import mcpi.minecraft as minecraft

ALIGN_LEFT = 0
ALIGN_RIGHT = 1
ALIGN_CENTER = 2

# vectors must be minecraft.Vec3
def drawGlyph(mc, pos, forwardVec, upVec, glyph, foreground, background=None, buffer=None):
bitmap = glyph[3]
Expand All @@ -30,27 +34,41 @@ def drawGlyph(mc, pos, forwardVec, upVec, glyph, foreground, background=None, bu
pixelPos += forwardVec
return pos + forwardVec*delta

def textLength(font, text):
l = 0
for value in text:
try:
glyph = font[value]
except:
glyph = font[32]
l += glyph[2]
return l

def drawText(mc, font, pos, forwardVec, upVec, text, foreground, background=None, buffer=None):
def drawText(mc, font, pos, forwardVec, upVec, text, foreground=None, background=None, align=ALIGN_LEFT, buffer=None):
try:
text = bytearray(text.encode("cp1252"))
except:
text = bytearray(text.encode("iso8859_1"))
pixelPos = pos.clone()
height = len(font[32][3])
numLines = text.count(b'\n')+1
pixelPos += upVec * ((numLines-1) * height)
lines = text.split(b'\n')
pixelPos += upVec * ((len(lines)-1)* height)
lineStart = pixelPos.clone()
for value in text:
if value == 10:
lineStart += upVec * (-height)
pixelPos = lineStart.clone()
else:
for line in lines:
pixelPos = lineStart.clone()
if align == ALIGN_RIGHT:
pixelPos -= forwardVec * textLength(font, line)
elif align == ALIGN_CENTER:
pixelPos -= forwardVec * (0.5 * textLength(font, line))

for value in line:
try:
glyph = font[value]
except:
glyph = font[32]
pixelPos = drawGlyph(mc, pixelPos, forwardVec, upVec, glyph, foreground, background, buffer)

lineStart += upVec * (-height)
return pixelPos

def angleToTextDirectionCardinal(angle):
Expand Down Expand Up @@ -91,4 +109,4 @@ def angleToTextDirection(angle):
del sys.argv[0]
text = " ".join(sys.argv)

drawText(mc, FONTS['metrix7pt'], left, bottom+HEIGHT+1, forward, minecraft.Vec3(0,1,0), text, foreground, background)
drawText(mc, fonts.FONTS['metrix7pt'], pos, forward, minecraft.Vec3(0,1,0), text, foreground, background, align=ALIGN_RIGHT)

0 comments on commit ee1e80b

Please sign in to comment.