Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Star Dust
- import turtle
- import os
- import math
- #Set up the screen
- wn = turtle.Screen()
- wn.tracer(0)
- wn.bgcolor("black") #space
- wn.title("Star Dust")
- #Draw border
- border_pen = turtle.Turtle()
- border_pen.speed(0)
- border_pen.color("white") #border color
- border_pen.penup()
- border_pen.setposition(-300, -300) #cor. system location
- border_pen.pendown()
- border_pen.pensize(3) #border size
- for side in range(4):
- border_pen.fd(600) #forward 600 degrees
- border_pen.lt(90) #left 90 degrees
- border_pen.hideturtle()
- #Create the player turtle
- player = turtle.Turtle()
- player.color("silver") #turtle color
- player.shape("triangle") #turtle shape
- player.penup()
- player.speed(0) #max speed
- player.setposition(0, -250) #1st position
- player.setheading(90) #90 = up
- playerspeed = 15 #best speed option
- #Create the enemy
- enemy = turtle.Turtle()
- enemy.color("red")
- enemy.shape("circle")
- enemy.penup() #no drawing on screen
- enemy.speed(0)
- enemy.setposition(-200, 250) #starting pos. not a necessity
- enemyspeed = 0.35
- #Create the turtle's bullet
- bullet = turtle.Turtle()
- bullet.color("yellow")
- bullet.shape("square")
- bullet.penup()
- bullet.speed(0)
- bullet.setheading(90) #bullet's heading
- bullet.shapesize(0.25, 0.25) #dimensions of the bullet
- bullet.hideturtle() #hides bullet when game is started
- bulletspeed = 1.5
- #Define bullet state(s)
- #ready - ready to fire
- #fire - bullet is firing
- bulletstate = "ready" #bullet state when game is started
- #Move the player turtle left and right
- def move_left(): #left function
- x = player.xcor() #x = turtle x
- x -= playerspeed #negative movement
- if x < - 285:
- x = - 285
- player.setx(x) #sets x to x(turtle)
- def move_right(): #right function
- x = player.xcor()
- x += playerspeed #positive movement
- if x > 285:
- x = 285
- player.setx(x)
- def fire_bullet():
- #Declare bullet state as a global
- global bulletstate
- if bulletstate == "ready":
- bulletstate = "fire"
- #Move the bullet just above the player upon start
- x = player.xcor()
- y = player.ycor() +10 #10 pixels above turtle
- bullet.setposition(x, y)
- bullet.showturtle()
- def isCollision(t1, t2):
- distance = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2) + math.pow(t1.ycor()- t2.ycor(), 2))
- if distance < 15: #15 or any
- return True
- else:
- return False
- #Create keyboard bindings
- turtle.listen() #listen for keyword actions
- turtle.onkey(move_left, "Left")
- turtle.onkey(move_right, "Right")
- turtle.onkey(fire_bullet, "space")
- #Main game loop
- while True:
- wn.update()
- #Move the enemy
- x = enemy.xcor() #cor. position of enemy(starting)
- x += enemyspeed #enemy moves alone with this speed
- enemy.setx(x) #set enemy as that x
- #Move the enemy back and down
- if enemy.xcor() > 285:
- y = enemy.ycor()
- y -= 70 #when enemy hits boundary, drop down 40px
- enemyspeed *=-1 #when touching boundary, turn negative to reverse
- enemy.sety(y) #set y pos. to y(enemy)
- if enemy.xcor() < - 285:
- y = enemy.ycor()
- y -= 70
- enemyspeed *= -1
- enemy.sety(y)
- #Move the bullet
- y = bullet.ycor()
- y += bulletspeed
- bullet.sety(y)
- if bulletstate == "fire":
- y = bullet.ycor()
- y += bulletspeed
- bullet.sety(y)
- #Check to see if bullet hit boundary
- if bullet.ycor() > 282.5:
- bullet.hideturtle() #hides bullet if it crosses boundary
- bullet.sety(-240)
- bulletstate = "ready" #when it does, it switches to 'ready' state
- if isCollision(bullet, enemy):
- #Reset the bullet
- bullet.hideturtle() #if hit enemy, hide bullet
- bulletstate = "ready" #when enemy hit, ready another bullet
- bullet.setposition(-600, -600) #move bullet offscreen(bc. hidden bullet still impacts)
- #Reset the enemy
- enemy.setposition(-200, 250) #when killed, spawn another on this cor.
- if isCollision(player, enemy):
- player.hideturtle()
- enemy.hideturtle()
- print("GAME OVER")
- break #breaks the main loop(ends the game)
- turtle.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement