[go: up one dir, main page]

0% found this document useful (0 votes)
29 views2 pages

La Culebrita

This document contains code for a Snake game written in Python using the curses library. It initializes the screen and window settings, sets up the initial snake position and food location. The main game loop continuously checks the key input to move the snake's head, checks if it has eaten the food to grow longer, otherwise moves the rest of the body, and checks for collisions with walls or itself to end the game.

Uploaded by

emanuel17t06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

La Culebrita

This document contains code for a Snake game written in Python using the curses library. It initializes the screen and window settings, sets up the initial snake position and food location. The main game loop continuously checks the key input to move the snake's head, checks if it has eaten the food to grow longer, otherwise moves the rest of the body, and checks for collisions with walls or itself to end the game.

Uploaded by

emanuel17t06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import curses

from random import randint

# Configuración inicial
c = curses.initscr()
w = curses.newwin(20, 60, 0, 0)
w.keypad(1)
curses.noecho()
curses.curs_set(0)
w.border(0)
w.timeout(100)

# Inicialización de la serpiente
key = curses.KEY_RIGHT
snake = [(4, 10), (4, 9), (4, 8)]
food = (10, 20)

# Bucle principal del juego


while key != 27: # 27 es la tecla ESC
w.addch(food[0], food[1], '*')
w.addch(snake[0][0], snake[0][1], 'O')

# Captura la entrada del usuario


key = w.getch()

# Mueve la serpiente
new_head = [snake[0][0], snake[0][1]]

if key == curses.KEY_DOWN:
new_head[0] += 1
if key == curses.KEY_UP:
new_head[0] -= 1
if key == curses.KEY_LEFT:
new_head[1] -= 1
if key == curses.KEY_RIGHT:
new_head[1] += 1

snake.insert(0, new_head)

# Verifica si la serpiente ha alcanzado la comida


if snake[0] == food:
food = None
while food is None:
nf = [
randint(1, 18),
randint(1, 58)
]
food = nf if nf not in snake else None
w.addch(food[0], food[1], '*')
else:
# Si no ha comido, mueve la cola de la serpiente
tail = snake.pop()
w.addch(tail[0], tail[1], ' ')

# Verifica colisiones con las paredes o la propia serpiente


if (
snake[0][0] in [0, 19] or
snake[0][1] in [0, 59] or
snake[0] in snake[1:]
):
curses.endwin()
quit()

curses.endwin()

You might also like