Skip to content
Menu
Daniel J. Pierce
  • C#
  • Games
  • Graphics
  • Math
  • Python
  • Tynker
  • Unity
Daniel J. Pierce

Escape asteroids

Posted on September 1, 2019November 10, 2019

Restart Game

Turn the space ship using left and right arrows.

Move forwards with the up arrows.

Made in tynker.com, the Python 1 course, not cross-platform compatible but source code is below

import turtle, random
screen = turtle.Screen()
screen.register_shape("ship", ((-10, -10), (0, -5), (10, -10), (0, 10)))
screen.bgcolor('black') 
for i in range(10):
    screen.register_shape("rock" + str(i), ((random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20))))
'''screen.register_shape("rock1", ((random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20))))
screen.register_shape("rock2", ((random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20))))
screen.register_shape("rock3", ((random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20)), (random.randint(-20, 20), random.randint(-20, 20))))'''

ship = turtle.Turtle()
ship.shape("ship")
ship.penup()
ship.speed(0) 
ship.color("green")
asteroids = []
for i in range(10):
    a = turtle.Turtle()
    a.penup()
    a.speed(0)
    a.shape('rock' + str(i))
    a.color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    a.goto(random.randint(-200, 200), random.randint(-200, 200))
    asteroids.append(a)
    #make each asteroid its own shape and color  
def right(): 
	ship.right(10)
def left():
    ship.left(10) 
def forward(): 
    ship.forward(5)
    x = ship.position()
    q = 0
    for i in range(10): 
        if abs(int(x[0]) - int(asteroids[i].position()[0])) <= 20:
            if abs(int(x[1]) - int(asteroids[i].position()[1])) <= 20:
                q += 1
    if q > 0:
        ship.ht()
    else:
        ship.st()   
def bullet(): 
    bullet = turtle.Turtle() 
    bullet.color("white")
    x = ship.position() 
    bullet.penup()
    bullet.speed(0)
    bullet.goto(x[0], x[1])
    bullet.color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    bullet.left(ship.heading() - 5) 
    for p in range(400):
        bullet.forward(1)
        q = 0 
        x = bullet.position()
        for i in range(len(asteroids)):
            if abs(int(x[0]) - int(asteroids[i].position()[0])) <= 20:
                if abs(int(x[1]) - int(asteroids[i].position()[1])) <= 20:
                    q += 1
            if q > 0:
                asteroids[i].ht()
                bullet.ht()
                del asteroids[i]
                q = 0  
        
screen.onkey(right, "Right")
screen.onkey(left, "Left")
screen.onkey(forward, "Up")
screen.onkey(bullet, "Space")
screen.listen()

Projects

  • Procedure Platforming
  • Particle Simulator
  • AP Computer Science Paint Program
  • Maybe Good Platformer
  • Custom Gravity Test
  • Triangle Types
  • Unity Obstacle Course v1
  • Collapsing rows
  • Java Tic Tac Toe (No AI)
  • Mersenne Prime Tester
  • Rainbow
  • Pong
  • Simple Tkinter Calculator
  • Hexidecimal Converter
  • Snake eats apple
  • Protect the Galactic Empire
  • Hangman
  • Beautiful Colorful Thing
  • Four Dots
  • Frogga
  • Escape asteroids
  • Portals
  • Guess the Number 2.0
  • Illogical
  • Tic Tac Toe
  • Guess the Number
  • Ultimate Calculator 1.0.0a0
  • Prime Number Tester
  • Number Base Converter 2.0a0
  • Number Base Converter 1.1
  • Python Equation Calculator
©2025 Daniel J. Pierce | WordPress Theme by Superb WordPress Themes