Skip to content

Commit

Permalink
win32movefix
Browse files Browse the repository at this point in the history
  • Loading branch information
arpruss committed Jul 10, 2016
1 parent 8bfc555 commit d50d22d
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 168 deletions.
Binary file modified python2-scripts.zip
Binary file not shown.
148 changes: 148 additions & 0 deletions python2-scripts/mcpipy/input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#
# Windows-only (right now)
# Copyright (c) 2016 Alexander Pruss. MIT License
#

from platform import system

if system() == 'Windows':
from ctypes import windll

LBUTTON = 1
RBUTTON = 2
CANCEL = 3
MBUTTON = 4
BACK = 8
TAB = 9
CLEAR = 12
RETURN = 13
SHIFT = 16
CONTROL = 17
MENU = 18
PAUSE = 19
CAPITAL = 20
KANA = 21
HANGUL = 21
JUNJA = 23
FINAL = 24
HANJA = 25
KANJI = 25
ESCAPE = 27
CONVERT = 28
NONCONVERT = 29
ACCEPT = 30
MODECHANGE = 31
SPACE = 32
PRIOR = 33
NEXT = 34
END = 35
HOME = 36
LEFT = 37
UP = 38
RIGHT = 39
DOWN = 40
SELECT = 41
PRINT = 42
EXECUTE = 43
SNAPSHOT = 44
INSERT = 45
DELETE = 46
HELP = 47
LWIN = 91
RWIN = 92
APPS = 93
NUMPAD0 = 96
NUMPAD1 = 97
NUMPAD2 = 98
NUMPAD3 = 99
NUMPAD4 = 100
NUMPAD5 = 101
NUMPAD6 = 102
NUMPAD7 = 103
NUMPAD8 = 104
NUMPAD9 = 105
MULTIPLY = 106
ADD = 107
SEPARATOR = 108
SUBTRACT = 109
DECIMAL = 110
DIVIDE = 111
F1 = 112
F2 = 113
F3 = 114
F4 = 115
F5 = 116
F6 = 117
F7 = 118
F8 = 119
F9 = 120
F10 = 121
F11 = 122
F12 = 123
F13 = 124
F14 = 125
F15 = 126
F16 = 127
F17 = 128
F18 = 129
F19 = 130
F20 = 131
F21 = 132
F22 = 133
F23 = 134
F24 = 135
NUMLOCK = 144
SCROLL = 145
LSHIFT = 160
RSHIFT = 161
LCONTROL = 162
RCONTROL = 163
LMENU = 164
RMENU = 165
PROCESSKEY = 229
ATTN = 246
CRSEL = 247
EXSEL = 248
EREOF = 249
PLAY = 250
ZOOM = 251
NONAME = 252
PA1 = 253
OEM_CLEAR = 254
XBUTTON1 = 0x05
XBUTTON2 = 0x06
VOLUME_MUTE = 0xAD
VOLUME_DOWN = 0xAE
VOLUME_UP = 0xAF
MEDIA_NEXT_TRACK = 0xB0
MEDIA_PREV_TRACK = 0xB1
MEDIA_PLAY_PAUSE = 0xB3
BROWSER_BACK = 0xA6
BROWSER_FORWARD = 0xA7

def getPressState(key):
v = windll.user32.GetAsyncKeyState(int(key))
return bool(0x8000 & v), bool(0x0001 & v)

def isPressedNow(key):
return bool(0x8000 & windll.user32.GetAsyncKeyState(int(key)))

def wasPressedSinceLast(key):
return bool(0x0001 & windll.user32.GetAsyncKeyState(int(key)))

def clearPressBuffer(key):
while wasPressedSinceLast(key):
pass

if __name__ == '__main__':
from time import sleep
print("Press ESC to exit. Testing spacebar.")
while True:
if wasPressedSinceLast(ESCAPE):
print("Done")
break
now,last = getPressState(ord(' '))
if now or last:
print now, last
sleep(0.01)

49 changes: 23 additions & 26 deletions python2-scripts/mcpipy/minetris.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#
# requires Windows and pywin32
# requires Windows (for input.py to work)
# Copyright (c) 2016 Alexander Pruss. MIT License.
#

#
# Make sure to have input.py and text.py in the same directory.
#


from mc import *
from time import sleep,time
from random import randint
import input
import text
from fonts import FONTS
import win32con,win32api ## Windows-specific

FONT = 'thin9pt' #metrix7pt
HEIGHT = 20
Expand All @@ -24,54 +30,45 @@
(('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)
return input.wasPressedSinceLast(input.DOWN)

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

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

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

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

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

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

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

def answerYes():
clearState(ord('Y'))
clearState(ord('N'))
input.clearPressBuffer(ord('Y'))
input.clearPressBuffer(ord('N'))
while True:
if win32api.GetAsyncKeyState(ord('Y')) & 1:
if input.wasPressedSinceLast(ord('Y')):
return True
if win32api.GetAsyncKeyState(ord('N')) & 1:
if input.wasPressedSinceLast(ord('N')):
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,
for k in (input.DOWN, input.LEFT, input.RIGHT,
input.PRIOR, input.NEXT, input.UP,
ord('N'), ord('L'), ord('P'), ord('Y')):
clearState(k)
## End of Windows specific code ##
############################################################
input.clearPressBuffer(k)

def drawBoard():
mc.setBlocks(left-1, bottom-1, plane, left+WIDTH, bottom-1, plane, BORDER)
Expand Down
16 changes: 8 additions & 8 deletions python2-scripts/mcpipy/win32move.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from mc import *
from time import sleep
from sys import argv
import win32con,win32api
import input

mc = Minecraft()

Expand All @@ -25,23 +25,23 @@
pos = mc.entity.getPos(entity)
yaw = mc.entity.getRotation(entity)
move = False
if win32api.GetAsyncKeyState(win32con.VK_NEXT):
if input.wasPressedSinceLast(input.NEXT):
pos.y -= 1
move = True
if win32api.GetAsyncKeyState(win32con.VK_PRIOR):
if input.wasPressedSinceLast(input.PRIOR):
pos.y += 1
move = True
if win32api.GetAsyncKeyState(win32con.VK_LEFT):
if input.wasPressedSinceLast(input.LEFT):
yaw -= 15
mc.entity.setRotation(entity,yaw)
if win32api.GetAsyncKeyState(win32con.VK_RIGHT):
if input.wasPressedSinceLast(input.RIGHT):
yaw += 15
mc.entity.setRotation(entity,yaw)
if win32api.GetAsyncKeyState(win32con.VK_UP):
if input.wasPressedSinceLast(input.UP):
pos.x += .5 * -sin(radians(yaw))
pos.z += .5 * cos(radians(yaw))
move = True
if win32api.GetAsyncKeyState(win32con.VK_DOWN):
if input.wasPressedSinceLast(input.DOWN):
pos.x -= .5 * -sin(radians(yaw))
pos.z -= .5 * cos(radians(yaw))
move = True
Expand All @@ -55,7 +55,7 @@
else:
drew = None
mc.entity.setPos(entity,pos)
if lastPlatform != under:
if lastPlatform is not None and lastPlatform != under:
mc.setBlock(lastPlatform,AIR)
lastPlatform = None
if drew:
Expand Down
Binary file modified python3-scripts.zip
Binary file not shown.
Loading

0 comments on commit d50d22d

Please sign in to comment.