Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.14 KB | None | 0 0
  1. from threading import Thread
  2. import pygame
  3. import random
  4. import time
  5.  
  6. def Draw(windows, PosL, PosR, PosB, BClass):
  7.     windows.fill((0, 0, 0))  # Fill the screen black
  8.  
  9.     Ball = pygame.Rect(int(PosB[0]), int(PosB[1]), 10, 10)  # Ball
  10.  
  11.     # Check UP and Down of left
  12.     if PosL[1] < 380:
  13.         lt = pygame.Rect(int(PosL[0]), int(PosL[1]), 5, 100)
  14.     else:
  15.         lt = pygame.Rect(int(PosL[0]), 370, 5, 100)
  16.         PosL[1] = 380
  17.  
  18.     if PosL[1] > 0:
  19.         lt = pygame.Rect(int(PosL[0]), int(PosL[1]), 5, 100)
  20.     else:
  21.         lt = pygame.Rect(int(PosL[0]), 0, 5, 100)
  22.         PosL[1] = 0
  23.  
  24.     # Check UP and Down of right
  25.     if PosR[1] < 380:
  26.         rt = pygame.Rect(int(PosR[0]), int(PosR[1]), 5, 100)
  27.     else:
  28.         rt = pygame.Rect(int(PosR[0]), 370, 5, 100)
  29.         PosR[1] = 380
  30.  
  31.     if PosR[1] > 0:
  32.         rt = pygame.Rect(int(PosR[0]), int(PosR[1]), 5, 100)
  33.     else:
  34.         rt = pygame.Rect(int(PosR[0]), 0, 5, 100)
  35.         PosR[1] = 0
  36.  
  37.     pygame.draw.rect(window, (255, 255, 255, 255), Ball)  # Draw the ball
  38.     pygame.draw.rect(window, (255, 255, 255, 255), lt)    # Draw the left rectangle
  39.     pygame.draw.rect(window, (255, 255, 255, 255), rt)    # Draw the right rectangle
  40.  
  41.     # Draw counter point
  42.     font = pygame.font.SysFont("comicsansms", 25)
  43.     Counter1, Counter2 = BClass.Get_Point()
  44.     Win1 = font.render(str(Counter1), 1, (255, 255, 255))
  45.     Win2 = font.render(str(Counter2), 1, (255, 255, 255))
  46.     windows.blit(Win1, (300, 1))
  47.     windows.blit(Win2, (340, 1))
  48.  
  49. class ball(Thread):
  50.     def __init__(self, PosB, RectL, RectR):
  51.         super().__init__()
  52.         self._X = random.randint(-1, 1)   # X direction
  53.         self._Y = random.randint(-1, 1)   # Y direction
  54.         self._Velocity = 2.5              # Velocity of the ball
  55.         self._PosB = PosB                 # Ball position
  56.         self._RectL = RectL               # Left rectangle position
  57.         self._RectR = RectR               # Right rectangle position
  58.         self._Counter1 = 0                # Counter for P1
  59.         self._Counter2 = 0                # Counter for P2
  60.  
  61.     def run(self):
  62.         while True:
  63.             # Left or right direction (Collision detection of the ball)
  64.             time.sleep(0.01)
  65.             if self._X == 1:
  66.                 self._PosB[0] += - self._Velocity
  67.                 self._PosB[1] += self._Y
  68.             else:
  69.                 self._PosB[0] += self._Velocity
  70.                 self._PosB[1] += self._Y
  71.  
  72.             Ball = pygame.Rect(int(self._PosB[0]), int(self._PosB[1]), 10, 10)  # Ball
  73.             LT = pygame.Rect(int(self._RectL[0]), int(self._RectL[1]), 5, 100)  # Left rectangle
  74.             RT = pygame.Rect(int(self._RectR[0]), int(self._RectR[1]), 5, 100)  # Right rectangle
  75.  
  76.             # Up or down direction (Collision detection of the ball)
  77.             # Collide with P1
  78.             if Ball.colliderect(LT):
  79.                 self._X = -1
  80.                 self._Y = random.randint(-5, 5)
  81.  
  82.             # Collide with P2
  83.             elif Ball.colliderect(RT):
  84.                 self._X = 1
  85.                 self._Y = random.randint(-5, 5)
  86.  
  87.             # Collide at the bottom from the left
  88.             elif self._PosB[1] >= 470 and self._X == -1:
  89.                 self._Y = random.randint(-5, -1)
  90.  
  91.             # Collide at the bottom from the right
  92.             elif self._PosB[1] >= 470 and self._X == 1:
  93.                 self._Y = random.randint(-5, -1)
  94.  
  95.             # Collide at the top from the left
  96.             elif self._PosB[1] <= 5 and self._X == -1:
  97.                 self._Y = random.randint(1, 5)
  98.  
  99.             # Collide at the top from the right
  100.             elif self._PosB[1] <= 5 and self._X == 1:
  101.                 self._Y = random.randint(1, 5)
  102.  
  103.             # P1 win
  104.             elif self._PosB[0] >= 630:
  105.                 self._PosB = [int(640 / 2), int(480 / 2)]
  106.  
  107.                 time.sleep(2)
  108.                 self._X = 1
  109.                 self._Y = random.randint(-5, 5)
  110.                 self._Counter1 += 1
  111.  
  112.             # P2 win
  113.             elif self._PosB[0] <= 1:
  114.                 self._PosB = [int(640 / 2), int(480 / 2)]
  115.  
  116.                 time.sleep(2)
  117.                 self._X = -1
  118.                 self._Y = random.randint(-5, 5)
  119.                 self._Counter2 += 1
  120.  
  121.     def Get_Point(self):
  122.         return self._Counter1, self._Counter2
  123.  
  124.     def Get_PosB(self):
  125.         return self._PosB
  126.  
  127.  
  128. pygame.init()
  129. window = pygame.display.set_mode((640, 480))  # Create window size
  130.  
  131. l = [50, 50]                        # Coords of the left rectangle
  132. r = [590, 300]                      # Coords of the right rectangle
  133. bc = [int(640 / 2), int(480 / 2)]   # Coords of the ball
  134.  
  135. b = ball(bc, l, r)                  # Thread of Ball
  136. b.start()                           # Start Thread of Ball
  137.  
  138. run = True
  139. V = 0.5                             # Velocity of rectangles
  140. VLTy = 0                            # Direction and velocity of left rectangle
  141. VRTy = 0                            # Direction and velocity of right rectangle
  142. while run:
  143.     for event in pygame.event.get():
  144.         # If someone close the window
  145.         if event.type == pygame.QUIT:
  146.             b.join()
  147.  
  148.             run = False
  149.             pygame.quit()
  150.             quit()
  151.  
  152.         if event.type == pygame.KEYDOWN:
  153.             # Move up the left rectangle
  154.             if event.key == ord("w") and l[1] > 0:
  155.                 VLTy = -V
  156.  
  157.             # Move down the left rectangle
  158.             elif event.key == ord("s") and l[1] < 370:
  159.                 VLTy = V
  160.  
  161.             # Move up the right rectangle
  162.             if event.key == pygame.K_UP and r[1] > 0:
  163.                 VRTy = -V
  164.  
  165.             # Move down the right rectangle
  166.             elif event.key == pygame.K_DOWN and r[1] < 370:
  167.                 VRTy = V
  168.  
  169.         # Block the rectangle
  170.         if event.type == pygame.KEYUP:
  171.             if event.key == ord("w") or event.key == ord("s"):
  172.                 VLTy = 0
  173.             elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  174.                 VRTy = 0
  175.  
  176.     l[1] += VLTy
  177.     r[1] += VRTy
  178.  
  179.     bc = b.Get_PosB()
  180.     Draw(window, l, r, bc, b)
  181.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement