1
+ import tkinter as tk
2
+ from tkinter import PhotoImage , Canvas
3
+ import ctypes
4
+ import pyautogui
5
+ import random
6
+
7
+ class Banana :
8
+ def __init__ (self , scene , x = 0 , y = 0 ):
9
+ self .scene = scene
10
+ self .image = PhotoImage (file = 'banana1.png' )
11
+ self .image = self .image .subsample (16 )
12
+ self .image_bomb = PhotoImage (file = 'bomb.png' )
13
+ self .image_bomb = self .image_bomb .subsample (8 )
14
+ self .imageRef = scene .canvas .create_image (x ,y ,image = self .image )
15
+ self .bomb_status = False
16
+
17
+ def update (self ):
18
+ x , y = pyautogui .position ()
19
+ ban_x , ban_y = self .scene .canvas .coords (self .imageRef )
20
+ dist = (abs (x - ban_x )+ abs (y - ban_y ))
21
+ if self .bomb_status :
22
+ self .scene .canvas .move (
23
+ self .imageRef ,
24
+ random .choice ((- 30 , 30 )),
25
+ random .choice ((- 30 , 30 ))
26
+ )
27
+ self .scene .canvas .itemconfig (self .imageRef , image = self .image )
28
+ for _ in range (10 ):
29
+ self .scene .new_banana (
30
+ random .randint (0 , self .scene .screen_width ),
31
+ random .randint (0 , self .scene .screen_height ),
32
+ )
33
+ self .bomb_status = False
34
+ elif dist < 5 :
35
+ self .scene .canvas .itemconfig (self .imageRef , image = self .image_bomb )
36
+ self .bomb_status = True
37
+ else :
38
+ numero = random .choice ((1 ,2 ,5 ))
39
+ self .scene .canvas .move (
40
+ self .imageRef ,
41
+ numero if x > ban_x else - numero ,
42
+ numero if y > ban_y else - numero
43
+ )
44
+
45
+ class Scene :
46
+ def __init__ (self , window : tk .Tk ):
47
+ self .screen_width = window .winfo_screenwidth ()
48
+ self .screen_height = window .winfo_screenheight ()
49
+ self .canvas = Canvas (
50
+ window ,
51
+ width = self .screen_width ,
52
+ height = self .screen_height ,
53
+ highlightthickness = 0 ,
54
+ bg = 'white'
55
+ )
56
+ self .canvas .pack ()
57
+ self .bananas = list ()
58
+
59
+ def update (self ):
60
+ for banana in self .bananas :
61
+ banana .update ()
62
+
63
+ def new_banana (self , x , y ):
64
+ banana = Banana (self )
65
+ self .canvas .move (banana .imageRef , x , y )
66
+ self .bananas .append (banana )
67
+
68
+ class Game :
69
+ def __init__ (self ):
70
+ self .window = self .create_window ()
71
+ self .apply_click_through (self .window )
72
+ self .scene = Scene (self .window )
73
+
74
+ def update (self ):
75
+ self .scene .update ()
76
+ self .window .after (5 , self .update )
77
+
78
+ def create_window (self ):
79
+ window = tk .Tk ()
80
+ window .wm_attributes ("-topmost" , True )
81
+ window .attributes ("-fullscreen" , True )
82
+ window .overrideredirect (True )
83
+ # Transparencia
84
+ window .attributes ('-transparentcolor' , 'white' )
85
+ window .config (bg = 'white' )
86
+ return window
87
+
88
+ def apply_click_through (self , window ):
89
+ # Constantes API windows
90
+ WS_EX_TRANSPARENT = 0x00000020
91
+ WS_EX_LAYERED = 0x00080000
92
+ GWL_EXSTYLE = - 20
93
+
94
+ # Obtener el identificador de ventana (HWND)
95
+ hwnd = ctypes .windll .user32 .GetParent (window .winfo_id ())
96
+ # Obtener los estilos actuales de la ventana
97
+ style = ctypes .windll .user32 .GetWindowLongW (hwnd , GWL_EXSTYLE )
98
+ # Establecer nuevo estilo
99
+ style = style | WS_EX_TRANSPARENT | WS_EX_LAYERED
100
+ ctypes .windll .user32 .SetWindowLongW (hwnd , GWL_EXSTYLE , style )
101
+
102
+ def start (self ):
103
+ self .update ()
104
+ self .window .mainloop ()
105
+
106
+ game = Game ()
107
+ game .scene .new_banana (100 ,100 )
108
+ game .start ()
0 commit comments