Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Turtle Graphics Game
- import turtle
- import math
- import random
- #Set up screen
- wn = turtle.Screen()
- wn.bgcolor("Black")
- wn.tracer(3)
- #Draw border
- mypen = turtle.Turtle()
- mypen.penup()
- mypen.setposition(-300,-300)
- mypen.pendown()
- mypen.pensize(3)
- for side in range(4):
- mypen.forward(600)
- mypen.left(90)
- mypen.hideturtle()
- #Create player turtle
- player = turtle.Turtle()
- player.color("White")
- player.shape("arrow")
- player.penup()
- player.speed(0)
- #Create goals
- maxGoals = 6
- goals = []
- for count in range(maxGoals):
- goals.append(turtle.Turtle())
- goals[count].color("red")
- goals[count].shape("circle")
- goals[count].penup()
- goals[count].speed(0)
- goals[count].setposition(random.randint(-300, 300), random.randint(-300, 300))
- #Set speed variable
- speed = 1
- #Define functions
- def turnleft():
- player.left(30)
- def turnright():
- player.right(30)
- def increasespeed():
- global speed
- speed += 1
- def isCollision(t1, t2):
- d = math.sqrt(math.pow(t1.xcor()-t2.xcor(), 2) + math.pow(t1.ycor()-t2.ycor(),2))
- if d < 20:
- return True
- else:
- return False
- #Set keyboard bindings
- turtle.listen()
- turtle.onkey(turnleft, "Left")
- turtle.onkey(turnright, "Right")
- turtle.onkey(increasespeed, "Up")
- while True:
- player.forward(speed)
- #Boundary Checking
- if player.xcor() > 300 or player.xcor() < - 300:
- player.right(180)
- #Boundary Checking
- if player.ycor() > 300 or player.ycor() < - 300:
- player.right(180)
- #love the goal
- for count in range(maxGoals):
- goals[count].forward(3)
- # Boundary Checking
- if goals[count].xcor() > 300 or goals[count].xcor() < - 300:
- goals[count].right(180)
- # Boundary Checking
- if goals[count].ycor() > 300 or goals[count].ycor() < - 300:
- goals[count].right(180)
- # Collision checking
- if isCollision(player, goals[count]):
- goals[count].setposition(random.randint(-300, 300), random.randint(-300, 300))
- goals[count].right(random.randint(0,360))
- delay = raw_input("Press Enter to Finish")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement