to keep high score by write to a File
Automatically using Python to repeat tasks that we don’t want to do in live
email to a Client by name list
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial,24,normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.score = 0
self.high_score = 0
self.color("white")
self.penup()
self.goto(0,270)
self.write(f"Score: {self.score}", align=ALIGNMENT, font=FONT)
self.hideturtle()
def increase_score(self):
self.score+= 1
self.update_scoreboard()
def update_scoreboard(self):
self.clear()
self.write(f"Score: {self.score} High Score:{self.high_score}", align=ALIGNMENT, font=FONT)
#TODO: day24 to keep high score
def reset(self):
if self.score > self.high_score:
self.high_score = self.score
self.score = 0
self.update_scoreboard()
def game_over(self):
self.goto(0,0)
self.write("Game over", align=ALIGNMENT, font=FONT)
from turtle import Turtle
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
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 STARTING_POSITIONS:
self.add_segment(position)
def add_segment(self, position):
new_segment = Turtle("square")
new_segment.color("white")
new_segment.penup()
new_segment.goto(position)
self.segments.append(new_segment)
#TODO: day24 improve high score record to add reset()
#when game over to clear snake body and create new one with move old snake body to out of boundary
def reset(self):
for seg in self.segments:
seg.goto(400,400)
self.segments.clear()
self.create_snake()
self.head = self.segments[0]
def extend(self):
#add a new segment to the snake
self.add_segment(self.segments[-1].position())
#移動貪食蛇身體
def move(self):
for seg_num in range(len(self.segments) - 1, 0, -1):
new_x = self.segments[seg_num - 1].xcor()
new_y = self.segments[seg_num - 1].ycor()
self.segments[seg_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)
from turtle import Screen
from snake import Snake
import time
from food import Food
from scoreboard import Scoreboard
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("My Snake Game")
screen.tracer(0)
snake = Snake()
food = Food()
scoreboard = Scoreboard()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
snake.move()
#Detech collision with food.
if snake.head.distance(food) < 15:
food.refresh()
#increase snake's body length
snake.extend()
#increase score on board
scoreboard.increase_score()
#Detect collision with wall
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
#game_is_on = False
#scoreboard.game_over()
#TODO: day24 improve high score record
scoreboard.reset()
snake.reset()
#Detect collision with tail.
for segment in snake.segments[1:]:
if snake.head.distance(segment) < 10:
#game_is_on = False
#scoreboard.game_over()
#TODO: day24 improve high score record
scoreboard.reset()
snake.reset()
screen.exitonclick()
#save game score to a File and read it in next time when we open up snake program
#working with file system as open and read
#working with file system as write something to a file by write with mode w or a