Guest User

Untitled

a guest
Jan 11th, 2020
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. from pywinauto.application import Application
  2. import pywinauto
  3. import serial
  4.  
  5. def get_mcwindow(mc_title):
  6.     mc_app = Application().connect(title=mc_title)
  7.     return mc_app.window(title=mc_title)
  8.  
  9. def capture_mc(mc_window, left, top, right, bottom):
  10.     capture_rect = pywinauto.win32structures.RECT() # The capture method needs this certain structure
  11.     capture_rect.left = left
  12.     capture_rect.top = top
  13.     capture_rect.right = right
  14.     capture_rect.bottom = bottom
  15.  
  16.     return mc_window.capture_as_image(rect=capture_rect)
  17.  
  18. def get_redpixels(mc_capture):
  19.     red_pixels = 0
  20.     for height in range(1, mc_capture.size[1]): # Iterate over each pixel column
  21.         for width in range(1, mc_capture.size[0]): # Iterate over each pixel row
  22.             cur_pixel = mc_capture.getpixel( (width, height) )
  23.             if cur_pixel == (255, 19, 19): # Checking color, making the capture grayscale might make it faster
  24.                 red_pixels += 1
  25.    
  26.     return red_pixels
  27.  
  28. def main():
  29.     window = get_mcwindow('Minecraft 1.15.1') # Connect to minecraft window
  30.     ser = serial.Serial('COM5', 9600) # Create serial port COM5 with 9600 baudrate
  31.  
  32.     last_pixels = 0 # The previous amount of red pixels
  33.    
  34.     # Monitor health loop
  35.     while(True):
  36.         capture = capture_mc(window, 161, 695, 319, 697) # Take a screenshot of the health bar
  37.         red_pixels = get_redpixels(capture) # Count the amount of red pixels in the capture
  38.  
  39.         # Check if health was lost
  40.         if red_pixels < last_pixels:
  41.             ser.write(b'HEALTH_LOST\n')
  42.             ser.flush() # Probably not necessary
  43.             print('HEALTH LOST')
  44.        
  45.         last_pixels = red_pixels # Update the previous pixel amount for the next iteration
  46.  
  47.         print(red_pixels)
  48.  
  49. if __name__ == "__main__":
  50.     main()
Advertisement
Add Comment
Please, Sign In to add comment