Advertisement
Guest User

Ping pong

a guest
Apr 9th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.75 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 left
  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.             if Ball.colliderect(LT):
  78.                 print("Collido con P1")
  79.                 self._X = -1
  80.                 self._Y = random.randint(-5, 5)
  81.  
  82.             elif Ball.colliderect(RT):
  83.                 print("Collido con P2")
  84.  
  85.                 self._X = 1
  86.                 self._Y = random.randint(-5, 5)
  87.  
  88.             elif self._PosB[1] >= 475 and self._X == -1:
  89.                 self._Y = random.randint(-5, -1)
  90.                 print("Collido in basso da sinistra")
  91.  
  92.             elif self._PosB[1] >= 475 and self._X == 1:
  93.                 self._Y = random.randint(-5, -1)
  94.                 print("Collido in basso da destra")
  95.  
  96.             elif self._PosB[1] <= 5 and self._X == -1:
  97.                 self._Y = random.randint(1, 5)
  98.                 print("Collido in alto da sinistra")
  99.  
  100.             elif self._PosB[1] <= 5 and self._X == 1:
  101.                 self._Y = random.randint(1, 5)
  102.                 print("Collido in alto da destra")
  103.  
  104.             elif self._PosB[0] >= 630:
  105.                 print("Ha vinto P1")
  106.  
  107.                 time.sleep(2)
  108.                 self._X = 1
  109.                 self._Y = random.randint(-5, 5)
  110.                 self._Counter1 += 1
  111.  
  112.             elif self._PosB[0] <= 1:
  113.                 print("Ha vinto P2")
  114.  
  115.                 time.sleep(2)
  116.                 self._X = -1
  117.                 self._Y = random.randint(-5, 5)
  118.                 self._Counter2 += 1
  119.  
  120.     def Get_Point(self):
  121.         return self._Counter1, self._Counter2
  122.  
  123.  
  124. pygame.init()
  125. window = pygame.display.set_mode((640, 480))  # Create window size
  126.  
  127. l = [50, 50]                        # Coords of the left rectangle
  128. r = [590, 300]                      # Coords of the right rectangle
  129. bc = [int(640 / 2), int(480 / 2)]   # Coords of the ball
  130.  
  131. b = ball(bc, l, r)                  # Thread of Ball
  132. b.start()                           # Start Thread of Ball
  133.  
  134. run = True
  135. V = 0.5                             # Velocity of rectangles
  136. VLTy = 0                            # Direction and velocity of left rectangle
  137. VRTy = 0                            # Direction and velocity of right rectangle
  138. while run:
  139.     for event in pygame.event.get():
  140.         # If someone close the window
  141.         if event.type == pygame.QUIT:
  142.             b.join()
  143.  
  144.             run = False
  145.             pygame.quit()
  146.             quit()
  147.  
  148.         if event.type == pygame.KEYDOWN:
  149.             # Move up the left rectangle
  150.             if event.key == ord("w") and l[1] > 0:
  151.                 VLTy = -V
  152.  
  153.             # Move down the left rectangle
  154.             elif event.key == ord("s") and l[1] < 370:
  155.                 VLTy = V
  156.  
  157.             # Move up the right rectangle
  158.             if event.key == pygame.K_UP and r[1] > 0:
  159.                 VRTy = -V
  160.  
  161.             # Move down the right rectangle
  162.             elif event.key == pygame.K_DOWN and r[1] < 370:
  163.                 VRTy = V
  164.  
  165.     l[1] += VLTy
  166.     r[1] += VRTy
  167.  
  168.     Draw(window, l, r, bc, b)
  169.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement