Okonar

Арканоїд

Oct 14th, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()
  4.  
  5. back = (200, 255, 255)
  6. mw = pygame.display.set_mode((500, 500))
  7. mw.fill(back)
  8. clock = pygame.time.Clock()
  9. dx = 3
  10. dy = 3
  11.  
  12. platform_x = 200
  13. platform_y = 330
  14. move_right = False
  15. move_left = False
  16. game_over = False
  17.  
  18.  
  19. class Area():
  20. def __init__(self, x=0, y=0, width=10, height=10, color=None):
  21. self.rect = pygame.Rect(x, y, width, height)
  22. self.fill_color = back
  23. if color:
  24. self.fill_color = color
  25.  
  26. def color(self, new_color):
  27. self.fill_color = new_color
  28.  
  29. def fill(self):
  30. pygame.draw.rect(mw, self.fill_color, self.rect)
  31.  
  32. def collidepoint(self, x, y):
  33. return self.rect.collidepoint(x, y)
  34.  
  35. def colliderect(self, rect):
  36. return self.rect.colliderect(rect)
  37.  
  38.  
  39. class Label(Area):
  40. def set_text(self, text, fsize=12, text_color=(0, 0, 0)):
  41. self.image = pygame.font.SysFont('verdana', fsize).render(text, True, text_color)
  42.  
  43. def draw(self, shift_x=0, shift_y=0):
  44. self.fill()
  45. mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))
  46.  
  47.  
  48. class Picture(Area):
  49. def __init__(self, filename, x=0, y=0, width=10, height=10):
  50. Area.__init__(self, x=x, y=y, width=width, height=height, color=None)
  51. self.image = pygame.image.load(filename)
  52.  
  53. def draw(self):
  54. mw.blit(self.image, (self.rect.x, self.rect.y))
  55.  
  56.  
  57. ball = Picture('ball.png', 160, 200, 50, 50)
  58. platform = Picture('platform.png', platform_x, platform_y, 100, 30)
  59. start_x = 5
  60. start_y = 5
  61. count = 9
  62.  
  63. monsters = []
  64. for j in range(3):
  65. y = start_y + (55 * j)
  66. x = start_x + (27.5 * j)
  67. for i in range(count):
  68. d = Picture('enemy.png', x, y, 50, 50)
  69. monsters.append(d)
  70. x = x + 55
  71. count = count - 1
  72.  
  73. game_over = False
  74. while not game_over:
  75.  
  76. mw.fill(back) # Очищуємо поверхню
  77.  
  78. ball.fill()
  79. platform.fill()
  80.  
  81. for event in pygame.event.get():
  82. if event.type == pygame.QUIT:
  83. exit()
  84. game_over = True
  85. if event.type == pygame.KEYDOWN:
  86. if event.key == pygame.K_RIGHT:
  87. move_right = True
  88. if event.key == pygame.K_LEFT:
  89. move_left = True
  90. elif event.type == pygame.KEYUP:
  91. if event.key == pygame.K_RIGHT:
  92. move_right = False
  93. if event.key == pygame.K_LEFT:
  94. move_left = False
  95.  
  96. if move_right:
  97. platform.rect.x += 3
  98. if move_left:
  99. platform.rect.x -= 3
  100. ball.rect.x += dx
  101. ball.rect.y += dy
  102. if ball.rect.y < 0:
  103. dy *= -1
  104. if ball.rect.x > 450 or ball.rect.x < 0:
  105. dx *= -1
  106. if ball.rect.y > 350:
  107. time_text = Label(150, 150, 50, 50, back)
  108. time_text.set_text('ПРОГРАШ', 60, (255, 0, 0))
  109. time_text.draw(10, 10)
  110. game_over = True
  111. if len(monsters) == 0:
  112. time_text = Label(150, 150, 50, 50, back)
  113. time_text.set_text('ВИГРАШ', 60, (0, 200, 0))
  114. time_text.draw(10, 10)
  115. game_over = True
  116. if ball.rect.colliderect(platform.rect):
  117. dy *= -1
  118. for m in monsters:
  119. m.draw()
  120. # якщо монстра торкнувся м'яч, видаляємо монстра зі списку та міняємо напрямки руху м'яча
  121. if m.rect.colliderect(ball.rect):
  122. monsters.remove(m)
  123. m.fill()
  124. dy *= -1
  125. platform.draw()
  126. ball.draw()
  127. pygame.display.update()
  128. clock.tick(40)
  129. pygame.display.update()
  130.  
Advertisement
Add Comment
Please, Sign In to add comment