11# Juego Snake
22# @autor: Magno Efren
33# Youtube: https://www.youtube.com/c/MagnoEfren
4-
54#print('🟢❤🍎🌍💛💚🧡🧡❤💥⚪🟥🟧🟨🟩🟦🟫🟪🔶🔸🔷🔹🔺🔻')
65
76from tkinter import Tk , Frame , Canvas , Button ,Label , IntVar , ALL
1514posicion_food = (15 ,15 )
1615posicion_snake = [(75 ,75 )]
1716nueva_posicion = [(15 ,15 )]
18-
1917mixer .init ()
2018
21-
2219def coordenadas_snake ():
2320 global direccion , posicion_snake ,x ,y ,nueva_posicion
24-
2521 if direction == 'up' : # arriba
2622 y = y - 30
2723 nueva_posicion [0 :] = [(x , y )]
@@ -50,15 +46,12 @@ def coordenadas_snake():
5046 x = 15
5147 elif x <= 0 :
5248 x = 15
53-
5449 posicion_snake = nueva_posicion + posicion_snake [:- 1 ]
55-
5650 for parte , lugar in zip (canvas .find_withtag ("snake" ), posicion_snake ):
5751 canvas .coords (parte , lugar )
5852
5953def direccion (event ):
6054 global direction
61-
6255 if event == 'left' :
6356 if direction != 'right' :
6457 direction = event
@@ -71,31 +64,22 @@ def direccion(event):
7164 elif event == 'down' :
7265 if direction != 'up' :
7366 direction = event
74-
7567def movimiento ():
7668 global posicion_food , posicion_snake ,nueva_posicion
7769 posiciones = [15 , 45 , 75 ,105 ,135 ,165 , 195 , 225 , 255 ,
7870 285 , 315 , 345 , 375 , 405 , 435 , 465 ]
79-
8071 coordenadas_snake ()
81-
8272 if posicion_food == posicion_snake [0 ]:
8373 n = len (posicion_snake )
84-
8574 cantidad ['text' ] = 'Cantidad 🍎 : {}' .format (n )
86-
8775 posicion_food = (random .choice (posiciones ), random .choice (posiciones ))
8876 posicion_snake .append (posicion_snake [- 1 ])
89-
9077 mixer .music .load ("audio_snake.mp3" )
9178 mixer .music .play (loops = 0 )
92-
9379 if posicion_food not in posicion_snake :
9480 canvas .coords (canvas .find_withtag ("food" ), posicion_food )
95-
9681 canvas .create_text (* posicion_snake [- 1 ], text = '▀' , fill = 'green2' ,
9782 font = ('Arial' ,20 ), tag = 'snake' )
98-
9983 if posicion_snake [- 1 ] == nueva_posicion [0 ] and len (posicion_snake )>= 4 :
10084 cruzar_snake ()
10185
@@ -105,7 +89,6 @@ def movimiento():
10589
10690 cantidad .after (300 , movimiento )
10791
108-
10992def cruzar_snake ():
11093 canvas .delete (ALL )
11194 canvas .create_text (canvas .winfo_width () / 2 , canvas .winfo_height () / 2 ,
@@ -122,7 +105,6 @@ def salir ():
122105 ventana .destroy ()
123106 ventana .quit ()
124107
125-
126108ventana = Tk ()
127109ventana .config (bg = 'black' )
128110ventana .title ('Juego Snake' )
@@ -139,15 +121,13 @@ def salir ():
139121ventana .bind ("<KeyPress-Down>" , lambda event :direccion ('down' ))
140122ventana .bind ("<KeyPress-Left>" , lambda event :direccion ('left' ))
141123ventana .bind ("<KeyPress-Right>" , lambda event :direccion ('right' ))
142-
143124canvas = Canvas (frame_2 , bg = 'black' , width = 479 , height = 479 )
144125canvas .pack ()
145126
146127for i in range (0 ,460 ,30 ):
147128 for j in range (0 ,460 ,30 ):
148129 canvas .create_rectangle (i ,j ,i + 30 , j + 30 , fill = 'gray10' )
149130
150-
151131canvas .create_text (75 ,75 , text = '🍎' , fill = 'red2' ,
152132 font = ('Arial' ,18 ), tag = 'food' )
153133
0 commit comments