SHARE
TWEET

Enemy moving trough border

Vilkkupvpz May 24th, 2019 (edited) 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import turtle
  3. import os
  4.  
  5. #setup the screen
  6.  
  7. wn = turtle.Screen()
  8. wn.bgcolor('black')
  9. wn.title('Space Invaders')
  10.  
  11.  
  12.  
  13.  
  14. #Draw border
  15. border_pen = turtle.Turtle()
  16. border_pen.speed(0)
  17. border_pen.color('white')
  18. border_pen.penup()
  19. border_pen.setposition(-300,-300)
  20. border_pen.pendown()
  21. border_pen.hideturtle()
  22. border_pen.pensize(3)
  23. for side in range(4):
  24.     border_pen.fd(600)
  25.     border_pen.lt(90)
  26.  
  27. #create the player turtle
  28.  
  29. player = turtle.Turtle()
  30. player.penup()
  31. player.color('blue')
  32. player.shape('triangle')
  33. player.speed(0)
  34. player.setposition(0,-250)
  35. player.setheading(90)
  36.  
  37. #Enemy
  38. enemy = turtle.Turtle()
  39. enemy.penup()
  40. enemy.color('red')
  41. enemy.shape('circle')
  42. enemy.speed(0)
  43. enemy.setposition(-200, 250)
  44.  
  45. enemyspeed = 2
  46.  
  47.  
  48. #Movement
  49. playerspeed = 20
  50.  
  51.  
  52.  
  53.  
  54. def move_left():
  55.     x = player.xcor()
  56.     x -= playerspeed
  57.     if x <-280:
  58.         x = - 280
  59.     player.setx(x)
  60.    
  61.  
  62. def move_right():
  63.     x= player.xcor()
  64.     x +=playerspeed
  65.     if x >280:
  66.         x = 280
  67.     player.setx(x)
  68.  
  69.  
  70. #create keybinds
  71. turtle.listen()
  72. turtle.onkey(move_left, 'a')
  73. turtle.onkey(move_right, 'd')
  74.  
  75. #main loop
  76. while True:
  77.  
  78.     #move the enemy
  79.     x = enemy.xcor()
  80.     x += enemyspeed
  81.     enemy.setx(x)
  82.  
  83.     #Enemy Reverse
  84.     if enemy.xcor() > 280:
  85.         enemyspeed *= 1
  86.  
  87.     if enemy.xcor() < -280:
  88.         enemyspeed *= 1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top