OtsoSilver

Untitled

Aug 22nd, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #pgzero
  2. import random
  3.  
  4. WIDTH = 600
  5. HEIGHT = 450
  6.  
  7. TITLE = "Космическое путешествие"
  8. FPS = 30
  9.  
  10. # Объекты и переменные
  11. ship = Actor("ship", (300, 400))
  12. space = Actor("space")
  13. enemies = []
  14. planets = [Actor("plan1", (random.randint(0, 600), -100)), Actor("plan2", (random.randint(0, 600), -100)), Actor("plan3", (random.randint(0, 600), -100))]
  15. meteors = []
  16. bullets = []
  17. mode = 'menu'
  18. type1 = Actor('ship1', (100, 200))
  19. type2 = Actor('ship2', (300, 200))
  20. type3 = Actor('ship3', (500, 200))
  21.  
  22.  
  23. # Заполнение списка врагов
  24. for i in range(5):
  25. x = random.randint(0, 600)
  26. y = random.randint(-450, -50)
  27. enemy = Actor("enemy", (x, y))
  28. enemy.speed = random.randint(2, 8)
  29. enemies.append(enemy)
  30.  
  31. # Заполнение списка метеоритов
  32. for i in range(5):
  33. x = random.randint(0, 600)
  34. y = random.randint(-450, -50)
  35. meteor = Actor("meteor", (x, y))
  36. meteor.speed = random.randint(2, 10)
  37. meteors.append(meteor)
  38.  
  39. # Отрисовка
  40. def draw():
  41. # Режим игры
  42. if mode == 'game':
  43. space.draw()
  44. planets[0].draw()
  45. # Отрисовка метеоритов
  46. for i in range(len(meteors)):
  47. meteors[i].draw()
  48. ship.draw()
  49. # Отрисовка врагов
  50. for i in range(len(enemies)):
  51. enemies[i].draw()
  52. # Окно проигрыша
  53. elif mode == 'end':
  54. space.draw()
  55. screen.draw.text("GAME OVER!", center = (300, 200), color = "white", fontsize = 36)
  56. elif mode == 'menu':
  57. space.draw()
  58. type1.draw()
  59. type2.draw()
  60. type3.draw()
  61. # Управление
  62. def on_mouse_move(pos):
  63. ship.pos = pos
  64.  
  65. # Добавление в список нового врага
  66. def new_enemy():
  67. x = random.randint(0, 400)
  68. y = -50
  69. enemy = Actor("enemy", (x, y))
  70. enemy.speed = random.randint(2, 8)
  71. enemies.append(enemy)
  72.  
  73. # Движение врагов
  74. def enemy_ship():
  75. for i in range(len(enemies)):
  76. if enemies[i].y < 650:
  77. enemies[i].y = enemies[i].y + enemies[i].speed
  78. else:
  79. enemies.pop(i)
  80. new_enemy()
  81.  
  82. # Движение планет
  83. def planet():
  84. if planets[0].y < 550:
  85. planets[0].y = planets[0].y + 1
  86. else:
  87. planets[0].y = -100
  88. planets[0].x = random.randint(0, 600)
  89. first = planets.pop(0)
  90. planets.append(first)
  91. def on_mouse_down(buttom,pos):
  92. global ship, mode, bullets
  93. if buttom == mouse.LEFT and mode == 'menu':
  94. if type1.collidepoint(pos):
  95. ship.image = 'ship1'
  96. mode = 'game'
  97. elif type2.collidepoint(pos):
  98. ship.image = 'ship2'
  99. mode = 'game'
  100. elif type3.collidepoint(pos):
  101. ship.image = 'ship3'
  102. mode = 'game'
  103. if buttom == mouse.LEFT:
  104. bullet = Actor('missiles', ship.pos)
  105. bullet.speed = 15
  106. bullets.append(bullet)
  107. # Движение метеоритов
  108. def meteorites():
  109. for i in range(len(meteors)):
  110. if meteors[i].y < 450:
  111. meteors[i].y = meteors[i].y + meteors[i].speed
  112. else:
  113. meteors[i].x = random.randint(0, 600)
  114. meteors[i].y = -20
  115. meteors[i].speed = random.randint(2, 10)
  116.  
  117. # Столкновения
  118. def collisions():
  119. global mode
  120. for i in range(len(enemies)):
  121. if ship.colliderect(enemies[i]):
  122. mode = 'end'
  123.  
  124. def update(dt):
  125. if mode == 'game':
  126. enemy_ship()
  127. collisions()
  128. planet()
  129. meteorites()
Advertisement
Add Comment
Please, Sign In to add comment