Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pywinauto.application import Application
- import pywinauto
- import serial
- def get_mcwindow(mc_title):
- mc_app = Application().connect(title=mc_title)
- return mc_app.window(title=mc_title)
- def capture_mc(mc_window, left, top, right, bottom):
- capture_rect = pywinauto.win32structures.RECT() # The capture method needs this certain structure
- capture_rect.left = left
- capture_rect.top = top
- capture_rect.right = right
- capture_rect.bottom = bottom
- return mc_window.capture_as_image(rect=capture_rect)
- def get_redpixels(mc_capture):
- red_pixels = 0
- for height in range(1, mc_capture.size[1]): # Iterate over each pixel column
- for width in range(1, mc_capture.size[0]): # Iterate over each pixel row
- cur_pixel = mc_capture.getpixel( (width, height) )
- if cur_pixel == (255, 19, 19): # Checking color, making the capture grayscale might make it faster
- red_pixels += 1
- return red_pixels
- def main():
- window = get_mcwindow('Minecraft 1.15.1') # Connect to minecraft window
- ser = serial.Serial('COM5', 9600) # Create serial port COM5 with 9600 baudrate
- last_pixels = 0 # The previous amount of red pixels
- # Monitor health loop
- while(True):
- capture = capture_mc(window, 161, 695, 319, 697) # Take a screenshot of the health bar
- red_pixels = get_redpixels(capture) # Count the amount of red pixels in the capture
- # Check if health was lost
- if red_pixels < last_pixels:
- ser.write(b'HEALTH_LOST\n')
- ser.flush() # Probably not necessary
- print('HEALTH LOST')
- last_pixels = red_pixels # Update the previous pixel amount for the next iteration
- print(red_pixels)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment