Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import pygame
  2. import math
  3. pygame.init()
  4.  
  5. pygame.font.SysFont('arial', 36)
  6. sc = pygame.display.set_mode((1000, 600))
  7. running = True
  8.  
  9. class Wall():
  10. def __init__(self, x, y, len1, len2, sdvig):
  11. self.x = x
  12. self.y = y
  13. self.len1 = len1
  14. self.len2 = len2
  15. self.sdvig = sdvig
  16. self.color = (0, 125, 255)
  17.  
  18. def move(self):
  19. self.y += self.sdvig
  20.  
  21. def draw1(self,sc):
  22. pygame.draw.rect(sc, (128, 0, 128), (0, 0, 12, 600))
  23. pygame.draw.rect(sc, (128, 0, 128), (988, 0, 12, 600))
  24.  
  25. def draw(self, screen):
  26. pygame.draw.rect(screen, self.color, (self.x, self.y, self.len1, self.len2))
  27.  
  28.  
  29. class Ball():
  30. def __init__(self, x, y, rad, sdvigx, sdvigy):
  31. self.x = x
  32. self.y = y
  33. self.rad = rad
  34. self.sdvigx = sdvigx
  35. self.sdvigy = sdvigy
  36. self.x1 = 0
  37. self.x2 = 0
  38. self.color = (255, 0, 0)
  39.  
  40. def move(self, pl1, pl2):
  41. self.x = self.x + self.sdvigx
  42. self.y = self.y + self.sdvigy
  43. if (self.x + self.rad >= 1000) or (self.x + self.rad <= 0):
  44. self.sdvigx *= -1
  45. if (self.y + self.rad >= 600) or (self.y + self.rad <= 0):
  46. self.sdvigy *= -1
  47. if (self.x - 10 <= pl1.x + 25 and self.y >= pl1.y and self.y < pl1.y + 200):
  48. self.sdvigx *= -1
  49. if (self.x + 10 >= pl2.x and self.y >= pl2.y and self.y <= pl2.y + 200):
  50. self.sdvigx *= -1
  51. if self.x <= 10:
  52. self.x2 += 1
  53. self.x = 500
  54. self.y = 300
  55. if self.x >= 990:
  56. self.x1 += 1
  57. self.x = 500
  58. self.y = 30
  59.  
  60. def draw(self, screen):
  61. pygame.draw.circle(screen, self.color, (self.x, self.y), self.rad)
  62.  
  63. clock = pygame.time.Clock()
  64.  
  65. b = []
  66. player1 = Wall(0, 80, 25, 200, 0)
  67. player2 = Wall(975, 80, 25, 200, 0)
  68.  
  69. while running:
  70. for event in pygame.event.get():
  71. if event.type == pygame.QUIT:
  72. running = False
  73. if event.type == pygame.MOUSEBUTTONDOWN:
  74. if event.button == 1:
  75. b.append(Ball(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1], 10, -20, -20))
  76. if event.type == pygame.KEYDOWN:
  77. if event.key == pygame.K_UP:
  78. if player2.y > 10:
  79. player2.sdvig = -30
  80. player2.move()
  81. if event.key == pygame.K_DOWN:
  82. if player2.y < 390:
  83. player2.sdvig = 30
  84. player2.move()
  85. if event.key == pygame.K_w:
  86. if player1.y > 10:
  87. player1.sdvig = -30
  88. player1.move()
  89. if event.key == pygame.K_s:
  90. if player1.y < 390:
  91. player1.sdvig = 30
  92. player1.move()
  93.  
  94. sc.fill((0, 0, 0))
  95.  
  96. player1.draw1(sc)
  97. for i in b:
  98. i.draw(sc)
  99. i.move(player1, player2)
  100. player1.draw(sc)
  101. player2.draw(sc)
  102. font = pygame.font.Font(None, 25)
  103. text = font.render(str(b[0].x1) + " : " + str(b[0].x2), 1, (0, 100, 0))
  104. place = text.get_rect(center=(500, 30))
  105. sc.blit(text, place)
  106. pygame.display.flip()
  107. pygame.time.wait(90)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement