Guest User

Untitled

a guest
May 5th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | None | 0 0
  1. import pyautogui
  2. import time
  3. import random
  4. import keyboard
  5. import pygetwindow as gw
  6. import win32gui
  7. import os
  8. import win32con
  9.  
  10. pyautogui.hotkey('win', 'r')
  11. pyautogui.write('mspaint')
  12. pyautogui.press('enter')
  13. time.sleep(2)
  14.  
  15. mspaint_window = gw.getWindowsWithTitle('Sans titre - Paint')[0]
  16.  
  17. win32gui.SetForegroundWindow(mspaint_window._hWnd)
  18. time.sleep(0.1)
  19.  
  20. win32gui.ShowWindow(mspaint_window._hWnd, win32con.SW_MAXIMIZE)
  21.  
  22. pyautogui.hotkey('altleft', 'f')
  23. pyautogui.press('s')
  24. pyautogui.write('805')
  25. pyautogui.press('tab')
  26. pyautogui.write('607')
  27. pyautogui.press('enter')
  28.  
  29. pyautogui.moveTo(323,66)
  30. time.sleep(0.05)
  31. pyautogui.click()
  32. pyautogui.moveTo(748,97)
  33. time.sleep(0.05)
  34. pyautogui.click()
  35. time.sleep(0.05)
  36. pyautogui.moveTo(748,255)
  37. time.sleep(0.05)
  38. pyautogui.click()
  39.  
  40. cell_size = 20
  41. score = 0
  42. pyautogui.PAUSE = 0.035
  43. pause_change = 0.001
  44.  
  45.  
  46. def get_canvas_position():
  47.     paint_win = win32gui.FindWindow(None, 'Sans titre - Paint')
  48.     rect = win32gui.GetWindowRect(paint_win)
  49.     x, y, w, h = rect
  50.    
  51.     x_offset = 15
  52.     y_offset = 155
  53.     width_offset = 20
  54.     height_offset = 150
  55.    
  56.     x += x_offset
  57.     y += y_offset
  58.     w -= width_offset
  59.     h -= height_offset
  60.    
  61.     canvas_width = 800
  62.     canvas_height = 600
  63.     board_width = canvas_width // cell_size
  64.     board_height = canvas_height // cell_size
  65.    
  66.     return x, y, canvas_width, canvas_height, board_width, board_height
  67.  
  68. def draw_game_board(x, y, width, height, cell_size):
  69.     pyautogui.click(x, y)
  70.     pyautogui.dragTo(x + width, y, 0.1, button='left')
  71.     pyautogui.dragTo(x + width, y + height, 0.1, button='left')
  72.     pyautogui.dragTo(x, y + height, 0.1, button='left')
  73.     pyautogui.dragTo(x, y, 0.1, button='left')
  74.  
  75. canvas_x, canvas_y, canvas_width, canvas_height, board_width, board_height = get_canvas_position()
  76. draw_game_board(canvas_x, canvas_y, canvas_width, canvas_height, cell_size)
  77.  
  78. player_x = board_width // 2
  79. player_y = board_height // 2
  80. player_dx = 1
  81. player_dy = 0
  82.  
  83. trail = [(player_x, player_y)]
  84.  
  85. pellet_x = random.randint(1, board_width-2)
  86. pellet_y = random.randint(1, board_height-2)
  87.  
  88. score = 0
  89. length = 4
  90. game_over = False
  91.  
  92. last_key_press_time = 0
  93. key_press_interval = 0.1
  94.  
  95. def on_key_press(event):
  96.     global player_dx, player_dy, last_key_press_time
  97.  
  98.     current_time = time.time()
  99.     if current_time - last_key_press_time < key_press_interval:
  100.         return  
  101.  
  102.     last_key_press_time = current_time
  103.     key_name = event.name
  104.  
  105.     if key_name == 'z' and player_dy != 1:
  106.         player_dx = 0
  107.         player_dy = -1
  108.     elif key_name == 's' and player_dy != -1:
  109.         player_dx = 0
  110.         player_dy = 1
  111.     elif key_name == 'q' and player_dx != 1:
  112.         player_dx = -1
  113.         player_dy = 0
  114.     elif key_name == 'd' and player_dx != -1:
  115.         player_dx = 1
  116.         player_dy = 0
  117.  
  118. keyboard.on_press_key("z", on_key_press)
  119. keyboard.on_press_key("s", on_key_press)
  120. keyboard.on_press_key("q", on_key_press)
  121. keyboard.on_press_key("d", on_key_press)
  122.  
  123. def on_escape_press(event):
  124.     global game_over
  125.     game_over = True
  126.  
  127. keyboard.on_press_key("escape", on_escape_press)
  128.  
  129. while not game_over:
  130.     player_x += player_dx
  131.     player_y += player_dy
  132.  
  133.     if (player_x, player_y) in trail[:-1]:
  134.         game_over = True
  135.  
  136.     if player_x <= 0 or player_x >= board_width - 0.8 or player_y <= 0 or player_y >= board_height - 0.8:
  137.         game_over = True
  138.  
  139.     trail.append((player_x, player_y))
  140.  
  141.     if len(trail) > length:
  142.         last_x, last_y = trail.pop(0)
  143.         last_px = last_x * cell_size + canvas_x
  144.         last_py = last_y * cell_size + canvas_y
  145.         pyautogui.rightClick(last_px, last_py)
  146.  
  147.     if player_x == pellet_x and player_y == pellet_y:
  148.         while (pellet_x, pellet_y) in trail or pellet_x < 0 or pellet_x >= board_width or pellet_y < 0 or pellet_y >= board_height:
  149.             pellet_x = random.randint(1, board_width-2)
  150.             pellet_y = random.randint(1, board_height-2)
  151.             score += 1
  152.             length += 1
  153.             if pyautogui.PAUSE > 0.001:
  154.                 pyautogui.PAUSE -= pause_change
  155.  
  156.     head_x, head_y = trail[-1]
  157.     head_px = head_x * cell_size + canvas_x
  158.     head_py = head_y * cell_size + canvas_y
  159.     pyautogui.click(head_px, head_py)
  160.  
  161.     pellet_px = pellet_x * cell_size + canvas_x
  162.     pellet_py = pellet_y * cell_size + canvas_y
  163.     pyautogui.click(pellet_px, pellet_py)
  164.  
  165. keyboard.unhook_all()
  166. pyautogui.alert('Game over! Score: ' + str(score))
  167. os.system('taskkill /f /im mspaint.exe')
Advertisement
Add Comment
Please, Sign In to add comment