Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Código para crear Snake en Python con el módulo Turtle

MAIN (main.py)

from turtle import Screen

from snake import Snake

from food import Food

from score import Score

import time

screen = Screen()

screen.setup(600, 600)

screen.title("Snake")

screen.colormode(255)

screen.bgcolor(204, 255, 204)

screen.tracer(0)

game_mode = screen.textinput("Game Mode", "Press 'y' to play with borders")

snake = Snake()

screen.listen()

screen.onkey(snake.up, "Up")

screen.onkey(snake.down, "Down")

screen.onkey(snake.left, "Left")

screen.onkey(snake.right, "Right")

score = Score()
food = Food()

game_is_on = True

while game_is_on:

time.sleep(0.1)

screen.update()

snake.move()

if snake.head.distance(food) < 15:

snake.add_segments()

food.refresh()

score.increase_score()

if game_mode == 'y':

if snake.head.xcor() > 280 or snake.head.ycor() > 280:

game_is_on = False

score.high_s()

if snake.head.xcor() < -280 or snake.head.ycor() < -280:

game_is_on = False

score.high_s()

else:

if snake.head.xcor() > 280:

snake.head.goto(-280, snake.head.ycor())

elif snake.head.ycor() > 280:

snake.head.goto(snake.head.xcor(), -280)

elif snake.head.xcor() < -280:

snake.head.goto(280, snake.head.ycor())

elif snake.head.ycor() < -280:


snake.head.goto(snake.head.xcor(), 280)

for segment in snake.segments[1::]:

if snake.head.distance(segment) < 10:

score.high_s()

game_is_on = False

screen.exitonclick()

FOOD (food.py)

from turtle import Turtle

import random

class Food(Turtle):

def __init__(self):

super().__init__()

self.shape("circle")

self.penup()

self.color(255, 128, 0)

self.shapesize(0.5)

self.refresh()

def refresh(self):

pos_x = random.randint(-270, 270)

pos_y = random.randint(-270, 270)

self.goto(pos_x, pos_y)
SCORE (score.py)

from turtle import Turtle

ALIGNMENT = "center"

FONT = ("Roboto", 18, "normal")

class Score(Turtle):

def __init__(self):

super().__init__()

self.score = 0

with open("data.txt", "r") as data:

self.high_score = int(data.read())

self.hideturtle()

self.penup()

self.goto(0, 270)

self.update_score()

def update_score(self):

self.clear()

self.write(f"Score: {self.score} - High Score: {self.high_score}", font=FONT, align=ALIGNMENT)

def increase_score(self):

self.score += 1

self.clear()

self.update_score()

def high_s(self):

if self.score > self.high_score:

self.high_score = self.score
with open("data.txt", "w") as hs:

hs.write(str(self.score))

SNAKE (snake.py)

from turtle import Turtle

INITIAL_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]

UP = 90

DOWN = 270

LEFT = 180

RIGHT = 0

class Snake:

def __init__(self):

self.segments = []

self.create_snake()

self.head = self.segments[0]

def create_snake(self):

for position in INITIAL_POSITIONS:

segment = Turtle("turtle")

segment.penup()

segment.goto(position)

segment.color(153, 0, 153)

self.segments.append(segment)

self.segments[0].color(0, 0, 0)

def add_segments(self):
segment = Turtle("turtle")

segment.penup()

segment.goto(self.segments[1].xcor(), self.segments[1].ycor())

segment.color(153, 0, 153)

self.segments.append(segment)

def move(self):

for segment in range(len(self.segments) - 1, 0, -1):

x_cor = self.segments[segment - 1].xcor()

y_cor = self.segments[segment - 1].ycor()

self.segments[segment].goto(x_cor, y_cor)

self.head.forward(20)

def up(self):

if self.head.heading() != DOWN:

self.head.setheading(UP)

def down(self):

if self.head.heading() != UP:

self.head.setheading(DOWN)

def left(self):

if self.head.heading() != RIGHT:

self.head.setheading(LEFT)

def right(self):

if self.head.heading() != LEFT:

self.head.setheading(RIGHT)

You might also like