Advertisement
Guest User

Untitled

a guest
Mar 7th, 2021
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import pygame
  2. import sys
  3.  
  4.  
  5. SIZE = (1000, 600)
  6. FRAMES = 60
  7. win = pygame.display.set_mode(SIZE)
  8. clock = pygame.time.Clock()
  9.  
  10.  
  11. class Player:
  12.     def __init__(self, x, y):
  13.         self.size = (15, 120)
  14.         self.x = x
  15.         self.y = y
  16.  
  17.     def draw(self, surface):
  18.         pygame.draw.rect(surface, (255, 255, 255),
  19.                          pygame.Rect((self.x, self.y), self.size))
  20.  
  21.  
  22. class Ball:
  23.     def __init__(self):
  24.         self.x = SIZE[0] // 2
  25.         self.y = SIZE[1] // 2
  26.         self.vel = [-5, 0]
  27.  
  28.     def move(self):
  29.         self.x += self.vel[0]
  30.         self.y += self.vel[1]
  31.         if self.y <= 20 or self.y >= SIZE[1] - 20:
  32.             self.vel[1] *= -1
  33.         if (self.x <= 45 and bot.y < self.y < bot.y + bot.size[1]) or (self.x >= SIZE[0] - 45 and p.y < self.y < p.y + p.size[1]):
  34.             self.vel[0] *= -1
  35.  
  36.     def draw(self, surface):
  37.         pygame.draw.circle(surface, (255, 255, 255), (self.x, self.y), 10)
  38.  
  39.  
  40. class Bot(Player):
  41.     def __init__(self, x, y):
  42.         super().__init__(x, y)
  43.         self.ball_vel = b.vel
  44.         self.ball_x = b.x
  45.         self.ball_y = b.y
  46.  
  47.  
  48. def draw_screen(surface):
  49.     surface.fill((0, 0, 0))
  50.     p.draw(surface)
  51.     b.draw(surface)
  52.     bot.draw(surface)
  53.     b.move()
  54.     pygame.display.update()
  55.  
  56.  
  57. def key_handler():
  58.     keys = pygame.key.get_pressed()
  59.  
  60.     if (keys[pygame.K_UP] or keys[pygame.K_w]) and p.y >= 20:
  61.         p.y -= 5
  62.     elif (keys[pygame.K_DOWN] or keys[pygame.K_s]) and p.y + p.size[1] <= SIZE[1] - 20:
  63.         p.y += 5
  64.  
  65.  
  66. def main_loop():
  67.     while True:
  68.         for event in pygame.event.get():
  69.             if event.type == pygame.QUIT:
  70.                 sys.exit()
  71.         key_handler()
  72.  
  73.         draw_screen(win)
  74.  
  75.         clock.tick(FRAMES)
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     pygame.init()
  80.     p = Player(SIZE[0] - 45, SIZE[1] // 2 - 60)
  81.     b = Ball()
  82.     bot = Bot(20, SIZE[1] // 2 - 60)
  83.     main_loop()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement