Skip to content

Commit

Permalink
next
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jul 9, 2016
1 parent 1fe219b commit 4da1411
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 36 deletions.
72 changes: 45 additions & 27 deletions python2-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

FONT = 'thin9pt' #metrix7pt
HEIGHT = 20
WIDTH = 10
BORDER = WOOL_BLACK
Expand Down Expand Up @@ -54,41 +55,46 @@ 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
Expand All @@ -112,6 +118,9 @@ 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 addPiece(x, y, piece, color):
global score,level,totalDropped
Expand Down Expand Up @@ -144,22 +153,22 @@ 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()
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 @@ -170,8 +179,8 @@ def updateText(buffer,x,y,text):

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))
scoreBuffer = updateText(scoreBuffer,left+WIDTH+2,bottom+HEIGHT-10,str(score),text.ALIGN_LEFT)
levelBuffer = updateText(levelBuffer,left,bottom+HEIGHT-10,str(level),text.ALIGN_RIGHT)
currentDescendDelay = DELAYS[level-1]

mc = Minecraft()
Expand All @@ -193,6 +202,7 @@ def updateScoreAndLevel():
levelBuffer = {}
showNext = False
updateScoreAndLevel()
makeNext()

newPiece = True

Expand Down Expand Up @@ -233,6 +243,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 python2-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 4da1411

Please sign in to comment.