Advertisement
Vladkoheca

Galaxy Invaders

Feb 29th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. import turtle
  2. from turtle import *
  3.  
  4. sc = Screen()
  5. sc.setup(500, 620)
  6.  
  7. can_shoot = True
  8. alien_speed = 5;
  9. delay(0)
  10. t = Turtle()
  11. t.up()
  12. t.shapesize(2)
  13. t.tilt(90)
  14. t.speed(0)
  15. t.goto(0, -280)
  16. bullets = []
  17. enemies = []
  18.  
  19. def create_aliens():
  20.     for j in range(3):
  21.        for i in range(8):
  22.            bad = Turtle()
  23.            bad.speed(0)
  24.            bad.up()
  25.            bad.goto(-250 + i * 40, 250 -40*j)
  26.            bad.color("green")
  27.            bad.shape("square")
  28.            enemies.append(bad)
  29.  
  30. create_aliens()
  31. counter = 0
  32.  
  33.  
  34. def move():
  35.    global alien_speed, counter
  36.  
  37.    counter += 1
  38.    if counter > 20:
  39.        counter = 0
  40.    if counter %2 == 0:
  41.        for i in bullets:
  42.            i.fd(15)  # forward = fd
  43.            if i.ycor() > 300:
  44.                i.hideturtle()
  45.                bullets.remove(i)
  46.            # proverka za sblusuk
  47.            for y in enemies:
  48.                if (y.xcor() - 10) < i.xcor() < (y.xcor() + 10) and (y.ycor() - 13) < i.ycor() < (y.ycor() + 13):
  49.                    y.hideturtle()
  50.                    enemies.remove(y)
  51.                    i.hideturtle()
  52.                    bullets.remove(i)
  53.                    # boom
  54.                    if len(enemies)<1:
  55.                        if alien_speed > 1:
  56.                            alien_speed -= 1
  57.                        create_aliens()
  58.  
  59.  
  60.    # aliens movement
  61.    if counter %alien_speed == 0:
  62.         for bad in enemies:
  63.            if bad.xcor() > 250 or bad.xcor() < -250:
  64.                for badx in enemies:
  65.                    badx.sety(badx.ycor() - 20)
  66.                    badx.left(180)
  67.                break
  68.         for bad in enemies:
  69.            bad.fd(5)
  70.  
  71.  
  72.    ontimer(move, 5)
  73.  
  74.  
  75. def move_left():
  76.    t.bk(10)
  77.  
  78.  
  79. def move_right():
  80.    t.fd(10)
  81.  
  82. def allow_shoot():
  83.     global can_shoot
  84.     can_shoot = True
  85.  
  86. def shoot():
  87.     global can_shoot
  88.     if can_shoot:
  89.        s = Turtle()
  90.        s.shape("circle")
  91.        s.speed(0)
  92.        s.shapesize(0.5)
  93.        s.up()
  94.        s.left(90)
  95.        s.goto(t.xcor(), t.ycor())
  96.        bullets.append(s)
  97.        can_shoot = False
  98.        ontimer(allow_shoot, 100)
  99.  
  100.  
  101. sc.onkeypress(move_left, "Left")
  102. sc.onkeypress(move_right, "Right")
  103. sc.onkey (shoot, "space")
  104. sc.listen()
  105.  
  106. move()
  107.  
  108. done()
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement