Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Thread
- import pygame
- import random
- import time
- def Draw(windows, PosL, PosR, PosB, BClass):
- windows.fill((0, 0, 0)) # Fill the screen black
- Ball = pygame.Rect(int(PosB[0]), int(PosB[1]), 10, 10) # Ball
- # Check UP and Down of left
- if PosL[1] < 380:
- lt = pygame.Rect(int(PosL[0]), int(PosL[1]), 5, 100)
- else:
- lt = pygame.Rect(int(PosL[0]), 370, 5, 100)
- PosL[1] = 380
- if PosL[1] > 0:
- lt = pygame.Rect(int(PosL[0]), int(PosL[1]), 5, 100)
- else:
- lt = pygame.Rect(int(PosL[0]), 0, 5, 100)
- PosL[1] = 0
- # Check UP and Down of right
- if PosR[1] < 380:
- rt = pygame.Rect(int(PosR[0]), int(PosR[1]), 5, 100)
- else:
- rt = pygame.Rect(int(PosR[0]), 370, 5, 100)
- PosR[1] = 380
- if PosR[1] > 0:
- rt = pygame.Rect(int(PosR[0]), int(PosR[1]), 5, 100)
- else:
- rt = pygame.Rect(int(PosR[0]), 0, 5, 100)
- PosR[1] = 0
- pygame.draw.rect(window, (255, 255, 255, 255), Ball) # Draw the ball
- pygame.draw.rect(window, (255, 255, 255, 255), lt) # Draw the left rectangle
- pygame.draw.rect(window, (255, 255, 255, 255), rt) # Draw the right rectangle
- # Draw counter point
- font = pygame.font.SysFont("comicsansms", 25)
- Counter1, Counter2 = BClass.Get_Point()
- Win1 = font.render(str(Counter1), 1, (255, 255, 255))
- Win2 = font.render(str(Counter2), 1, (255, 255, 255))
- windows.blit(Win1, (300, 1))
- windows.blit(Win2, (340, 1))
- class ball(Thread):
- def __init__(self, PosB, RectL, RectR):
- super().__init__()
- self._X = random.randint(-1, 1) # X direction
- self._Y = random.randint(-1, 1) # Y direction
- self._Velocity = 2.5 # Velocity of the ball
- self._PosB = PosB # Ball position
- self._RectL = RectL # Left rectangle position
- self._RectR = RectR # Right rectangle position
- self._Counter1 = 0 # Counter for P1
- self._Counter2 = 0 # Counter for P2
- def run(self):
- while True:
- # Left or right direction (Collision detection of the ball)
- time.sleep(0.01)
- if self._X == 1:
- self._PosB[0] += - self._Velocity
- self._PosB[1] += self._Y
- else:
- self._PosB[0] += self._Velocity
- self._PosB[1] += self._Y
- Ball = pygame.Rect(int(self._PosB[0]), int(self._PosB[1]), 10, 10) # Ball
- LT = pygame.Rect(int(self._RectL[0]), int(self._RectL[1]), 5, 100) # Left rectangle
- RT = pygame.Rect(int(self._RectR[0]), int(self._RectR[1]), 5, 100) # Right rectangle
- # Up or down direction (Collision detection of the ball)
- # Collide with P1
- if Ball.colliderect(LT):
- self._X = -1
- self._Y = random.randint(-5, 5)
- # Collide with P2
- elif Ball.colliderect(RT):
- self._X = 1
- self._Y = random.randint(-5, 5)
- # Collide at the bottom from the left
- elif self._PosB[1] >= 470 and self._X == -1:
- self._Y = random.randint(-5, -1)
- # Collide at the bottom from the right
- elif self._PosB[1] >= 470 and self._X == 1:
- self._Y = random.randint(-5, -1)
- # Collide at the top from the left
- elif self._PosB[1] <= 5 and self._X == -1:
- self._Y = random.randint(1, 5)
- # Collide at the top from the right
- elif self._PosB[1] <= 5 and self._X == 1:
- self._Y = random.randint(1, 5)
- # P1 win
- elif self._PosB[0] >= 630:
- self._PosB = [int(640 / 2), int(480 / 2)]
- time.sleep(2)
- self._X = 1
- self._Y = random.randint(-5, 5)
- self._Counter1 += 1
- # P2 win
- elif self._PosB[0] <= 1:
- self._PosB = [int(640 / 2), int(480 / 2)]
- time.sleep(2)
- self._X = -1
- self._Y = random.randint(-5, 5)
- self._Counter2 += 1
- def Get_Point(self):
- return self._Counter1, self._Counter2
- def Get_PosB(self):
- return self._PosB
- pygame.init()
- window = pygame.display.set_mode((640, 480)) # Create window size
- l = [50, 50] # Coords of the left rectangle
- r = [590, 300] # Coords of the right rectangle
- bc = [int(640 / 2), int(480 / 2)] # Coords of the ball
- b = ball(bc, l, r) # Thread of Ball
- b.start() # Start Thread of Ball
- run = True
- V = 0.5 # Velocity of rectangles
- VLTy = 0 # Direction and velocity of left rectangle
- VRTy = 0 # Direction and velocity of right rectangle
- while run:
- for event in pygame.event.get():
- # If someone close the window
- if event.type == pygame.QUIT:
- b.join()
- run = False
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- # Move up the left rectangle
- if event.key == ord("w") and l[1] > 0:
- VLTy = -V
- # Move down the left rectangle
- elif event.key == ord("s") and l[1] < 370:
- VLTy = V
- # Move up the right rectangle
- if event.key == pygame.K_UP and r[1] > 0:
- VRTy = -V
- # Move down the right rectangle
- elif event.key == pygame.K_DOWN and r[1] < 370:
- VRTy = V
- # Block the rectangle
- if event.type == pygame.KEYUP:
- if event.key == ord("w") or event.key == ord("s"):
- VLTy = 0
- elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
- VRTy = 0
- l[1] += VLTy
- r[1] += VRTy
- bc = b.Get_PosB()
- Draw(window, l, r, bc, b)
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement