import turtle
import random

# Screen
screen = turtle.Screen()
screen.setup(width=600, height=400)
screen.bgcolor("black")
screen.tracer(0)

# Starry background
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()
for _ in range(100):
  pen.goto(random.randint(-300, 300), random.randint(-200, 200))
  pen.color(random.choice(["white", "white", "yellow", "pink", "lightblue"]))
  pen.dot(random.choice([1, 2, 2, 3, 4, 5]))

# Ship shape
# Each pair of numbers is a point: (across, up) from the centre of the ship.
# Positive numbers go right and up; negative go left and down.
screen.addshape("ship", ((0, 25), (-18, -15), (-8, -8), (8, -8), (18, -15)))

# Player
player = turtle.Turtle()
player.shape("ship")
player.color("cyan")
player.penup()
player.goto(0, -170)
player.setheading(90)

# Movement
def move_left():
    if player.xcor() > -265:
        player.setx(player.xcor() - 15)

def move_right():
    if player.xcor() < 265:
        player.setx(player.xcor() + 15)

screen.listen()
screen.onkey(move_left, "Left")
screen.onkey(move_right, "Right")

# Game loop
def game_loop():
    screen.update()
    screen.ontimer(game_loop, 33)

game_loop()
screen.mainloop()

