OtsoSilver

Untitled

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