Untitled
unknown
python
a year ago
3.7 kB
5
Indexable
## main.py from turtle import Screen from snake import Snake from food import Food from scoreboard import Scoreboard import time screen = Screen() screen.setup(600,600) screen.title("Snake Game 🐍") screen.bgcolor("black") screen.tracer(0) # Turning the animation off snake = Snake() food = Food() scoreboard = Scoreboard() screen.listen() screen.onkeypress(fun=snake.up,key="Up") screen.onkeypress(fun=snake.down,key="Down") screen.onkeypress(fun=snake.left,key="Left") screen.onkeypress(fun=snake.right,key="Right") is_game_on = True score = 0 while is_game_on: screen.update() time.sleep(0.1) snake.move() # Detect collision with food if snake.head.distance(food) < 15: food.refresh() scoreboard.score_update() snake.extend() # Detect collision with wall if snake.head.xcor() > 299 or snake.head.xcor() < -299 or\ snake.head.ycor() > 299 or snake.head.ycor()<-299: scoreboard.game_over() is_game_on = False screen.exitonclick() ## food.py import random from turtle import Turtle class Food(Turtle): def __init__(self) -> None: super().__init__() self.shape("circle") self.penup() self.shapesize(stretch_len=0.5,stretch_wid=0.5) self.color("red") self.speed("fastest") self.refresh() def refresh(self): random_coordinates = ( random.randint(-280,280), random.randint(-280,280) ) self.goto(random_coordinates) # scoreboard.py from turtle import Turtle ALIGNMENT = "center" FONT_NAME = "Courier" FONT_SIZE = 17 FONT_TYPE = "normal" class Scoreboard(Turtle): score = 0 def __init__(self): super().__init__() self.color("yellow") self.hideturtle() self.penup() self.teleport(0,275) self.write(arg="score = 0",move=False,align=ALIGNMENT,font=(FONT_NAME, FONT_SIZE, FONT_TYPE)) def score_update(self,): self.score += 1 self.clear() self.write(arg=f"score = {self.score}",move=False,align=ALIGNMENT,font=(FONT_NAME, FONT_SIZE, FONT_TYPE)) def game_over(self): self.home() self.write(arg=f"GAME OVER",move=False,align=ALIGNMENT,font=(FONT_NAME, FONT_SIZE, FONT_TYPE)) # snake.py from turtle import Turtle, Screen MOVE_DISTANCE = 20 UP = 90 DOWN = 270 LEFT = 180 RIGHT = 0 class Snake: def __init__(self) -> None: self.segments = [] self.create_snake() self.head = self.segments[0] def create_snake(self): self.x_cor = 0 for i in range(3): self.add_segment(self.x_cor) self.x_cor -= 20 def add_segment(self,position): segment = Turtle("square") segment.color("white") segment.penup() segment.speed("slowest") segment.teleport(x=position) self.segments.append(segment) def extend(self): self.add_segment(self.segments[-1].pos()) def move(self): for seq_num in range(len(self.segments) -1, 0 ,-1): new_x = self.segments[seq_num - 1].xcor() new_y = self.segments[seq_num - 1].ycor() self.segments[seq_num].goto(new_x, new_y) self.head.forward(MOVE_DISTANCE) 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)
Editor is loading...
Leave a Comment